Tuesday, May 21, 2013

How to Create WCF Services with Multiple Endpoints and Bindings in visual studio 2012

step 1-------------------------------------------
Create new WCF Application
File-New-Project-WCF Service Application


Step2-----------------------------------------------------
Delete the created files Service1 and IService1 from the project

Step3- ----------------------------------------------------
right click on the project add-new item-WCF service and give it a proper name

after adding this service two file are created in the project one is  IMultipleBindingService.cs interface



[ServiceContract]
    public interface IMultipleBindingService
    {
        [OperationContract]
        void DoWork();
    }



another is MultipleBindingService.svc



public class MultipleBindingService : IMultipleBindingService
    {
        public void DoWork()
        {
        }
    }




Step4---------------------------------------------------------
just make some changes in the IMultipleBindingService.cs


[ServiceContract]
    public interface IMultipleBindingService
    {
        [OperationContract]
        string GetDate();
    }


 and in MultipleBindingService.svc


public class MultipleBindingService : IMultipleBindingService
    {
        public string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }

Step5-----------------------------------------------------------------
Now we created the service that return us a current date time  Lets expose this service through multiple endpoints and bindings

in web.config we configure two endpoints one for Basichttp Binding and other for WSHttpBinding


<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MultipleWcfService.MultipleBindingService" behaviorConfiguration="Mg">
        <endpoint name="BasicHTTPBinding" address="/BasicHttpBinding" binding="basicHttpBinding" contract="MultipleWcfService.IMultipleBindingService" />
        <endpoint name="WSHTTPBinding" address="/wsHttpBinding" binding="wsHttpBinding" contract="MultipleWcfService.IMultipleBindingService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:49714/MultipleBindingService.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Mg">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>


Step6------------------------------------------------------------------

Right Click on the solution and add-new Project- Select Console Application



after creating the Console application right click on the references and add service reference

give the service address and press ok.

Step7----------------------------------------------------------------------

access the service in the client by both endpoints by their name


 static void Main(string[] args)

        {
            MultipleBindingServiceClient proxy = null;
            //BasicHTTPBinding
            proxy = new MultipleBindingServiceClient("BasicHTTPBinding");
            Console.WriteLine(proxy.GetDate());

            //WSHTTPBinding
            proxy = new MultipleBindingServiceClient("WSHTTPBinding");
            Console.WriteLine(proxy.GetDate());
        }


hopefully this post is helpfull for you thanks












1 comment:

  1. Excellent post the beginners. I hope we will see some more useful posts in near future, as well. Thank You.

    ReplyDelete

Earn Money ! Affiliate Program
Open Directory Project at dmoz.org