2010-06-16 47 views
7

我仍然是一個wcf的新手,並且在.net中一般不太瞭解。我有一個WCF 4 Web服務,它使用global.asax路由方法和使用標準端點方法的非常簡化的web.config。該wcf服務目前作爲iis 7.5上的默認網站運行。如果可能,我需要它支持http和https界面。如果這太複雜,那麼只有https。如何最好地處理維護當前的方法?爲http和https端點配置路由(global.asax)的WCF 4

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

+0

您是否曾經找到答案? – 2010-08-20 01:46:15

回答

9

我找到了答案:你只需要把這個片段在你的web.config在serviceModel標籤:

<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

我不認爲你需要標準端點中的第二個「」部分。不應該引起任何問題。 – dan 2011-10-02 23:50:35

+0

非常感謝,解決了這個問題 – 2011-11-07 15:02:50

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>