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, 6772👍, 0💬
Which are the various programming approaches for WCF?
What is V model in testing? V model mapís the type of test to the stage of development in a project....
How To Add Column Headers to a Table? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells I...
What are the types of variables x, y, y and u defined in the following code? #define Atype int* type...
How To Assign Debug Privileges to a User? - Oracle DBA FAQ - Introduction to Oracle SQL Developer In...