WCF is introduces in .net 3.0
web services is interoperable - because it uses open protocol HTTP and message format as XML.
.Net remoting is not interoperable.
Before WCF, for to build distributed application we used .Net remoting, Enterprise Services, Web services etc.
Why should we use WCF ?
To unify all communication technologies under one umbrella Microsoft come up with WCF.
Lets see one example:
If we have 2 diff clients having diff requirement like
Client 1: Java application to interact with service, Needs message in XML and protocol is HTTP.
Client 2: Needs message in binary format and protocol is ftp.
So to achieve the same
Before WCF: Build web service for client 1 & .net remoting for client 2
After WCF: We can build one wcf application which can be used by both clients and for others also.
Here we can use single WCF service and expose diff endpoint to satisfy diff clients. No need to change application code.
-------------------3: Create WCF service --------------------------------------------------
Now lets create one WCF to achieve the above scenario.
VS --> Class library --> Delete the default class created
Select project --> Add --> New Item --> WCF Service --> Name it as HelloService
When we add WCF service it does 2 things for us.
Hosting of WCF can be done in many ways like Console App, Windows Service, IIS etc..
Lets host it using Console App..
Add --> New Project --> Console App
Add reference to System.ServiceModel assembly and WCF service application
Add --> New Item --> Application Config File (To configure EndPoint and all ...)
EndPoint configuration in config file
Now we need to expose another end point (default one) to exchange metadata of our service. its because when client add a service reference then it allow the service to exchange metadata. For this we have to add another endpoint. For this the configuration will be like bellow.
Also we need to define service behavior. It allow the service to exchange metadata
And link up this service behavior name with <Service>
tagNavigation to define service behavior is <behaviors> --> <ServiceBehaviors> --> <behavior>
Now finally we need to open the service host from console application for communication.
Add reference to System.ServiceModel namespace
Using ServiceHost class we can host our service
Now we can run the console application and wcf service is ready to be invoked by client application.
WCF is used for building Distributed and interepable
application
Distributed : Connected Systems (different parts of this
runs in diff comp nodes and connected each other)
Why we need Distributed application?
- Because to connect other applications like paypal service for payment
- For better scalability (as number of users increases the performance should not decrease)
web services is interoperable - because it uses open protocol HTTP and message format as XML.
.Net remoting is not interoperable.
Before WCF, for to build distributed application we used .Net remoting, Enterprise Services, Web services etc.
Why should we use WCF ?
To unify all communication technologies under one umbrella Microsoft come up with WCF.
Lets see one example:
If we have 2 diff clients having diff requirement like
Client 1: Java application to interact with service, Needs message in XML and protocol is HTTP.
Client 2: Needs message in binary format and protocol is ftp.
So to achieve the same
Before WCF: Build web service for client 1 & .net remoting for client 2
After WCF: We can build one wcf application which can be used by both clients and for others also.
Here we can use single WCF service and expose diff endpoint to satisfy diff clients. No need to change application code.
-------------------3: Create WCF service --------------------------------------------------
Now lets create one WCF to achieve the above scenario.
VS --> Class library --> Delete the default class created
Select project --> Add --> New Item --> WCF Service --> Name it as HelloService
When we add WCF service it does 2 things for us.
- One: Add reference to System.ServiceModel assembly. (it's the core to WCF, It contains all attributes & classes used in WCF)
- Two: Add 2 class files (...*.cs & I..*.cs --> i.e. HeloService.cs & IHelloService.cs)
- The created interface (IHelloService.cs) is decorated with [ServiceContract]. This ServiceContract attribute converts the interface to a WCF service.
- In interface the method decorated with [OperationContract]: It enable that method available for client.
- Created Class (HelloService.cs) is implement the interface (IHelloService).
Hosting of WCF can be done in many ways like Console App, Windows Service, IIS etc..
Lets host it using Console App..
Add --> New Project --> Console App
Add reference to System.ServiceModel assembly and WCF service application
Add --> New Item --> Application Config File (To configure EndPoint and all ...)
EndPoint configuration in config file
<EndPoint> tag is present under <Configuration> --> <System.ServiceModel> --> <Services> --> <Service>
<Service> tag has Some attributes:- Name : Name of the service I.e NamespaceName.className of your service file (HelloService.cs)
- Address: It specifies to client application that where WFC service is available
- Bindings: Bindings depending on what protocol and message formats like basicHttpBinding…(to check all available bindings create <ServiceModel> then <Bindings> check here)
- Contract: Client come to know that what all operations are available within the contract (NameSpaceName.InterfaceName)
Now we need to expose another end point (default one) to exchange metadata of our service. its because when client add a service reference then it allow the service to exchange metadata. For this we have to add another endpoint. For this the configuration will be like bellow.
- Address = mex
- Binding : MexHttpBinding
- Contract: IMetadataExchange
Also we need to define service behavior. It allow the service to exchange metadata
Now finally we need to open the service host from console application for communication.
Add reference to System.ServiceModel namespace
Using ServiceHost class we can host our service
Now we can run the console application and wcf service is ready to be invoked by client application.
No comments:
Post a Comment