Wednesday 14 May 2014

Introduction to WCF.

WCF( Windows Communication Foundation ):- Windows Communication Foundation (code named Indigo) is a programming platform and operating system to build, configure and deploy distributed network services. This is the latest technology services; interoperability are the basic characteristics of the Working Capital Fund. Is a unified programming model provided by the Net Framework 3.0. WCF is a combined features of Web Service, Remoting, and MSMQ COM +. Provides Working Capital Fund a common platform for all. NET communications.




What is EndPoint ?

IIS 5/6 Hosting

The main advantage of hosting service is that IIS will automatically start when the host process is put the client's request first. IIS uses features such as recycling process, and the closure of inactivity, health monitoring and activation process messages list. The main disadvantage of using IIS is that the HTTP protocol support only.
That like some hands, to create a set of service and IIS.


Step1:- Start program Visual Studio 2008, and then click File> New> Web site. Select 'Service WCF "and the site as HTTP. Will host this service directly into IIS, and then click OK.

 
Step2:- I created HelloWorld service appears to accept as input a name and back with 'hello' and the name. Are then displayed interface and service implementation.



IMyService.cs
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string HelloWorld(string name);   

}
MyService.cs
public class MyService : IMyService
{

    #region IMyService Members

    public string HelloWorld(string name)
    {
        return "Hello " + name;
    }

    #endregion
}
  Step3:-Service file (. SVC) contains the name of the service and the code behind the file name. It uses this file to identify the service.



MyService.svc
<%@ ServiceHost Language="C#" Debug="true"
Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>
  Step4:-According to the settings in the server configuration file. I'm here to have a single endpoint that is configured for recovery 'wsHttpBinding', may also have multiple endpoint with differnet binding. As we will host in IIS. We only use HTTP link. Let's get to know more at the end, and its configuration in a later tutorial. Web.config file



<system.serviceModel>
  <services>
   <service behaviorConfiguration="ServiceBehavior" name="MyService">
        <endpoint address="http://localhost/IISHostedService/MyService.svc"
        binding="wsHttpBinding" contract="IMyService">
        <identity>
        <dns value="localhost"/>
        </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
   </service>
 </services>
 <behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
        <!-- To avoid disclosing metadata information,
        set the value below to false and remove the
        metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for
        debugging purposes, set the value below to true. 
        Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
   </serviceBehaviors>
  </behaviors>
</system.serviceModel>
  
Note: It is necessary to mention the name of the service, along with references to addresses in the configuration file. Snapshot IIS


   
 This screen will appear when we run the application.

 
Step5:Now we get a successful service in IIS. Then we need to consume this service in the client application. Before creating a client application, we need to create a proxy for this service. This agent is used by the client application to interact with the service. To create a proxy, run Visual Studio 2008 command prompt. Using the service tool that can create a proxy class and configuration information.



svcutil  http://localhost/IISHostedService/MyService.svc
















 After executing this command we will find two file generated in the default location
  •    MyService.cs - Proxy class for the WCF service
  •    output.config - Configuration information about the service
 Step6:Now let's start creating the console application with Visual Studio 2008 (client application).
 
Step7:-Add the reference 'System.ServiceModel'; this is the core dll for WCF.




Step8:- Create the object for the proxy class and call the HelloWorld method.


static void Main(string[] args)
        {
            //Creating Proxy for the MyService
             MyServiceClient client = new MyServiceClient();
             Console.WriteLine("Client calling the service...");
             Console.WriteLine(client.HelloWorld("Ram"));
             Console.Read();

        }

 Step9:-If we run the application we will find the output as shown below.

0 comments:

Post a Comment