Skip to main content

How to create a WCF Service Application

WCF is a buzz word now a days. Full form of WCF is 'Windows Communication Foundation'. Though it is not that much difficult to create a wcf service, sometimes its a bit tricky to configure the service correctly. I want to share simple steps to create and configure a WCF service here. But before to that we should go through some important terms of WCF.
  1. EndPoint: A service must have atleast one EndPoint. It is the component of the service that communicates with the client and provides the service operations.
  2. Address: This is the address of EndPoint. It may look like general Web URL.
  3. Bindings: Bindings are what define how an endpoint communicates with the outside world. Each endpoint must have a binding. The binding, which is simply a set of properties, defines things like the transport pattern, the security pattern, and the message pattern. Though most of these things are optional, binding should specify transport at least.
  4. Contracts: Contracts define format and structure of the message, also behavior of the service. Service contracts are well-formed XML documents, typically found in the format of WSDL or XSD. WCF supports the following three types of contracts.
  • Service Contract: It define the operations that a service will perform when executed. They tell about the service such as message data types, operation locations, the protocols the client will need in order to communicate with the service, and the operations the service provides.
  • Message Contract: Message contracts allow the control of SOAP messages that are produced and consumed by WCF.
  • Data Contract: Data contracts specifically define the data that is being exchanged between a client and service. The client and the service must agree on the data contract in order for the exchange of data to take place.
Now we are ready to create a wcf service application. Here are the steps to follow:
  1. Create a solution and add a new 'WCF Service Application' to that.
  2. This by default will add two important files Service1.svc and IService1.cs along with web.config file. Out of these IService1.cs is the interface to write all the signatures of our service and in Service1.svc.cs will have the actual implementation of the methods.
  3. In IService1.cs file we can check the default/ example contracts defined already.
  4. While adding a new method we need to declare that as [OperationContract] so that method can be accessed over service.
  5. All the classes, which need to be exchanged between server and client, should be declared as [DataContract] and all the members with [DataMember]. If these are skipped we will get null objects in the client end.
  6. Method complete implementation should be written in Service1.svc.cs. The logic to fetch or send the data objects will be here in this file.
  7. Deploy this service on IIS.
  8. This service can be used in any application with the url. To use the service methods, We need to generate proxy for this service. This can be done automatically through visual studio or manually with svcutil tool.
  9. To generate proxy automatically we need to add the service reference in our application. Whenever there is change in the service we need to update the service reference.
  10. With 'svcutil.exe http://<hostname>/<servicename>?wsdl' command the proxy will be generated manually. We can add the generated proxy in our application and use the service methods. Even here whenever there is a change in service, we need to change the file manually or the proxy file should be recreted.
  11. While using we need to create ServiceClient Object and through that object we can access all the methods in the service.
These are all the basic steps for creating and accessing a WCF service. All these will be available in any starter guide in a scattered manner. I just want to group all those to one place here.

Comments

Popular posts from this blog

Solution Array for List in WCF

Here is another common problem encounters with WCF Service in initial days. Suppose there is a method which returns a List of Objects the proxy will be generated as method which returns an array of objects. Reason: This problem is due to the default interpretation of collections. While generating proxy for the service all the collections are by default interpreted as arrays. That is why all the lists will be converted to arrays. Solution: If you are generating proxy by adding service reference from visual studio directly, there will be a 'advanced' button. Click that button and select the System.Generic.List as your Collection. This will resolve the problem. If you are generating proxy with svcutil here is the command to do the job. svcutil.exe http://ServerName/ServiceName/Servic.svc?wsdl /collectionType:System.Collections.Generic.List`1 or in short svcutil http://ServerName/ServiceName/Servic.svc?wsdl /ct:System.Collections.Generic.List`1 for more information go through this ...

Linq: Query for multiple join conditions in Linq to SQL

This is common to join a table with another on multiple conditions, Like Select * from Customer c Join Invoice i on c.CustomerID = i.CustomerID and c.CustomerTypeID = i.CustomerTypeID I need to write a Linq to SQL query for the same scenario. But the syntax for this query is a bit different to expect. The Linq query is as follows: From c in DataContext .Customers Join i in DataContext .Invoices on new { c.CustomerID, c.CustomerTypeID } equals new { i.CustomerID, i.CustomerTypeID } *DataContext in the above query should be replaced with actual datacontext name.

Troubleshoot WCF service returning object with null fields

When you are working with WCF service for the first time, generally you will get this error for some reason. Here is the description of the error: WebServiceReference . ServiceClient client = new WebServiceReference . ServiceClient (); WebServiceReference . ImplementationRequest req = new WebServiceReference . ImplementationRequest (); req = client.GetRequest(); Here the Method GetRequest will return an object of ImplementationRequest. Everything will run without any error but the req object will contain all null fields/ majority null fields. Solution: Those nulls are due to error in generating the proxy for the Data Contract defined by the service. Verify that [DataMember] attribute tag is given for all the data fields and [DataContract] for the Class. If those attributes are missing, the object will be generated with null values. If all fields have the tags but the issue is not resolved, then the service reference should be re-added and the proxy should be re-generated. Then t...