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:
interface that defines a ServiceContract with the following methods (OperationContracts):
- string GetData(int value)
- CompositeType GetDataUsingDataContract(CompositeType composite)
Class that defines a DataContract with the following properties (DataMembers):
- public bool BoolValue
- public string StringValue
Implementation of the IService1 interface
contains sample config entries to define the endpoints for Service1
- 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
- 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
<service name=”MyWcfServiceLibrary.Service1″ behaviorConfiguration=”MyWcfServiceLibrary.Service1Behavior”>
<serviceDebug includeExceptionDetailInFaults=”False” />
- 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:
- Copy the code provided by the test page as a starting point.
- Use the service and display the results.
static void Main(string args)
Service1Client client = new Service1Client();
int myValue = 7;
string serviceResponse = client.GetData(myValue);
// Always close the client.
- Run the application and see our output.
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 <compilation debug=”false”> and set it to true.
- Run the project in debug mode again and you’ll be able to step into the actual GetData() method. Pretty cool.