【问题标题】:connect programmatically to a WCF service through HTTPS通过 HTTPS 以编程方式连接到 WCF 服务
【发布时间】:2013-02-02 06:03:18
【问题描述】:

我正在开发一个使用 WCF 服务的项目。我已经构建了服务,配置了 web.config 文件,并将其部署在 IIS 7 服务器上。该服务是通过 HTTPS 访问的(在我的开发机器上,我已经自行创建了证书)。 在 Visual Studio 2010 中创建 ServiceReference 时一切正常,它创建了客户端并且工作正常。

我需要以编程方式创建客户端(需要一点灵活性),所以当我尝试“手动”连接时,它给了我这样的错误:

提供的 URI 方案“https”无效;预期的“http”。 参数名称:via

web.config的代码是:(希望没有错)

<system.serviceModel>    
    <services>           
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="WcfService1.IService1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <serviceMetadata httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>


    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

  </system.serviceModel>

我写的访问WCF服务的过程是:

 void proc()
 {
        string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";
        WSHttpBinding bind= new WSHttpBinding();

        EndpointAddress ea = new EndpointAddress(ADRESASSL);
        var myChannelFactory = new ChannelFactory<IService1>(bind, ea);

        IService1 client = null;
        try
        {
            client = myChannelFactory.CreateChannel();
            client.RunMethod1();
            client.Close();                
            //((ICommunicationObject)client).Close();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            if (client != null)
                client.Close();
        }
    }

IService1 的代码

[ServiceContract]
public interface IService1 : IClientChannel
{
    [OperationContract]
    int RunMethod1();

 //....................................
}

看来我在这里做错了,程序引发了我提到的异常。我必须做更多的事情才能工作,但我没有弄清楚。

提前感谢您能给我的任何建议。

【问题讨论】:

  • 您如何托管您的服务?该传输是否启用了 https?

标签: wcf iis service https connect


【解决方案1】:

我没有测试过这个,但是我相信你需要在创建工厂之前为绑定设置安全模式。 WSHttpBinding 的默认安全模式是SecurityMode.Message,而您需要SecurityMode.Transport。

您可以通过以下三种方式之一解决此问题。

首先,您可以使用WSHttpBinding构造函数的重载版本来指定安全模式,如下所示:

WSHttpBinding bind= new WSHttpBinding(SecurityMode.Transport);
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

其次,您可以使用无参数构造函数并指定安全模式(和客户端凭据类型),如下所示:

WSHttpBinding bind= new WSHttpBinding();
bind.Security.Mode = SecurityMode.Transport;
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

第三,您可以在客户端配置中放置一个binding 配置部分,并在构造函数中引用该部分,如下所示:

WSHttpBinding bind = new WSHttpBinding("TransportSecurity");

第三个示例假定客户端配置文件中有一个名为“TransportSecurity”的wsHttpBinding 部分。

有关详细信息,请查看以下 MSDN 文章:

How to: Set the Security Mode

WSHttpBinding Constructor

【讨论】:

  • 我会重写代码并测试它。感谢您的回答,我将深入研究。
  • 感谢蒂姆的回答,但不幸的是它会产生以下错误:无法为具有权限“localhost”的 SSL/TLS 安全通道建立信任关系。我认为“localhost”是本地生成的证书。
  • 我在证书方面做得不多,但请查看this answer。
  • 是的,我认为问题出在证书上。这是一个自行生成的证书(目前还不能访问由 certsign 等公司颁发的证书)
  • 您是否尝试过导出证书并将其也安装在客户端上?
【解决方案2】:

好吧,解决了自创证书的问题。 我已经更改了 Viosual Studio 2010 中编程连接和服务引用的端点地址。

string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";

现在是

string ADRESASSL = "https://eu-pc/ServiciuSSLwsBind/Service1.svc";

我已将地址从 localhost 更改为 pc 名称“eu-pc”。它与颁发证书的域有关。 使用 localhost 或 127.0.0.1 仅适用于一种方法。

希望这对可能遇到此问题的其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 2013-01-14
    • 2011-02-25
    • 1970-01-01
    • 2011-07-20
    • 2015-07-30
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多