2013-02-17 88 views
1

我正在使用WCF服务的项目。我已经构建了该服务,配置了web.config文件,并将其部署在IIS 7服务器上。该服务通过HTTPS加密(在我的开发机器上,我自己创建了证书)。 在Visual Studio 2010中创建ServiceReference时,一切都很好,它创建客户端并且工作正常。通过HTTPS以编程方式连接到WCF服务

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

The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via

用于Web的代码。配置为:(我希望没有什么错吧)

<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(); 

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

代码看来我错在这里做什么,该工序会提高我提到的异常。我还需要做更多的工作,但我没有弄明白。

在此先感谢您提供给我的任何建议。

+0

你是如何托管你的服务?该交通工具是否启用了https? – stephenl 2013-02-17 23:37:09

回答

3

我还没有测试过这个,但我相信你需要在创建工厂之前设置绑定的安全模式。 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"); 

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

欲了解更多信息,请检查以下MSDN文章:

How to: Set the Security Mode

WSHttpBinding Constructor

+0

我将重写代码并对其进行测试。 感谢您的回答,我将深入研究这一点。 – 2013-02-18 07:36:44

+0

谢谢Tim的回答,但不幸的是,它会产生以下错误:无法为权限'localhost'的SSL/TLS安全通道建立信任关系。我认为“localhost”是本地生成的证书。 – 2013-02-18 18:54:33

+0

我没有做过很多与证书,但检查[这个答案](http://stackoverflow.com/a/8854765/745969)了。 – Tim 2013-02-18 19:29:57

1

好了,解决了自创建的证书的问题。 我在Viosual Studio 2010中更改了编程连接和服务参考的端点地址。

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

现在

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

我已经改变了ADRESS从本地主机到PC “EU-PC” 的名字。它与证书颁发的域名有关。 使用本地主机或127.0.0.1只能用于一种方法或另一种方法。

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

相关问题