Create, host and consume a WCF Service using the WCF Service Library template in Visual Studio 2008
Create a new project using the WCF Service Library template
- In Visual Studio 2008, create a new WCF Service Library project. For this example I named the project MyWcfServiceLibrary
- The template creates a Class Library project with the following files:
- IService1.cs
- IService1
interface that defines a ServiceContract with the following methods (OperationContracts):- string GetData(int value)
- CompositeType GetDataUsingDataContract(CompositeType composite)
- CompositeType
Class that defines a DataContract with the following properties (DataMembers):- public bool BoolValue
- public string StringValue
- IService1
- Service1.cs
Implementation of the IService1 interface - App.Config
contains sample config entries to define the endpoints for Service1
- IService1.cs
- Compile the project. It should compile without any problems.
Test the WCF Service
- Run the project.
- The WCF Test Client tool should open
- A notification window should pop up telling you that the service is being hosted
- The WCF Test Client should open.
- Take note of the url (http://localhost:8731/Design_Time_Addresses/MyWcfServiceLibrary/Service1/mex)
- A config file is generated that contains the configuration settings you can use if you want to host it somewhere else (like IIS, WAS or your own application)
- You’ll see the Service available as IService1. WSHttpBinding_IService1 is the name of the service as defined in the config file
- Double click on the GetData() method in the left hand pane. Enter a value and click on Invoke. View the results. Rejoice.
Host the WCF Service in IIS using the Personal Webserver
- Create a new ASP.Net Web Application in the same solution and add a reference to the project created above. For this example I named the project MyASPWcfServiceHost. Remember to set this project as the startup project.
- Create the file that will host the service (it’s a svc file)
- Add a new item and choose Text file
- Edit the file and add the following:
<%@ServiceHost language=c# Debug="true" Service="MyWcfServiceLibrary.Service1"%>
- Note the namespace (MyWcfServiceLibrary) which corresponds to the namespace of the Class Library we created earlier.
- Set this file as the start page.
- Update the config file to register the endpoint of the service.
Take a look at the config file generated by the WCF Test Client for some
guidance when necessary. (I used it a lot)
- Edit the web.config file and add the following
binding=”wsHttpBinding”
contract=”MyWcfServiceLibrary.IService1″ />
- Edit the web.config file and add the following
Test the hosting
- Run the application. You should see a page like this:
- This means our service is hosted and working.
- If you read through the page you’ll notice that metadata sharing is not enabled. The page is kind enough to tell us how to turn it on (if we want).
Enable metadata sharing and test again
- Modifying the web.config file as follows
binding=”wsHttpBinding”
contract=”MyWcfServiceLibrary.IService1″ />
binding=”mexHttpBinding”
contract=”IMetadataExchange” />
- Run the application. You should now see a page like this: This page now tells us what to do and how to use the service. Which is what we are going to do next.
Consume the hosted service
We’ll create a console application that uses our hosted service. Fun We can follow the advice of the test page shown above, but not now – let’s do it another way.Create the console application
- Create a new console application (MyConsoleClient) in the same solution. Remember to set it as the startup project.
Create the service proxy
We’ll use Visual Studio to do the work for us instead of running the command line as mentioned in the test page shown above.- Right-click on References and select Add Service Reference.
- Click on “Discover”
- Choose the MyTestService.svc from the list (note the url) and click on OK.
- The proxy class and config file are created and added to the project, as well as additional references.
- In the Object Browser we see the following:
- We have the IService1 interface, as well as a definition of the CompositeType created in the service.
- We also have Service1Client which is what we’ll use to consume the service.
Consume the service
- Add a reference to the proxy:
using MyConsoleClient.ServiceReference1; - Copy the code provided by the test page as a starting point.
- Use the service and display the results.
static void Main(string[] args)
int myValue = 7;
{
Service1Client client = new Service1Client();
string serviceResponse = client.GetData(myValue);
Console.WriteLine(serviceResponse);
Console.ReadLine();
// Always close the client.
client.Close();
} - Run the application and see our output.
Debugging
If you try to debug right now, you might get this message: Unable to automatically step into the server. The remote procedure could not be debugged. This usually indicates that debugging has not been enabled on the server. See help for more information.- The “Server” refers to the hosting server, which is the ASP host.
- Modify the web.config file of the MyASPWcfServiceHost project to enable debugging.
- Modify the
and set it to true.
- Modify the
- Run the project in debug mode again and you’ll be able to step into the actual GetData() method. Pretty cool.
No comments:
Post a Comment