【问题标题】:How to open multiple http endpoints in WCF?如何在 WCF 中打开多个 http 端点?
【发布时间】:2021-12-13 18:17:06
【问题描述】:

目前我有一个使用以下 App.Config 端点的 WCF 服务

        <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可以做到这一点吗?我认为它应该允许我打开多个侦听器

【问题讨论】:

  • 将使用代码进行设置以读取配置并以编程方式创建绑定。
  • @AliK 哦,好吧,所以我假设这意味着我将使用 serviceHost.AddServiceEndpoint 并且传入的服务类型将与我的命令相关。好奇这是否意味着我从 App.config 中删除了绑定定义——谢谢
  • 从技术上讲,您可以离开它们并从 comfig 或数据库中读取。
  • 你可以参考this post,想办法解决。

标签: c# .net wcf


【解决方案1】:

我发现要做的(因为我们没有使用 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);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-17
    • 2011-09-19
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多