如何设置WCF服务以在HTTP上运行

时间:2016-12-28 11:02:06

标签: c# wcf iis https

我创建了一个wcf服务,并希望以安全的方式运行(https)

的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>    
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1" behaviorConfiguration ="MyServiceTypeBehaviors">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8734/Service1/" />
          </baseAddresses>
        </host>

        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">

          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>            
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">             
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>             
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我尝试将基地址从http更改为https。 我已经从IIS创建了自签名证书,并将其与https绑定,端口为8374

1 个答案:

答案 0 :(得分:0)

尝试使用该配置。我已将BasicHttpBinding的绑定更改为BasicHttpsBinding,并添加了“serviceCredentials”部分,您必须从计算机上的某个商店中选择证书。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
    <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" />
    </system.web>    
    <system.serviceModel>
        <services>
            <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
                <endpoint address="" binding="basicHttpsBinding" contract="WcfServiceLibrary1.IService1"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyServiceTypeBehaviors">
                    <serviceCredentials>
                        <serviceCertificate findValue="" storeLocation="LocalMachine" x509FindType="FindByThumbprint" storeName="My"/>
                    </serviceCredentials>             
                    <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>             
                    <serviceDebug includeExceptionDetailInFaults="False" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https"/>
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>
</configuration>

P.S。如果Web服务托管在IIS中,则您不需要baseAddress和port。端口绑定从IIS管理器

完成