Professional Documents
Culture Documents
Introduction
What is .NET 3.0?
What is Windows Card Space?
What is WCF?
What are the important principles of SOA (Service oriented Architecture)?
What are ends, contract, address, and bindings?
Which specifications does WCF follow?
What are the main components of WCF?
Explain how Ends, Contract, Address, and Bindings are done in WCF?
what is a service class?
what is a service contract, operation contract and Data Contract?
what are the various ways of hosting a WCF service?
How do we host a WCF service in IIS?
what are the advantages of hosting WCF Services in IIS as compared to self-hosting?
what are the major differences between services and Web services?
What is the difference WCF and Web services?
What are different bindings supported by WCF?
Which are the various programming approaches for WCF?
What is one-way operation?
Introduction
In this section we will run through a quick FAQ for WCF. I am sure after reading this
you will get a good understanding of the fundamentals of WCF.
In case you like the article or your think I need improvements please send me a
message at http://www.questpond.com . I am sure every one needs improvements.
Enjoy
Click here to see Windows Communication Framework (WCF) - Part 2
What is WCF?
First let us give a short answer to this: - WCF (Indigo was the code name for WCF)
is a unification of .NET framework communication technologies .WCF is a unification
technology, which unites the following technologies: NET remoting
MSMQ
Web services
COM+.
Below figure depicts WCF fundamentals pictorially.
In SOA, everything is formalized. The client who is consuming the service does not
need to know how the implementation of the service is done. If you look at some old
methodologies of communication like DCOM. Any changes at server level the client
also has to change. Therefore, the server and client implementation was so much
bound that changes need to be done at all places. In SOA, the rule is if you do
enhancement you do not need to change anything at the client. SOA based
application only understands that there is an end point, contract, and bindings.
Note: - Just to clarify shortly about end point and contract. Any SOA service
is exposed through an end point. End point defines three important aspects
What, Where and How. We will understand more details of the same in the
later questions.
Services evolve
Change is the law of nature and services will evolve. In SOA, services can be
versioned and you can host those services in new ends. For instance, you have a
service called as Search Tickets (Ticket Number) which gives details based on
Ticket Number and its exposed on end point ep1. Tomorrow you want make your
Search Tickets service more useful by also providing an extra option of allowing him
to search by passenger name. Therefore, you just declare a new end ep2 with
service Search Tickets (Ticket Number, Passenger Name). So the client who is
consuming the service at end ep1 continues and at the other end, we have evolved
our service by adding new ends ep2.
Services use Schemas to represent data and contracts to understand behavior. They
do not use language dependent types or classes in order to understand data and
behavior. XML is used to define schemas and contracts. Due to this, there is not
heavy coupling between environments.
Policy describes the capabilities of the system. Depending on policies, the service can
degrade to match the service for the client. For instance your service needs to be
hosted for two types of client one which uses Remoting as the communication
methodology while other client uses DCOM. An ideal SOA service can cater to both of
them according to there communication policies.
Note: - Many people assume Web services are the base for SOA. The answer
is 50 % right. What web services lack is the policy based Service
compatibility. If you host a web service it can only serve with HTTP
communication channel and SOAP message. Any other type of client trying
to communicate he will not degrade it self. This is what is provided by WCF.
You can host the service in one or more mode. For instance you can host a
WCF service using remoting and ASMX.
Contract (What)
Contract is an agreement between two or more parties. It defines the protocol how
client should communicate with your service. Technically, it describes parameters and
return values for a method.
Address (Where)
An Address indicates where we can find this service. Address is a URL, which points
to the location of the service.
Binding (How)
Note: - You can also remember the end point by ABC where A stands for
Address, B for bindings and C for Contract.
Note: - During interview the interviewer expects that you know what WS-*
specification are supported by WCF and its advantages with respect to
interacting with other languages.
Explain how Ends, Contract, Address, and Bindings are done in WCF?
In this project, we add a new class and name it as serviceGetCost.cs. This class will
have our core implementation and this is the class, which has all the action. The
service class, which has to be exposed to the external client. We need to use the
Service Contract attribute to mark it as a service class.
Service Contract attribute define saying which application interface will be exposed
as a service.
You can see in the below code snippet we have made an interface and marked it as
Service Contract. It is not essential that you need to use an interface you can also
use a simple class and mark it as Service but interface represent a contract and do
not have implementation. In short, they stand at a very higher level of abstraction.
So as a good design practice-using interface to represent a service contract makes
more sense.
The next thing to note is the Operation Contract attribute.
Operation Contract dictates which methods should be exposed to the external client
using this service.
It defines individual exchange or request and replies. In the current sample, we have
defined GetTotalCost method, which will be used by the end client to get the total
cost results.
The next thing to note in the code snippet is the Data Contract attribute. In the
previous two steps, we have exposed class as a service by using Service Contract
and methods by using Operation Contract. Every operation will definitely do some
kind of data transfer.
Data Contract attributes defines which type of complex data will be exchanged
between the client and the service. They determine which parameters to be
serialized.
When you are using simple data types like int, bolo etc it is not necessary that you
need to mark the data contract attribute. Because you will always find matching
types on the client. However, complex structure like one shown in the below code
snippet you will need to define a data contract. Remember data contract define how
this data will be passed during transmission. In short data contract attribute define
how data will be serialized will transmission.
In the below sample we have marked the structure product data to be serialized.
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 us have a look what
entries do, we need to make in the App.config file. In the above figure, everything is
clear but let us understands all the section defined in the App.config file.
In the configuration section, we need to add a new section <system.serviceModel>.
The most important part of <system.serviceModel> is the endpoint tag. As said in
the previous answer End 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 ().
Therefore, 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 Service Host 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. Therefore, below figure depicts
the various steps.
Go to command prompt of windows SDK and run the following command:Svcutil <Service hosted URI>
In the above command <Service Hosted URI> 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.
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 is 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.
First thing you will need is to create the SVC file, which exposes the service class.
SVC file contains the pointer to the class. You can see from the figure below the class
attribute points to the class whose interface is exposed by the service.svc.cs file.
Also, note the actual interface is in service.svc.cs file. Below figure, have both the
files service.svc, which has the class attribute which points to the service class, and
the interface, which resides in service.svc.cs file. We have taken the same sample,
which was self-hosted in the previous question.
We also need to provide implementation for the interface. So we have made a class
ServiceGetCost which has the actual implementation. Below figure shows the same
in detail. In the below figure you can also see the solution files.
We also need to specify the service type and endpoint in web.config file. Also, note
we have specified HTTP binding because we will be hosting the service on IIS.
Now that we are done with the coding part. We need to create the virtual directory in
IIS. In the below figure in Step1 and Step2 we have shown how to create the virtual
directory in IIS. One important thing to note while creating virtual directory set the
access permission to execute.
In the third step, we will publish the website to our virtual directory. Note the fourth
step in which we have copied the svc file so that the service can be requested.
Note: - ASP.NET compilation has changed in ASP.NET 2.0. In 2.0 there is no concept
of solution files. So if you want to have full compiled DLL you need to publish the
project to a virtual directory.
Once you have hosted the SVC file you can test the same by request the service.svc
file. If everything works fine you will get something as shown in the below figure
Using the Svcutil.exe, you will need to generate the proxy class and the config file.
The proxy and config will be same, as we had done for self-hosting. The one
important change is the address. The config file URL now points to the service.svc,
which is hosted on IIS. You can run the same client, which we had created for selfhosting. The only change you will need to do is change the endpoint address.
LOLYou should get the same output, which we had received, for self-hosting.
Automatic activation
IIS provides automatic activation that means the service is not necessary to be
running in advance. When any message is received by the service it then launches
and fulfills the request. But in case of self hosting the service should always be
running.
Process recycling
If IIS finds that a service is not healthy that means if it has memory leaks etc, IIS
recycles the process. Ok let us try to understand what is recycling in IIS process. For
every browser instance, a worker process is spawned and the request is serviced.
When the browser disconnects the worker, process stops and you loose all
information. IIS also restarts the worker process. By default, the worker process is
recycled at around 120 minutes. So why does IIS recycle. By restarting the worker
process it ensures any bad code or memory leak do not cause issue to the whole
system.
In case of self-hosting both the above features, you will need to code yourself. Lot of
work right!!. That is why IIS is the best option for hosting services until you are
really doing something custom.
Below figure shows where the recycle option is located in IIS. You need to click on
the DefaultAppool and then Properties.
BasicHttpBinding: - This binding is used when we need to use SOAP over HTTP. This
binding can also be configured to be used as HTTPS. It can be also configured to
send data in plain text or in optimized form like MTOM.
Above is the code snippet, which describes practically how one way works in WCF.
The above given code snippet is numbered. Below is the explanation according to the
numbers marked in figure:1 - This is the code snippet of the server service. We have created a method called
as doHugeTask. DoHugeTask makes the method sleep for 5000 MS and then displays
the time when the task is completed.
2 - This code snippet is for client. It creates a proxy object of serviceIsOneWay and
calls the doHugeTask method. After calling the doHugeTask, the client execution
continues ahead. So as a proof, we display the time when the method calling was
completed.
3 - This screen shot shows the output given by both server and client. The top
window displays the server output and the below windows displays the client output.
Note: - You can find the code for the same in WCFIsOneWay folder. For
generating the proxies you have to follow the same steps which are shown
in the previous steps.
So run the server program first i.e. ServiceIsOneWay and run the client later. You will
see the client runs the doHugeTask and moves ahead. Therefore, the client
completion time is less than the server is. One more thing to understand is that one
way does not give any notification back of completion. Therefore, it is like fire and
forgets.
License