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, 6934👍, 0💬
What is the difference between Session State and ViewState? ViewState is specific to a page in a ses...
How To Recover a Dropped Index? - Oracle DBA FAQ - Managing Oracle Table Indexes If you have the rec...
How To Save Query Output to a Local File? - Oracle DBA FAQ - Introduction to Command-Line SQL*Plus C...
How do you target a specific frame from a hyperlink? Include the name of the frame in the target att...
What does AddressOf operator do in background ? The AddressOf operator creates a delegate object to ...