what is a service contract, operation contract and Data Contract - part 2
what is a service contract, operation contract and Data Contract? - part 1
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
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
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, 6993👍, 0💬
.NET INTERVIEW QUESTIONS - How can you avoid deadlock in threading? A good and careful planning can ...
What Is Paint Shop Pro? - PSP Tutorials - Fading Images to Background Colors with PSP Paint Shop Pro...
What Are Data Pump Export and Import Modes? - Oracle DBA FAQ - Loading and Exporting Data Data pump ...
What Does a HTML Document Look Like? A HTML document is a normal text file with predefined tags mixe...
How do you target a specific frame from a hyperlink? Include the name of the frame in the target att...