Search This Blog

WCF EndPoints (Define through code & configuration)


ABC of Endpoints (WCF):
    Address : Where WCF service is available (e.g. http, tcp-ip address)
    Bindings: How WCF service should be communicated (e.g. basicHttpBinding, wsHttpBinding etc)
    Contract: What WCF service can do for you. Think of it as public web methods which provide some service.

 

Defining EndPoints:
Endpoints can be defined by configuration & code. Better practice is to define end points by configuration (in web.config if WCF service is deployed in IIS, or aap.config if WCF service is hosted in/as stand-alone managed application) , as change in any attribute of endpoints is easy in config file (vs code file, which need to be compiled)

Defining Endpoint of WCF service in config file:
    <system.ServiceModel>
        <services>
            <service name = "NameSpace.Class>
                <endpoint binding = "wsHttpBinding"
                 address = "http://localhost:25037/SomeName"
                     contract= "NameSpace.Interface_Name1"/>
                <endpoint binding = "basicHttpBinding"
                 address = "http://localhost:25037/SomeName/SomeOtherName"
                     contract= "NameSpace.Interface_Name2"/>
            </service>
        </services>
    </system.ServiceModel>

 

    We can also define base address for given service. In this case, 'address' attribute of 'endpoint' will have relative address to WCF service.
    <system.ServiceModel>
        <services>
            <baseAddress>
                <add baseAddress="http://localhost:25037/SomeName"/>
            </baseAddress>
            <service name = "NameSpace.Class>
                <endpoint binding = "wsHttpBinding"
                 address = ""
                     contract= "NameSpace.Interface_Name1"/>
                <endpoint binding = "basicHttpBinding"
                 address = "SomeOtherName"
                     contract= "NameSpace.Interface_Name2"/>
            </service>
        </services>
    </system.ServiceModel>

 


 

Defining EndPoints of WCF service by code (C#)
    // (1) Define base address
    Uri httpAddress = new Uri("http://localhost:25037/SomeName");    


    //(2) Define host:
    ServiceHost myHost = new ServiceHost(typeof(Name_Space.Class_Name),httpAddress);

    //(3) Define Bindings:
    BasicHttpBinding bBind = new BasicHttpBinding();
    WSHttpBinding      wBind = new WSHttpBinding();
    
    //(4) Add services in host.
    myHost.AddServiceEndPoint(
            typeof(NameSpace.Class_Name1),    /*fully qualified class name where operations are defined*/
            wBind, /*Binding object*/
            "" /*Relative Address*/
    );
    myHost.AddServiceEndPoint(
            typeof(NameSpace.Class_Name2),    /*fully qualified class name where operations are defined*/
            bBind, /*Binding object*/
            "SomeOtherName" /*Relative Address*/
    );
   
 

Keywords: WCF, Windows Communication Foundation, ABCs of WCF, EndPoints, Define Endpoints using Config file, Define EndPoints through code (C#), WCF EndPoints through code and configuration.

No comments:

Post a Comment