Introduction to Service Contract
Normally we write functions (logical implementation) and that should be accessed by the other method to get the benefit. Yes, what if the logic is to be exposed to the outside world? The client might not be a .Net client, perhaps Java or anything. In the SOA world, the messages need to be exchanged in a SOAP format. Until we attribute the class as [ServiceContract] and the method as [OperationContract] the logic will not be exposed to all the types of clients. As per the operations defined in the service contract the SOAP messages with the schemas are exchanged between the client and server. Schemas are nothing but a pattern, it organizes the categories of information with the references. Ok, let us see the procedure involved in creating the service contract
How to define a Service Contract
As a first step we need to create a service interface through the WCF service library. You can find the same in New -> Project -> WCF -> WCF Service Library. After creating the project, add an assembly reference for System.ServiceModel then create an interface as in the following:
As I said earlier, just put the [ServiceContract] attribute over the interface and the [Operation Contract] over the service method. This will include this service operation in the WSDL document. If we didn’t specify the preceding attributes then it will not be included in the WSDLdocument. This can also be called an opt-in model. We should not combine the service interface and the implementation together; if we do then we are breaking the rule of decoupling. Decoupling is none other than abstracting the service definition from the concrete implementation . Now we have done our part in defining the service, next we need to implement the defined service interface.
How to implement it
Create an another project and implement the service interface that we created. Then you can do your own implementation for each and every interface method. The following shows the implementation.
Once you have done your implementation, it is time to think about Hosting the implemented service. Hosting is the bigger concept I will not explain here, also it is irrelevant to this topic, you can follow my future articles in WCF, I will give you all a detailed view on this. So far we have a defined a service interface and implemented it. Now we will see how to utilize it on the client side. The first thing you need to do here is open the service reference and enter the service address to discover it. Ensure your host is running to get discovered.
Based on the service reference added to the project, app. config will be created automatically with the address, binding and contract information. As a next step create a proxy object in the client to communicate the server
Extracting Service MetaData
As said earlier, a WSDL file must be generated for all types of clients to make use of the service. A WSDL document contains method definitions and service operations. To get the WSDL document of a service you must use the command SVCUtil.exe from the Visual Studio command prompt. The following is the syntax that must be used to generate the WSDL document SVCUtil.exe OnlineShoppingService.DLL [DLL Name of the WCF Service Library].In the generated file, you can find the service method definitions. The following shows the part of the WSDL document with the method definitions.
Refer to the code attachment and WSDL document and XSD schemas for further details.
The [Service Contract] and the [Operation Contract] attributes are the foremost attributes to make the service method as accessible to the outside world.