2011-11-18 132 views
4

我有下面的结合WCF服务:消费WCF服务与basicHttpBinding的和Windows身份验证

<basicHttpBinding>   
    <binding name="bind" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> 
    <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> 
     <message algorithmSuite="Default" clientCredentialType="UserName"/> 
    </security> 
    </binding> 
</basicHttpBinding> 

我与这个端点动态地消费它在代码:

BasicHttpBinding binding = new BasicHttpBinding(); 
binding.Name = "bind"; 
binding.MaxBufferSize = int.MaxValue; 
binding.MaxReceivedMessageSize = int.MaxValue; 
ServiceEndpoint endPoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ImyContract)), binding, new EndpointAddress(endPointAddress)); 

endPoint.Name = name; 
ChannelFactory<ImyContract> channelFactory = new ChannelFactory<ImyContract>(endPoint); 

当我浏览该服务的一切正如预期的那样,但是当通过应用程序连接时,我得到:

内容键入text/xml; charset = utf-8不支持服务 http://localhost/SynchronizationService/SyncService.svc/DataBinding。 客户端和服务绑定可能不匹配。

我理解,因为basicHttpBinding的使用SOAP 1.1这个错误往往是造成和的wsHttpBinding使用1.2,但是,我没有在服务器上使用SSL,并有使用流另一个端点,所以开关是不是我的选择。我做错了什么是阻止我正确使用服务?

UPDATE:

另外,我不使用客户端上的默认basicHttpBinding的,但设置安全性以匹配服务器期待。我用这个方法设置安全:

private static BasicHttpSecurity SetEndpointSecurity() 
{ 
    BasicHttpSecurity security = new BasicHttpSecurity(); 
    HttpTransportSecurity transport = new HttpTransportSecurity(); 
    BasicHttpMessageSecurity message = new BasicHttpMessageSecurity(); 

    security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 

    transport.ClientCredentialType = HttpClientCredentialType.Windows; 
    transport.ProxyCredentialType = HttpProxyCredentialType.None; 
    transport.Realm = ""; 

    message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; 
    message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default; 

    security.Transport = transport; 
    security.Message = message; 

    return security; 
} 
+1

尝试用svcutil创建一个代理并查看它在那里的完成情况。哦,你还在使用元数据交换端点吗?因为如果你不小心使用了那个uri,那也经常会导致这个问题。 – albertjan

+0

删除我的答案,因为我不认为这是正确的看到BasicHTTPBinding更多(有助于知道我将要尝试的东西)。以下链接(和注释)似乎是一个有用的开始位置:http://rickgaribay.net/archive/2007/04/04/recipe-wcf-basichttpbinding-with-windows-authentication.aspx –

+0

服务器版本绝对使用上面指定的绑定?在.NET4中,我发现直到我得到我的命名空间和类似这样的正确,它提供的默认绑定(wsHTTPBinding)仍然有效,并且我的配置没有激活。如@ the_ajp的评论中所述使用svcutil可能有助于检查。 –

回答

1

感谢the_ajp的建议,让.NET生成代理类,然后重新实现它的端点的代码,因为这使我对正确的解决方案!

事实证明,我试图解决错误“客户端和服务绑定可能不匹配”,导致我使客户端和服务器端点绑定TOO类似。关键是在客户端端点上明确指定TextEncoding为UTF-8,并在客户端上使用wsHttpBinding,即使我连接到服务器上的basicHttpBinding。这将消息始终保存在SOAP 1.2中,而不需要在服务器上设置SSL安全性。

感谢您的帮助,找到合适的解决方案家伙!

克里斯C,希望这可以用于你在你的工作下一个:)你!

+0

谢谢 - 我在传输安全(通道的SSL/HTTPS)和TransportCredentialOnly之间弄错了,传输凭证未加密(因此非常不安全) –

+0

出于兴趣 - 对于客户端上的wsHTTPBinding,设置了什么样的安全模式这工作没有SSL? –

+0

@KrisC,在客户端将其设置为TransportCredentialOnly,在传输层上使用Windows的客户端凭证类型,在消息层上使用UserName。 –