what is a service contract, operation contract and Data Contract - part 2


what is a service contract, operation contract and Data Contract? - part 1

✍: Guest


In the below sample we have marked the structure productdata to be serialized.

Figure 15.5:- The Service class
As data contract are all about serialization you need to import System.Runtime.Serialization name space.
In the next step we implement the GetTotalCost function. It just returns a simple string with product name and the total cost of all products. Once our service class is done its time to host this service. There are various ways of hosting a WCF service we will look in to the same in the next question. For the current example we will host in their own process.

Figure 15.6 : - Hosting the service
Hosting the WCF service needs two things one is the config file and second is the hosting code on startup. Because we are hosting this service in its own application process this needs to be a windows application. So first let’s have a look what entries do we need to make in the App.config file. In the above figure everything is clear but let’s understand all the section defined in the App.config file.
In the configuration section we need to add a new section . The most important part of <system.serviceModel> is the endpoint tag. As said in the previous answer End point gives three important answers Where, What and How. In short where is the service, what the contract of the service is and how do we communicate with the service.
In the above code snippet we have only defined the contract i.e. what and how that is bindings. The where is defined in the application entry point static void main (). So the contract attribute defines the interface and binding says that the end clients can communicate using “HTTP” protocol.
In Static void Main method we create an object of ServiceHost class and use the open method to host the service. We have used the URI object to define the address where the service will be hosted.
If you compile the project you will see something as shown in the above figure. This says that the service is up and running and ready to serve any WCF client. Now its time to develop consumer which will consume this WCF service. Microsoft has provided a decent automation to generate the client. So below figure depicts the various steps.

Figure 15.8 : - svcutil in action
Go to command prompt of windows SDK and run the following command:
Svcutil <Service hosted URI>
In the above command is the URI on which the service is hosted. One you run the command against the URI it will generate two files one is the config file and the other is the proxy. You can see in the above figure two files are generated serviceGetCost.cs and output.config file. With the help of these two files we will make our client.

Figure 15.9 : - Client code walkthrough
You can see in the above figure we have made WFCClientGetCost project. In that we have added output.config and serviceGetCost.cs to the client project. We have renamed output.config to app.config.
Once we have done with everything its time to write the client code which calls the proxy who in turn will call the service hosted. In the above figure you can see we have the client code also. It’s a simple code we first created the object of the data structure set the values. Then we create the object of the service and call the GetTotalCost function.
If everything is compiled and you run the server and client, you should get your output as shown below.

2007-11-04, 6934👍, 0💬