Wcf服务仅实现Json和https

时间:2017-01-23 15:23:25

标签: c# json wcf ssl https

我正在实现一个需要使用Json对象的WCF服务。到目前为止这是有效的。现在,我希望服务只接受https,所以没有http。

对于这两个要求,我发现了一些我已经测试过并且似乎有效的样本。但是,我无法将这两个要求集成到一个配置文件中。

这是我对服务的配置:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="SMApi.SApi">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="" contract="SMApi.ISApi" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <!-- / -->
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <!-- / -->
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
      <!-- / -->
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <!-- / -->
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="false"/>
  </system.webServer>

</configuration>

1 个答案:

答案 0 :(得分:0)

尝试为<webHttpBinding>添加绑定配置并将安全模式设置为传输。您需要明确地将绑定配置设置为您的端点。

<bindings>
  <webHttpBinding name="SMApiWebhttpBinding">
    <security mode="Transport" />
  </webHttpBinding>
</bindings>

然后在您的端点中通过binding Configuration属性引用上述绑定配置:

<service behaviorConfiguration="serviceBehavior" 
         name="SMApi.SApi">
  <endpoint address="" behaviorConfiguration="web" 
            binding="webHttpBinding"
            bindingConfiguration="SMApiWebHttpBinding"
            contract="SMApi.ISApi" />