[英]How to connect to a WCF service with Custom Binding from unmanaged C++
我需要从本机C ++应用程序连接到WCF服务。 我尝试了下面的链接,它与wsHttpBinding
一起使用。
但是,我需要使用自定义绑定连接到WCF服务。 这就是我的应用程序配置文件中自定义绑定的代码的样子
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="ResourceCenterEndpoint5">
<mtomMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
messageVersion="Default" maxBufferSize="65536" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</mtomMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Ntlm"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536"
proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" />
</binding>
</customBinding>
</binding>
</bindings>
<client>
<endpoint address="http://usaabcxyzas1.na.abc.com/Build15/ReserSVC/Resource.svc"
binding="customBinding" bindingConfiguration="ResourceCenterEndpoint5"
contract="ServiceReference2.ResourceCenterServiceContract"
name="ResourceCenterEndpoint5">
<identity>
<userPrincipalName value="devlts_srv@na.abc.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
我有一个桥DLL,它是一个托管C ++ DLL。 托管C ++ DLL将C#Client连接到本机应用程序。 但是,由于Web服务使用自定义绑定,因此无法从托管C ++ DLL连接到Web Service。 我得到的错误是:
使用客户端身份验证方案“匿名”未授权http请求。 从服务器收到的身份验证标头是“Negotiate,NTLM”
这就是我尝试从manged C ++ dll连接到Webservice的方法:
Binding^ binding = gcnew BasicHttpBinding();
EndpointAddress^ address = gcnew EndpointAddress(gcnew String("http://usaabcxyzas1.na.abc.com/Build15/ReserSVC/Resource.svc"));
HelloServiceClient::ServiceReference2::ResourceCenterServiceContractClient^ client = gcnew HelloServiceClient::ServiceReference2::ResourceCenterServiceContractClient(binding, address);
client->DoWork();
所以基本上我需要使用自定义绑定将托管C ++ dll连接到WCF服务。 我怎样才能做到这一点?
您正尝试在客户端代码中使用BasicHttpBinding。
在配置文件中,您需要NTLM:
authenticationScheme="Ntlm"
该错误指向您在服务的配置文件中拥有的内容。
*The http request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the sever was 'Negotiate,NTLM'*
你也看起来像是试图入侵
proxyAuthenticationScheme="Anonymous"
所以它归结为您的安全要求。 如果您希望服务没有安全性,只需取出NTLM参考。 如果您需要安全性,则需要在绑定定义中使用安全性部分,例如:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
请看这篇文章了解更多
我不认为你想要自定义绑定,因为你想要自定义开箱即用的绑定。 除非您打算在TCP / IP之外创建一个专有的通信协议。
对于安全性问题,您可能需要查看设置Security.Mode属性以及分配正确的传输和/或消息安全性属性。 例如。 使用证书或密码质询,加密,加密和签名等。
您还需要在客户端执行相同的操作。 绑定应该与服务器端的绑定几乎相同。
如果你不喜欢basicHttp,总会有TCP,MSMQ,名为管道等等。 你应该查找它以获得完整列表。
您是否尝试使用SvcUtil从Web服务生成WSDL。 一旦你有了客户端代理和配置,它将具有客户端连接到服务所需的配置。
您还提到要使用CustomBinding进行连接,但在使用BasicHttpBinding的客户端代码中。
您需要设置代码的另一件事是AuthenticationScheme,因为服务器期望方案NTLM并且您的客户端代码没有设置它,并且默认情况下它是匿名的。
这看起来像是一个身份验证错误,所以你需要oto给自己更多的权限或找出谁得到它,但例如提供用户名和密码。
我维护使用Wcf服务的本机c ++应用程序。 我会建议使用优秀的gSoap库,而不是处理原始连接和XML。 这将从服务中获取WSDL并生成用于操作的代码。 通过使用http://code.google.com/p/gsoapwininet/插件,所有通信都通过IE进行定向,这意味着将自动支持所有各种Windows身份验证方法,这将解决您的具体问题。
您可以创建customBinding并传入所需的绑定配置名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.