2011-05-02 100 views
3

FooService.svc.cs:WCF:如何做 「添加服務引用」 到SSL服務(遇到錯誤)

[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] 
public interface IFooBar 
{ 
    [OperationContract()] 
    int Add(int num1, int num2); 
} 

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class FooService : IFooBar 
{ 
    public int Add(int num1, int num2) 
    { 
     return num1 + num2; 
    } 
} 

Web.config文件:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 

    <system.web> 
    <customErrors mode="Off"/> 
    <globalization culture="en-US" uiCulture="en-US" /> 
    <compilation defaultLanguage="c#" targetFramework="4.0" /> 
    </system.web> 

    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
     <defaultDocument> 
      <files> 
       <add value="index.aspx" /> 
      </files> 
     </defaultDocument> 
    </system.webServer> 

    <system.diagnostics> 
    <sources> 
     <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing"> 
     <listeners> 
      <add name="ServiceModelTraceListener" /> 
     </listeners> 
     </source> 

     <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing"> 
     <listeners> 
      <add name="ServiceModelTraceListener" /> 
     </listeners> 
     </source> 
     <source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing"> 
     <listeners> 
      <add name="ServiceModelTraceListener" /> 
     </listeners> 
     </source> 
    </sources> 
    <sharedListeners> 
     <add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp" /> 
    </sharedListeners> 
    </system.diagnostics> 

</configuration> 

我發佈了文件到我的服務器(它有一個SSL證書),但是當我去到網頁:https://localhost:443/FooService.svc我得到以下錯誤...

請求消息必須受到保護。 這是 合同 ('IFooBar','http://tempuri.org/')的操作所要求的。 保護必須由 綁定 ('BasicHttpBinding','http://tempuri.org/')提供。

無論我直接轉到url還是從Visual Studio中轉到Add Service Reference...,都會發生這種情況。


另外,如果我改變[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)][ServiceContract]它工作正常。

回答

4

WCF 4中HTTP傳輸的默認綁定是basicHttpBinding。當您將服務合同保護級別設置爲加密並簽名時,綁定還必須支持消息級安全性。由於basicHttpBinding不支持消息級安全性,因此需要手動配置服務以wsHttpBinding或更改協議映射,如下所示:

<system.serviceModel> 
    <protocolMapping> 
     <remove scheme="http" /> 
     <add scheme="http" binding="wsHttpBinding" bindingConfiguration="" /> 
    </protocolMapping> 
... snip ... 
</system.serviceModel> 
+0

我愛你man! :) <3 – michael 2011-05-02 15:52:58