简体   繁体   English

仅实现Json和https的Wcf服务

[英]Wcf service implementing Json and https only

I am implementing a WCF service that needs to work with Json objects. 我正在实现需要与Json对象一起使用的WCF服务。 This is working so far. 到目前为止,这是可行的。 Now, i want the service to accept https only, so no http. 现在,我希望该服务仅接受https,因此不接受http。

For both requirements i have found a couple of samples that i have tested and that seem to work. 对于这两个要求,我都找到了一些经过测试且似乎可以正常工作的示例。 However i am unable to get both requirements integrated into one config file. 但是我无法将两个要求都集成到一个配置文件中。

This is my configuration for the service: 这是我对服务的配置:

<?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>

Try adding a binding configuration for <webHttpBinding> and setting the Security mode to transport. 尝试为<webHttpBinding>添加绑定配置,并将安全模式设置为传输。 You'll need to explicitly set the binding configuration to your endpoint. 您需要将绑定配置显式设置为端点。

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

Then in your endpoint reference the above binding configuration via the binding Configuration attribute: 然后在您的端点中,通过binding Configuration属性引用上述绑定配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM