WCF - Do's and Dont's

Setting up WCF Service on IIS is a tad bit frustrating if you don't know the ropes. This article lists the subtle nuances of the same. The article enumerates some precautions and best practices, which if implemented, will make the task easier for the developer, especially if he / she is new to the subject or a novice.

IIS is used to host a website. It can also host a WCF Service.
There are many resources on the net that seek to explain the same.
However the subtleties are always omitted and the user is left confused and disillusioned in most cases. (especially if he / she is a novice.).

1) Firstly we need to select an appropriate type of project.Select a WCF Service application.

2) Build your interface to provide stubs for methods that will later be implemented in the svc.cs file.

3) The interface should be tagged as [ServiceContract] and those methods that need to be implemented (exposed by the service) should be tagged [OperationContract]. (use reference to System.ServiceModel.).

4) You can use multiple interfaces (as .net supports multiple interface inheritance). However your implementation class should inherit from all the relevant interfaces.

5) Clean and build your service application project.

6) Publish it by selecting the option for site / application as an application that you have set up in IIS.

A word of caution....

If you have not set up the website in IIS that you wish to publish the service to, the publish will fail.

7) Stop the website in IIS.

8) Ensure that directory browsing is enabled.

A word of caution....

Failure to do so will give error as the service will not be able to be accessed by IIS.

9) There is an option to edit bindings. Here specify a port that you wish to use for the service.

A word of caution....

This port should not already be in use. Also the port should not be blocked by the firewall.

10) In the edit bindings dialog specify the IP address of the machine which will work as you server. This enables access of the service over HTTP through any remote client.

This takes care of the IIS hosting. However the web.config file in the WCF Service project has not been discussed.

There are some interesting features to be remembered with respect to this file.

a) A behavior tag should be included. The service meta data tag should have httpGetEnabled set to true.

b) This behavior configuration tag should be referenced in the service tag.

Endpoints required for IIS hosting are two in number, namely,
- for httpbinding
- for metadataexchange

Both endpoints should list the A, B and C for the service viz. address, binding and contract.

For the httpbinding endpoint leave the address as a blank string.

This has already been configured in an earlier step. i.e. edit binding which will expose the endpoint.

Contract is ClassName.InterfaceName. e.g.)Wcf.IService1

Consuming the hosted service in IIS can be done through a simple console application in C#.

Using the service URL. eg) we should add a Service Reference to the console application.

This enables the console application to consume the WCF Service.

Create a proxy called servicename + "Client" e.g. Service1Client and you should have a line of code that looks like :

Service1Client proxy = new Service1Client();

Use the methods on the proxy e.g. proxy.Add(n1,n2) where n1 and n2 are read by the console app from the screen. (manually inputted).

This will then display the result.

A word of caution....

Be sure to reference the service reference namespace in the "using" list so that we can access the methods of the service through the proxy object created as described above.

That's all there is to it folks...so simple, yet so easy to foul up if not done carefully.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: