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.
- 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.
- Address: This is the address of EndPoint. It may look like general Web URL.
- 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.
- 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.
- Create a solution and add a new 'WCF Service Application' to that.
- 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.
- In IService1.cs file we can check the default/ example contracts defined already.
- While adding a new method we need to declare that as [OperationContract] so that method can be accessed over service.
- 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.
- 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.
- Deploy this service on IIS.
- 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.
- 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.
- 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.
- While using we need to create ServiceClient Object and through that object we can access all the methods in the service.
Comments
Post a Comment