如何将单个 WCF 服务配置为具有多个 HTTP 和 HTTPS 端点?

发布于 2024-09-25 07:04:43 字数 3492 浏览 1 评论 0原文

我想做的是让单个 WCF 服务在开发环境(即 HTTP 方案)中工作,并且让相同的服务在生产环境(即 HTTPS 方案)中工作。如果我删除两个 Https 端点(那些后缀为“Https”的端点),它可以在开发环境中工作;同样,如果我仅删除两个 Http 端点,那么它可以在生产环境中运行。如果可能的话,我希望在 web.config 中包含所有四个端点。

我的端点定义如下:

<endpoint address="/Web" 
        behaviorConfiguration="AjaxBehavior"
        binding="wsHttpBinding" 
        bindingConfiguration="web" 
        name="Web"
        contract="Service" />
<endpoint address="/Custom"
        binding="customBinding" 
        bindingConfiguration="custom" 
        name="Custom"   
        contract="Service" />
<endpoint 
        address="/WebHttps" 
        behaviorConfiguration="AjaxBehavior"
        binding="wsHttpBinding" 
        bindingConfiguration="webHttps" 
        name="WebHttps"
        contract="Service" />
<endpoint address="/CustomHttps"
        binding="customBinding" 
        bindingConfiguration="customHttps" 
        name="CustomHttps" 
        contract="Service" />

编辑:我正在编辑我的问题以添加我收到的错误以及绑定部分(如下)。抱歉问题的新长度。

错误是: “找不到与绑定 WebHttpBinding 的端点的方案 http 相匹配的基地址。注册的基地址方案是 [https]。”

此外,生产站点设置为“需要 SSL”。那是无法改变的。

绑定配置是:

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"  />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="AjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>

<bindings>
  <customBinding>
    <binding name="custom">
      <textMessageEncoding>
        <readerQuotas maxDepth="7000000" maxStringContentLength="7000000"
            maxArrayLength="7000000" maxBytesPerRead="7000000"
            maxNameTableCharCount="7000000" />
      </textMessageEncoding>

      <httpTransport maxBufferPoolSize="7000000" maxReceivedMessageSize="7000000"
          maxBufferSize="7000000" />
    </binding>
    <binding name="customHttps">
      <textMessageEncoding>
        <readerQuotas maxDepth="7000000" maxStringContentLength="7000000"
            maxArrayLength="7000000" maxBytesPerRead="7000000"
                  maxNameTableCharCount="7000000" />
      </textMessageEncoding>

      <httpsTransport maxBufferPoolSize="7000000" maxReceivedMessageSize="7000000"
          maxBufferSize="7000000" />

    </binding>
  </customBinding>

  <webHttpBinding>
    <binding name="web"  maxBufferPoolSize="70000000"
        maxReceivedMessageSize="70000000">
      <readerQuotas maxDepth="70000000" maxStringContentLength="70000000"
          maxArrayLength="70000000" maxBytesPerRead="70000000"
          maxNameTableCharCount="70000000" />
      <security mode="None" />
    </binding>

    <binding name="webHttps" maxBufferPoolSize="70000000"
        maxReceivedMessageSize="70000000">

      <readerQuotas maxDepth="70000000" maxStringContentLength="70000000"
                maxArrayLength="70000000" maxBytesPerRead="70000000"
                maxNameTableCharCount="70000000" />

      <security mode="Transport" />
    </binding>
  </webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

有什么想法吗?

What I am trying to do is get a SINGLE WCF Service to work in the development environment which is the HTTP scheme, and, also, have the SAME service work in the production environment which is the HTTPS scheme. If I remove the two Https endpoints (those suffixed 'Https'), it works in the development enviornment; likewise, if I remove only the two Http endpoints then it works in the production environment. I would like to have all four endpoints in the web.config, if possible.

My endpoints are defined below:

<endpoint address="/Web" 
        behaviorConfiguration="AjaxBehavior"
        binding="wsHttpBinding" 
        bindingConfiguration="web" 
        name="Web"
        contract="Service" />
<endpoint address="/Custom"
        binding="customBinding" 
        bindingConfiguration="custom" 
        name="Custom"   
        contract="Service" />
<endpoint 
        address="/WebHttps" 
        behaviorConfiguration="AjaxBehavior"
        binding="wsHttpBinding" 
        bindingConfiguration="webHttps" 
        name="WebHttps"
        contract="Service" />
<endpoint address="/CustomHttps"
        binding="customBinding" 
        bindingConfiguration="customHttps" 
        name="CustomHttps" 
        contract="Service" />

Edited: I am editing my question to add the error I am getting, and the binding sections (below). Sorry for the new length of the question.

The error is:
"Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https]."

Additionally, the production site is set up to "Require SSL". That can not change.

The bindings configurations are:

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"  />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="AjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>

<bindings>
  <customBinding>
    <binding name="custom">
      <textMessageEncoding>
        <readerQuotas maxDepth="7000000" maxStringContentLength="7000000"
            maxArrayLength="7000000" maxBytesPerRead="7000000"
            maxNameTableCharCount="7000000" />
      </textMessageEncoding>

      <httpTransport maxBufferPoolSize="7000000" maxReceivedMessageSize="7000000"
          maxBufferSize="7000000" />
    </binding>
    <binding name="customHttps">
      <textMessageEncoding>
        <readerQuotas maxDepth="7000000" maxStringContentLength="7000000"
            maxArrayLength="7000000" maxBytesPerRead="7000000"
                  maxNameTableCharCount="7000000" />
      </textMessageEncoding>

      <httpsTransport maxBufferPoolSize="7000000" maxReceivedMessageSize="7000000"
          maxBufferSize="7000000" />

    </binding>
  </customBinding>

  <webHttpBinding>
    <binding name="web"  maxBufferPoolSize="70000000"
        maxReceivedMessageSize="70000000">
      <readerQuotas maxDepth="70000000" maxStringContentLength="70000000"
          maxArrayLength="70000000" maxBytesPerRead="70000000"
          maxNameTableCharCount="70000000" />
      <security mode="None" />
    </binding>

    <binding name="webHttps" maxBufferPoolSize="70000000"
        maxReceivedMessageSize="70000000">

      <readerQuotas maxDepth="70000000" maxStringContentLength="70000000"
                maxArrayLength="70000000" maxBytesPerRead="70000000"
                maxNameTableCharCount="70000000" />

      <security mode="Transport" />
    </binding>
  </webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2024-10-02 07:04:43

请按照以下步骤操作

- 1) 为服务编写两个端点,一个用于 http,另一个用于 https。

<services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>     

    </service>
  </services>

2) 在 serviceBehaviors 中启用 httpGetEnabled="True" httpsGetEnabled="true"。

<behaviors>

<serviceBehaviors>      
  <behavior name="MyServiceBehavior">
    <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>      
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp/>
  </behavior>
</endpointBehaviors>

</behaviors>

3)编写http和https的两个绑定配置。对于 http,指定安全模式=“无”;对于 https,指定安全模式=“传输”。

<bindings>
    <webHttpBinding>

      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>

      <binding name="webBindingHTTPS">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>

    </webHttpBinding>
  </bindings>

检查此链接

Follow these steps-

1) Write two endpoints for the service, one is for http and another for https.

<services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>     

    </service>
  </services>

2) Enable both httpGetEnabled="True" httpsGetEnabled="true" in serviceBehaviors.

<behaviors>

<serviceBehaviors>      
  <behavior name="MyServiceBehavior">
    <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>      
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp/>
  </behavior>
</endpointBehaviors>

</behaviors>

3) Write two bindings configurations for http and https. For http give security mode="None" and for https give mode="Transport".

<bindings>
    <webHttpBinding>

      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>

      <binding name="webBindingHTTPS">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>

    </webHttpBinding>
  </bindings>

Check this link

挖鼻大婶 2024-10-02 07:04:43

如果您使用的是 Visual Studio 2010 和 Web 应用程序项目部署,则可以使用 Web.config 转换语法将服务终结点的 bindingConfiguration 指向启用 https 的绑定配置。

对我来说,我发现我只需要替换 Web.config 文件中的两个元素。端点的 bindingConfiguration 属性和 serviceMetadata 的 httpsGetEnabled 应设置为 true。

这是默认(调试)配置中的 Web.config:

<service name="Service"
            behaviorConfiguration="DefaultBehavior">
    <endpoint name="ServiceEndpoint"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding"
                contract="IService" />
    </service>
    ...
    <behaviors>
    <serviceBehaviors>
        <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="True" />
            <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
    </serviceBehaviors>
</behaviors>

这是 Web.Release.config 转换文件

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceMetadata httpsGetEnabled="True" xdt:Transform="Replace" />
            <serviceDebug includeExceptionDetailInFaults="False" xdt:Transform="SetAttributes(includeExceptionDetailInFaults)"/>
        </behavior>
    </serviceBehaviors>
</behaviors>
<services>
    <service>
        <endpoint bindingConfiguration="SecureTransportBinding"
                    xdt:Transform="SetAttributes(bindingConfiguration)"/>
    </service>
</services>

这是我的绑定的样子,但它们非常标准。请注意上面使用的名称:

<basicHttpBinding>
    <binding name="SecureTransportBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="Transport"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
    <binding name="BasicHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="None"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
</basicHttpBinding>

以下是有关 Web.config 转换的更多信息的链接:

http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

If you're using Visual Studio 2010 and Web Application Project Deployment you can use the Web.config Transformation Syntax to point your service endpoint's bindingConfiguration to an https enabled binding configuration.

For me, I figured out I only had to replace two elements in the Web.config file. The endpoint's bindingConfiguration attribute and serviceMetadata's httpsGetEnabled should be set to true.

Here's the Web.config, in its default (debug) configuration:

<service name="Service"
            behaviorConfiguration="DefaultBehavior">
    <endpoint name="ServiceEndpoint"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding"
                contract="IService" />
    </service>
    ...
    <behaviors>
    <serviceBehaviors>
        <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="True" />
            <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
    </serviceBehaviors>
</behaviors>

Here's the Web.Release.config transformation file

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceMetadata httpsGetEnabled="True" xdt:Transform="Replace" />
            <serviceDebug includeExceptionDetailInFaults="False" xdt:Transform="SetAttributes(includeExceptionDetailInFaults)"/>
        </behavior>
    </serviceBehaviors>
</behaviors>
<services>
    <service>
        <endpoint bindingConfiguration="SecureTransportBinding"
                    xdt:Transform="SetAttributes(bindingConfiguration)"/>
    </service>
</services>

Here's what my bindings look like, but they are pretty standard. notice the names that are used above:

<basicHttpBinding>
    <binding name="SecureTransportBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="Transport"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
    <binding name="BasicHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="None"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
</basicHttpBinding>

Here's a link to more about Web.config transformations:

http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

゛清羽墨安 2024-10-02 07:04:43

出现此错误的原因有很多:

    Could not find a base address that matches scheme http for the endpoint
    with   binding WebHttpBinding. Registered base address schemes are [https].

大多数原因来自 Web.config 设置,但也可能来自 IIS。我遇到了同样的问题,如果您同时使用 http 和 https 绑定来保护端点,则必须为您在 IIS->Site->Bindings 中创建的网站创建 http 和 https 绑定,否则您将收到此错误。

There are lots of reasons you can get the error:

    Could not find a base address that matches scheme http for the endpoint
    with   binding WebHttpBinding. Registered base address schemes are [https].

Most of the reasons are from the Web.config settings, but it could be from IIS. I had the same problems, if you defended Endpoints with both http and https bindings, you have to create http and https binding for the website you created in IIS->Site->Bindings, otherwise you will get this error.

巷雨优美回忆 2024-10-02 07:04:43

这是您可以从配置设置绑定的原因之一:能够在不同的环境中使用不同的设置。

最简单的解决方案是使用 makecert为您的开发环境创建测试证书,并在开发和生产计算机上使用 HTTPS。

另一个解决方案是创建安装包 (msi) 并让管理员在安装过程中更改 enpoint 设置。

编辑:

您在两台计算机上拥有所有 4 个端点的要求是可以实现的,但在这种情况下,您的服务也将在生产中公开在 HTTP 上,并且拥有该服务的 WSDL 的每个人都会知道这一点。

This is one reason why you can setup binding from configuration: To be able to use different settings in different environments.

The easiest solution for you is to use makecert to create test certificate for your development environment and use HTTPS on both development and production machine.

Another solution is to create installation package (msi) and let admin change enpoint setting during installation.

Edit:

Your requirement of having all 4 endpoints on both machines is achievable but in that case your service will be also exposed on HTTP in production and everybody who will have WSDL for the service will know about that.

笙痞 2024-10-02 07:04:43

默认接收超时为10分钟,因此WCF客户端在空闲时间超过该限制后将断开连接。如果需要保持连接,我该怎么办?

解决方案#1:

服务器提供一个虚拟操作供客户端定期调用,以使其不空闲。

解决方案#2:

启用可靠会话并将客户端和服务器中的 receiveTimeout 和 inactivityTimeout 设置为“无限”。
配置片段可能如下所示:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding" receiveTimeout="infinite">
        <reliableSession inactivityTimeout="infinite" enabled="true" />
      </binding>
    </wsHttpBinding>
  </bindings>
  <services>
  ...
  </services>
  ...
</system.serviceModel>

The default receive timeout is 10 minutes, so WCF client will be disconnected after the idle time exceeded that limitation. What can I do if the connection needs to be kept alive?

Solution #1:

Server provides a dummy operation for client calls it regularly to let it not idle.

Solution #2:

Enable reliableSession and set receiveTimeout and inactivityTimeout to “infinite” in both the client and server.
The configuration snippet may like the following:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding" receiveTimeout="infinite">
        <reliableSession inactivityTimeout="infinite" enabled="true" />
      </binding>
    </wsHttpBinding>
  </bindings>
  <services>
  ...
  </services>
  ...
</system.serviceModel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文