WCF 在 http 和 https 上都具有自定义绑定

问题描述 投票:0回答:2

我有一个带有自定义绑定的 WCF 服务,它在 http 或 https 上运行良好。但我完全不知道如何使其在 http 和 https 上都可用?

还有可能吗?

这是我在 web.config 中的配置。

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<behaviors>
    <serviceBehaviors>
        <behavior name="">
            <serviceMetadata httpsGetEnabled="true" />                    
            <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>         
    </serviceBehaviors>
</behaviors>
<bindings>
    <customBinding>                     
        <binding name="customBinding0">
          <binaryMessageEncoding />
          <httpsTransport />
        </binding>
    </customBinding>
</bindings>
<services>
    <service name="MyWCFService">                           
        <endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
            contract="MyWCFService" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
    </service>      
</services>

谢谢

wcf http https custom-binding
2个回答
12
投票

您需要有两个端点,一个用于 HTTP,另一个用于 HTTPS。应该可以正常工作。

<bindings>
    <customBinding>
        <binding name="customBindingHTTP">
            <binaryMessageEncoding />
            <httpTransport />
        </binding>
        <binding name="customBindingHTTPS">
            <binaryMessageEncoding />
            <httpsTransport />
        </binding>
    </customBinding>
</bindings>
<services>
    <service name="MyWCFService">
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="customBindingHTTP"
                  contract="MyWCFService" />
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="customBindingHTTPS"
                  contract="MyWCFService" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
    </service> 
</services> 

0
投票

我在代码中做了我的:

var basicbinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new CustomTextMessageBindingElement("iso-8859-1"));

var bindingElements = basicbinding.CreateBindingElements().ToList();
var encodingElement = bindingElements.OfType<MessageEncodingBindingElement>().FirstOrDefault();
if (encodingElement != null)
{
    bindingElements.Remove(encodingElement);
}
binding.Elements.AddRange(bindingElements.ToArray());
© www.soinside.com 2019 - 2024. All rights reserved.