如何在 WCF 中打开多个 http 端点?

How to open multiple http endpoints in WCF?

目前我有一个工作的 WCF 服务,具有以下 App.Config 端点

        <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>

根据新要求,我们需要在其他端口上打开端点。所以我想要一些像这样的地址设置,我可以通过某些端口路由某些端点

<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>

但是,我不知道如何修改我的配置和代码来实现多个绑定端口。这在 WCF 中可行吗?我认为它应该允许我打开多个侦听器

我发现要做的(因为我们没有使用 IIS,大多数答案似乎都倾向于 IIS)是在项目中添加多个服务以绑定多个 HTTP 侦听器。配置看起来像这样:

        <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>

将所有服务添加到项目安装程序和主条目如下所示:

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