Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
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
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, 7846👍, 0💬
Popular Posts:
How To Compile a JUnit Test Class? Compiling a JUnit test class is like compiling any other Java cla...
Can you explain in GSC and VAF in function points? In GSC (General System Characteristic) there are ...
What is cross page posting? By default, button controls in ASP.NET pages post back to the same page ...
Can include files be nested? The answer is yes. Include files can be nested any number of times. As ...
Can Two Forms Be Nested? - XHTML 1.0 Tutorials - Understanding Forms and Input Fields Can two forms ...