Home > Net >  How to open multiple http endpoints in WCF?
How to open multiple http endpoints in WCF?

Time:11-01

Currently I have a working WCF Service with the following App.Config endpoint

        <services>
            <service behaviorConfiguration="ServiceBehavior" name="ProxyWindowsService.HPCommands">
                <endpoint address="" binding="basicHttpBinding" contract="ProxyWindowsService.HPCommandsInterface"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://127.0.0.1:8004/ProxyService/HPCommands"/>
                    </baseAddresses>
                </host>
            </service>
        </services>

Based on new requirements, we would need to open endpoints on additional ports. So I would like some address setup like this where I can route certain endpoints via certain ports

<baseAddresses>
    <add baseAddress="http://127.0.0.1:8004/ProxyService/HPCommands/Command1" />
    <add baseAddress="http://127.0.0.1:8005/ProxyService/HPCommands/Command2" />
    <add baseAddress="http://127.0.0.1:8006/ProxyService/HPCommands/Command3" />
</baseAddresses>

However, I can not figure out how to modify my config and code to achieve multiple bound ports. Is this possible with WCF? I would think it should allow me to open multiple listeners

CodePudding user response:

What I found to do (since we are not using IIS, which most answers seem to lean toward) was add multiple services in the project to bind multiple HTTP listeners. The config looked something like this:

        <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service.Commands">
                <endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://127.0.0.1:80/Service/Commands" />
                    </baseAddresses>
                </host>
            </service>
            <service behaviorConfiguration="ServiceBehavior" name="Service.Commands2">
                <endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface2" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://127.0.0.1:81/Service/Commands" />
                    </baseAddresses>
                </host>
            </service>
            <service behaviorConfiguration="ServiceBehavior" name="Service.Commands3">
                <endpoint address="" binding="basicHttpBinding" contract="Service.CommandsInterface3" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://127.0.0.1:82/Service/Commands" />
                    </baseAddresses>
                </host>
            </service>
        </services>

With all services added to the Project Installer and a Main entry looking like so:

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new Service1(), new Service2(), new Service3()
            };
            ServiceBase.Run(ServicesToRun);
        }
  • Related