【问题标题】:Assign specific address or port to WCF service将特定地址或端口分配给 WCF 服务
【发布时间】:2018-12-19 02:09:15
【问题描述】:

我在 Angular 中有一个 Web 应用程序,并且我还在这个 Web 应用程序中创建了 WCF 服务。当我运行 Web 应用程序时,URL 是 localhost:53536/#/Page。服务地址为 localhost:53536/Service/Service/svc。我托管了 Web 应用程序,Web 应用程序和 WCF 服务都运行良好。但我想在不同的端口或不同的地址上托管 Web 服务。我怎样才能做到这一点。这是我的 web.config

<system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name="" >

          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <bindings>
      <wsDualHttpBinding>
        <binding name="MobileServiceBinding" closeTimeout="00:00:05" openTimeout="00:00:05">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <services>
      <service name="Web.Mobile.MobileService" >
        <endpoint address="" binding="wsDualHttpBinding"  bindingConfiguration="MobileServiceBinding" contract="Web.Mobile.IMobileService" />
      </service>
    </services>
  </system.serviceModel>

如何为 WCF 服务分配不同的地址?

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    这听起来像是您的项目设置中自动分配的端口,而您的 endpoint 配置中的 address 为空。

    只需在部署 Web 服务时根据您的需要修改 address

    <services>
        <service name="Web.Mobile.MobileService" >
            <endpoint address="http://whatever.you.like:1337" binding="wsDualHttpBinding"  bindingConfiguration="MobileServiceBinding" contract="Web.Mobile.IMobileService" />
         </service>
    </services>
    

    或修改项目配置以在 Visual Studio 中运行/调试 Web 服务。

    【讨论】:

    • 感谢您的回复。我已经这样做了。正如你所说,我已经设置了地址MobileService:1337。当我在浏览器中点击此 url 时,出现此错误当配置中的“system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled”设置为 true 时,端点需要指定相对地址。如果您在端点上指定相对侦听 URI,则地址可以是绝对地址。要解决此问题,请为端点“mobileservice:1337”指定一个相对 uri。
    • 似乎关于 [WCF 错误:相对端点地址](stackoverflow.com/q/15681573/205233) 的答案表明,address 需要留空multipleSiteBindingsEnabled,因此您将不得不放弃它或者您必须坚持使用相对于您的网站的端点地址,不能使用不同的端口。抱歉,我错过了您案例中的特定绑定。
    • 好的。如果我将 false 设置为 multipleSitebinded 会怎样。它仍然无法正常工作。
    • 您是否也尝试按照this answer 中的建议设置baseaddress
    • 我想你唯一的机会是将你的 WCF 服务转移到一个专门的项目中 - 我不熟悉 wsDualHttpBinding 对你的项目的具体影响,但对我来说它看起来是唯一的方法实现你想要的。
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2011-03-20
    相关资源
    最近更新 更多