WCF (Windows Communication
Foundation) Service
Windows Communication
Foundation (Code named Indigo) is a programming platform and runtime system for
building, configuring and deploying network-distributed services. It is the latest
service oriented technology; Interoperability is the fundamental
characteristics of WCF. It is unified programming model provided in .Net
Framework 4.0. WCF is a combined feature of Web Service, Remoting, MSMQ and
COM+. WCF provides a common platform for all .NET communication.
Advantages of WCF
- WCF is interoperable
with other services when compared to .Net Remoting where the client and
service have to be .Net.
- WCF services provide
better reliability and security in compared to ASMX web services.
- In WCF, there is no need
to make much change in code for implementing the security model and
changing the binding. Small changes in the configuration will make your
requirements.
- WCF has integrated
logging mechanism, changing the configuration file settings will provide
this functionality. In other technology developer has to write the code.
WCF Service components
1) Service Class - A
WCF service class implements some service as a set of methods.
2) Host Environment - A
Host environment can be a Console application or a Windows Service or a Windows
Forms application or IIS as in case of the normal ASMX web service in .NET.
3) Endpoints - All
communications with the WCF service will happen via the endpoints. The endpoint
is composed of 3 parts (collectively called as ABC's of endpoint) as defines
below:
Address: The endpoints specify an Address that defines where the endpoint
is hosted. It’s basically URL.
Ex:http://localhost/WCFServiceSample/Service.svc
Binding: The endpoints also define a binding that specifies how a client
will communicate with the service and the address where the endpoint is hosted.
Various components of the WCF are depicted in the figure below.
- "A" stands
for Address: Where is the service?
- "B" stands
for Binding: How can we talk to the service?
- "C" stands
for Contract: What can the service do for us?
Types of Bindings:
1. BasicHttpBinding: Basic
Web service communication. No security by default.
2. WSHttpBinding: Web
services with WS-* support. Supports transactions.
3. WSDualHttpBinding: Web
services with duplex contract and transaction support.
4. WSFederationHttpBinding: Web
services with federated security. Supports transactions.
5. MsmqIntegrationBinding: Communication
directly with MSMQ applications. Supports transactions.
6. NetMsmqBinding: Communication
between WCF applications by using queuing. Supports transactions.
7. NetNamedPipeBinding: Communication
between WCF applications on same computer. Supports duplex contracts and
transactions.
8. NetPeerTcpBinding: Communication
between WCF applications on same computer. Supports duplex contracts and
transactions
9. NetTcpBinding: Communication
between WCF applications across computers. Supports duplex contracts and
transactions.
10. BasicHttpBinding: Basic
Web service communication. No security by default.
11. WSHttpBinding: Web
services with WS-* support. Supports transactions.
Contract: The endpoints specify a Contract that defines which methods of
the Service class will be accessible via the endpoint; each endpoint may expose
a different set of methods.
Types of contracts
1. Service Contract: describe the
operation that service can provide. For Ex, a Service provides to know the
temperature of the city based on the zip code, this service is called as
Service contract. It will be created using Service and Operational Contract
attribute.
2. Data Contract: Data contract
describes the custom data type which is exposed to the client. This defines the
data types, which are passed to and from service. Data types like int, string
are identified by the client because it is already mention in XML schema
definition language document, but custom created class or data types cannot be
identified by the client e.g. Employee data type. By using Data Contract we can
make client to be aware of Employee data type that are returning or passing
parameter to the method.
3. Message Contract: Default SOAP
message format is provided by the WCF runtime for communication between Client
and service. If it is not meeting your requirements then we can create our own
message format. This can be achieved by using Message Contract attribute.
4. Fault Contract: Suppose the
service I consumed is not working in the client application. I want to know the
real cause of the problem. How I can know the error? For this we are having
Fault Contract. Fault Contract provides documented view for error occurred in
the service to client. This helps us to easy identity, what error has occurred.
Creating simple application using WCF
- First open Visual Studio and click file --> Select New --> Website Under that select WCF Service and give name for WCF Service and click OK
- Once you created application you will get default class files including Service.cs and IService.cs
- Here IService.cs is an interface it does contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where you can all the methods and other stuff.
- Now open IService.cs write the following code
- After that open Service.cs class file and write the following code
- Here we are using basicHttpBinding for that our web.config file system.serviceModel code should be like this and I hope no need to write any code because this code already exists in your web.config file insystem.serviceModel
- Our WCF service ready to use with basicHttpBinding. After completion of WCF service we can call this WCF Service method in console applications.
Calling
WCF Service using Console Application
- To call WCF service we have many ways like using console app, windows app and web app but here I am going for console application.
- Create new console app from visual studio select project type as console application gives some name as you like.
- After Creation Console application now we need to add WCF reference to our console application for that right click on your windows application and select Add Service Reference
- Now one wizard will open in that give your WCF service link and click Go after add your service click OK button.
- After completion of adding WCF Service write the following code in Program.cs class file Main method
- After that open your app.config file and check your endpoint connection for WCF Service reference that should be like this
Now everything is ready run your application that output should
be like this
0 comments:
Post a Comment