WCF中的传输安全性

时间:2011-09-25 08:40:17

标签: c# wcf

我的解决方案中有2个控制台应用程序。这是我的WCF服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TransportSecTaulty
{
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        void CallMe(string message);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TransportSecTaulty
{
    public class TestService : ITestService
    {
        public void CallMe(string message)
        {
            Console.BackgroundColor = ConsoleColor.Green;
            Console.Write(message);
            Console.BackgroundColor = ConsoleColor.Black;
        }
    }
}

这是托管我的WCF服务的应用程序的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata  httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="TransportSecTaulty.TestService">
                <endpoint address="" binding="basicHttpBinding" bindingConfiguration="serviceConfig" contract="TransportSecTaulty.ITestService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="https://localhost:8734/Design_Time_Addresses/TransportSecTaulty/TestService/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
      <bindings>
        <basicHttpBinding>
          <binding name="serviceConfig">
            <security mode="Transport">
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
    </system.serviceModel>
</configuration>

我的服务正确启动没有任何问题。但是,当我尝试在我的客户端添加服务引用时,我收到此错误:

There was an error downloading 'https://localhost:8734/Design_Time_Addresses/TransportSecTaulty/TestService/'.
The underlying connection was closed: An unexpected error occurred on a send.
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
An existing connection was forcibly closed by the remote host
Metadata contains a reference that cannot be resolved: 'https://localhost:8734/Design_Time_Addresses/TransportSecTaulty/TestService/'.
An error occurred while making the HTTP request to https://localhost:8734/Design_Time_Addresses/TransportSecTaulty/TestService/. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
The underlying connection was closed: An unexpected error occurred on a send.
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
An existing connection was forcibly closed by the remote host
If the service is defined in the current solution, try building the solution and adding the service reference again.

我无法在浏览器中浏览我的服务器。但是,同样的事情在http上完美运行。问题只出现在https。

知道什么是错的吗?

3 个答案:

答案 0 :(得分:1)

你有

<endpoint address="" 
          binding="basicHttpBinding" 
          bindingConfiguration="serviceConfig" 
          contract="TransportSecTaulty.ITestService">

但我希望你想要binding="basicHttpsBinding"

答案 1 :(得分:1)

您正尝试使用https保护元数据,而服务本身是纯http。这真的是你想要的吗?

MSDN有一篇关于保护元数据的文章。它还可以保护服务本身(这是合理的 - 为什么安全元数据而不是服务?)

答案 2 :(得分:-1)

你应该使用WSHttpBinding ......