同一主机/端口上的 Wcf HTTP 和 HTTPS [英] Wcf HTTP and HTTPS on the same host/port

查看:27
本文介绍了同一主机/端口上的 Wcf HTTP 和 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我知道如何为 http 或 https 创建自托管 wcf,但不能同时创建.

I know how to create a self hosted wcf for http or https, but not at the same time.

我想要这 2 个网址的 wcf :

I would like a wcf for this 2 urls :

  1. https://127.0.0.1:13070/ProxySips/
  2. http://127.0.0.1:13070/ProxySips/

目前我有 https 的配置(带有证书:makecert + netsh)并且它工作正常:

At the moment I have the configuration for https (with a certificate: makecert + netsh) and it works fine:

app.config

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="ProxySips_Wcf.Sips" behaviorConfiguration="ProxySips_Wcf.ProxySipsBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="https://127.0.0.1:13070/ProxySips/"   />
      </baseAddresses>
    </host>
    <endpoint address="" binding="basicHttpBinding"
              bindingConfiguration="basicHttp"
              contract="ProxySips_Wcf.ISips"/>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ProxySips_Wcf.ProxySipsBehavior">
      <serviceMetadata  httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>

主持人

var serviceType = typeof(ProxySips_Wcf.Sips);
var host = new ServiceHost(serviceType);
host.Open();

可以帮我设置相同地址的http版本吗?

Can help me set up the http version for the same address?

非常感谢

推荐答案

您可以通过使用 多个端点.通过多个端点,您可以通过多种协议(在您的情况下为 HTTP 和 HTTPS)支持相同的服务.

You can do this through the use of Multiple Endpoints. With multiple endpoints you can support the same service over multiple protocols (HTTP and HTTPS in your case).

因此您需要添加以下ServiceBehavior:

<behavior name="MyUnsecureServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
     <dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>

然后是下面的Binding:

<basicHttpBinding>
  <binding name= MyUnsecureBindingConfig"
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647" 
                 messageEncoding="Text">
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
                   maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                   maxNameTableCharCount="2147483647" />
     <security mode="None">
         <transport clientCredentialType="None" />
         <message establishSecurityContext="false" />
     </security>
</basicHttpBinding>

最后是以下Address配置:

<service behaviorConfiguration="MyUnsecureServiceBehavior"
                         name="MyUnsecureService">
   <endpoint address="http://127.0.0.1:13070/ProxySips/"
             binding="basicHttpBinding"
             contract="ProxySips_Wcf.ISips"
             bindingConfiguration="MyUnsecureBindingConfig" />
   <endpoint address="mex"
             binding="mexHttpBinding"
             contract="IMetadataExchange"/>
</service>

更新:

在您的 WCF 主机 应用程序上,您需要指定要监听的新 URI.您可以像这样设置您的主机:

On your WCF Host application, you will need to specify the new URI to listen at. You can set up your host similar to this:

var httpsAddress = new Uri("https:// 127.0.0.1:13070/ProxySips/");
var httpAddress = new Uri("http://127.0.0.1:13070/ProxySips/");
var host = new ServiceHost(typeof(ProxySips_Wcf.Sips), 
                            new Uri[] { httpsAddress, httpAddress });
host.Open();

这篇关于同一主机/端口上的 Wcf HTTP 和 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆