WCF: How to do "Add Service Reference" to an SSL Service (I'm getting errors)
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开发者_Python百科>
<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>
I published the Files to my server (which has a ssl certificate on it), but when I go to the webpage: https://localhost:443/FooService.svc I get the following error...
The request message must be protected. This is required by an operation of the contract ('IFooBar','http://tempuri.org/'). The protection must be provided by the binding ('BasicHttpBinding','http://tempuri.org/').
This happens whether I go to the url directly or if I go to Add Service Reference...
from within Visual Studio.
Also, if I change the [ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
to [ServiceContract]
it works fine.
The default binding for the HTTP transport in WCF 4 is basicHttpBinding. When you set the service contract protection level to encrypt and sign then the binding must also support message level security. Since basicHttpBinding does not support message level security, you need to configure the service manually to wsHttpBinding or change the protocol map as follows:
<system.serviceModel>
<protocolMapping>
<remove scheme="http" />
<add scheme="http" binding="wsHttpBinding" bindingConfiguration="" />
</protocolMapping>
... snip ...
</system.serviceModel>
精彩评论