WCF:如何“添加服务引用”到SSL服务(我收到错误)

时间:2011-05-02 15:04:57

标签: wcf web-services ssl binding service

FooService.svc.cs:

[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/')。

无论是直接访问网址还是从Visual Studio中转到Add Service Reference...,都会发生这种情况。


此外,如果我将[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]更改为[ServiceContract],则可以正常使用。

1 个答案:

答案 0 :(得分:4)

WCF 4中HTTP传输的默认绑定是basicHttpBinding。当您将服务合同保护级别设置为加密和签名时,绑定还必须支持消息级别安全性。由于basicHttpBinding不支持消息级安全性,因此您需要手动将服务配置为wsHttpBinding或更改协议映射,如下所示:

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