使用Http和Https的REST服务WebHttpBinding

时间:2017-04-02 16:09:12

标签: rest wcf http https

我有一个REST服务。此服务需要使用http和https。 我试图在我的web.config文件中添加两个端点。但是当我尝试通过http:

浏览我的服务时,我收到此错误
  

无法找到与方案https匹配的基地址   绑定WebHttpBinding的端点。注册的基地址方案   是[http]。

当我尝试浏览https时出现此错误:

  

找不到与绑定WebHttpBinding的端点的scheme http匹配的基址。注册的基地址方案是[https]。

如果我从配置文件中删除一个端点,http和https服务都可以正常工作。 我查看了这个链接:WebHttpBinding with Http and Https 但是当我从配置文件中删除端点时,http和https服务在Web浏览器上运行时没有任何错误。但是当我尝试在这个服务中调用我的一个方法(通过休息客户端工具)时,它会得到:

  

500内部服务器错误。

如何通过http和https运行此服务而不会出现任何错误?

我的配置文件是这样的:

 <system.serviceModel>
<protocolMapping>
  <add scheme="http" binding="webHttpBinding" bindingConfiguration="webHttpBinding"/>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="webHttpsBinding"/>
</protocolMapping>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBinding">
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" />
      </security>
    </binding>
    <binding name="webHttpsBinding">
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service name="MyProject.MyService" behaviorConfiguration="serviceBehavior">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="webHttpBinding" contract="MyProject.IMyService" />
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="webHttpsBinding" contract="MyProject.IMyService" />
    <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />-->
    <!--<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>-->
  </service>
</services> 
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceAuthorization serviceAuthorizationManagerType="MyProject.RestAuthorizationManager, MyProject"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>    

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

1 个答案:

答案 0 :(得分:0)

这里有一个示例web.config,以便同时支持httphttps。我希望能解决你的问题:

<?xml version="1.0" encoding="utf-8"?>
<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>
    <bindings>
      <basicHttpBinding>
        <binding name="SoapBinding" />
      </basicHttpBinding>
      <basicHttpsBinding>
        <binding name="SecureSoapBinding" />
      </basicHttpsBinding>

      <webHttpBinding>
        <binding name="RestBinding" />
        <binding name="SecureRestBinding">
          <security mode="Transport" />
        </binding>
      </webHttpBinding>

      <mexHttpBinding>
        <binding name="MexBinding" />
      </mexHttpBinding>
      <mexHttpsBinding>
        <binding name="SecureMexBinding" />
      </mexHttpsBinding>
    </bindings>

    <client />

    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Interface.Core">
        <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="SoapBinding" name="Soap" contract="Interface.ICore" />
        <endpoint address="soap" binding="basicHttpsBinding" bindingConfiguration="SecureSoapBinding" name="SecureSoap" contract="Interface.ICore" />
        <endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Rest" contract="Interface.ICore" />
        <endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="SecureRestBinding" name="SecureRest" contract="Interface.ICore" />
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="MexBinding" name="Mex" contract="IMetadataExchange" />
        <endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration="SecureMexBinding" name="SecureMex" contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="Web">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </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>