为http和&amp ;;配置带有路由(global.asax)的WCF 4。 https端点

时间:2010-06-16 19:57:29

标签: wcf web-services ssl

我仍然是wcf的新手,并且在.net中一般都不太了解。我有一个WCF 4 Web服务,它使用global.asax路由方法,并使用标准端点方法非常简化web.config。此wcf服务作为应用程序运行,目前在iis 7.5上使用默认网站。如果可能的话,我需要它支持http和https接口。如果那太复杂,那么只有https。如何最好地维持当前的方法呢?

global.asax.cs和web.config文件的内容非常基本:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        // Edit the base address of Service1 by replacing the "ippay" string below
        RouteTable.Routes.Add(new ServiceRoute("myservice", new WebServiceHostFactory(),   
typeof(myservice)));     
    }
}


<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
     <standardEndpoint name="" helpEnabled="true"    contentTypeMapper="myservice.Util.RawMapper,myservice">
        </standardEndpoint>
  </webHttpEndpoint>
</standardEndpoints>

2 个答案:

答案 0 :(得分:9)

我找到了答案:您只需将此代码段放在serviceModel标记中的web.config中:

<bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>

感谢此帖:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/1dd991a1-e32f-4035-a406-994729858b40

我的完整web.config是这样的:

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

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

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>   </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
          <security mode="Transport" >

          </security>
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>   </system.serviceModel> </configuration>

答案 1 :(得分:3)

如果您不同时需要HTTP和HTTPS,则上述工作正常。在我的情况下,我想要两者,因为一些服务需要SSL(身份验证)而其他服务不需要,因为他们提供的信息不敏感。如果请求不是来自https方案,则身份验证服务实现会自己进行验证并拒绝回答。

如果要在同一端点上同时设置HTTP和HTTPS,则波纹管配置有效。

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport" />
        </binding>
        <binding name="UnsecureBinding"></binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" bindingConfiguration="UnsecureBinding" />
    </protocolMapping>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>