Ferry Mulyono

See also: Other Geeks@INDC

WCF ServiceHost - Single contract multiple endpoints

It is possible to create multiple endpoints in WCF from one service contract declaratively (by declaring it in the configuration file), but it is also possible to do it programmatically (I think almost everything that can be done declaratively can also be done programmatically, but I’m not very sure about this one though).

Declarative settings

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <system.serviceModel>

        <bindings>

            <netMsmqBinding>

                <binding name="MyBinding" exactlyOnce="false" maxReceivedMessageSize="131072">

                  <!-- ... -->

                </binding>

            </netMsmqBinding>

        </bindings>

     

        <services>

            <service behaviorConfiguration="ServiceConfiguration" name="MyApp.MyService">

                <endpoint address="endpoint_address1" binding="netMsmqBinding"

                    bindingConfiguration="MyBinding" contract="MyAssembly.IContract" />

               <endpoint address="endpoint_address2" binding="netMsmqBinding"

                    bindingConfiguration="MyBinding" contract="MyAssembly.IContract" />

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

            </service>

        </services>

 

          <behaviors>

            <serviceBehaviors>

              <behavior name="ServiceConfiguration" >

              <!-- Add the following element to your service behavior configuration. -->

              <serviceMetadata httpGetEnabled="true" />

            </behavior>

          </serviceBehaviors>

        </behaviors>

     

    </system.serviceModel>

</configuration>

 

You can specify as many endpoints as you want to each service here.

Programmatically

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <system.serviceModel>

        <bindings>

            <netMsmqBinding>

                <binding name="MyBinding" exactlyOnce="false" maxReceivedMessageSize="131072">

                  <!-- ... -->

                </binding>

            </netMsmqBinding>

        </bindings>

     

        <services>

            <service behaviorConfiguration="ServiceConfiguration" name="MyApp.MyService">

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

            </service>

        </services>

 

          <behaviors>

            <serviceBehaviors>

              <behavior name="ServiceConfiguration" >

              <!-- Add the following element to your service behavior configuration. -->

              <serviceMetadata httpGetEnabled="true" />

            </behavior>

          </serviceBehaviors>

        </behaviors>

     

    </system.serviceModel>

</configuration>

 

Note that I removed the endpoints part. This is to ensure that the endpoints created are from my code, instead of my configuration file. Next is the ServiceHost creation, where I will define the endpoints.

MyService ms = new MyService();

ServiceHost svc = new ServiceHost(ms, new Uri("http://myuri.org"));

svc.AddServiceEndpoint(typeof(MyAssembly.IContract), new System.ServiceModel.NetMsmqBinding("MyBinding"), "endpoint_address1");

svc.AddServiceEndpoint(typeof(MyAssembly.IContract), new System.ServiceModel.NetMsmqBinding("MyBinding"), "endpoint_address2");

svc.Open();

 

Best Regards,

 

Ferry Mulyono

Share this post: | | | |
Posted: May 17 2007, 10:08 AM by ferry | with no comments
Filed under: ,

Comments

No Comments