如何从非托管 C++ 使用自定义绑定连接到 WCF 服务

发布于 2024-11-02 11:59:22 字数 2845 浏览 1 评论 0原文

我需要从本机 C++ 应用程序连接到 WCF 服务。我尝试了下面的链接,它适用于 wsHttpBinding

为非托管 C++ 客户端创建 WCF 服务

但是,我需要连接到具有自定义绑定的 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="[email protected]" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

我有一个桥接 DLL,它是一个托管 C++ DLL。托管 C++ DLL 将 C# 客户端连接到本机应用程序。但是,我无法从托管 C++ DLL 连接到 Web 服务,因为 Web 服务正在使用自定义绑定。我得到的错误是:

http 请求未经客户端身份验证方案“匿名”的授权。从服务器收到的身份验证标头是“Negotiate,NTLM”

这就是我尝试从托管 C++ dll 连接到 Web 服务的方式:

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 服务。我该怎么做?

I need to connect to a WCF service from a native C++ application. I tried the link below and it worked with wsHttpBinding.

Create WCF service for unmanaged C++ clients

However I need to connect to a WCF service with a custom binding. This is how the code for the Custom Binding looks like in my app config file

<?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="[email protected]" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

I have a bridge DLL which is a managed C++ DLL. The managed C++ DLL connects the C# Client to the native appliaction. However I am unable to connect to the Web Service from the managed C++ DLL as the web service is using custom binding. The error I get is:

The http request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the sever was 'Negotiate,NTLM'

This is how I tried to connect to the Webservice from the manged C++ dll:

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

So basically I need to connect the managed C++ dll to the WCF Service with custom binding. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

江南月 2024-11-09 11:59:22

您正在尝试在客户端代码中使用 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>

查看 这篇文章了解更多

You are trying to use the BasicHttpBinding in your client code.

In the config file you require NTLM:

authenticationScheme="Ntlm"

The error points you to what you you have in the service's config file.

*The http request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the sever was 'Negotiate,NTLM'*

You also look like you've tried to hack in

proxyAuthenticationScheme="Anonymous"

So it comes down to your security requirements. If you want the service to not have security just take out the NTLM reference. If you want security you'll need a security section in your binding definition, something like:

            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
              <message clientCredentialType="UserName" algorithmSuite="Default" />
            </security>

Look at this article for more

这样的小城市 2024-11-09 11:59:22

我认为您不需要自定义绑定,就像您想要自定义开箱即用的绑定一样。除非您的目的是在 TCP/IP 等之外创建专有通信协议。

对于安全问题,您需要考虑设置 Security.Mode 属性以及分配正确的传输和/或消息安全属性。例如。使用证书或密码质询、加密、加密和签名等。

您还需要在客户端执行相同的操作。绑定应该与服务器端的绑定几乎相同。

如果您不喜欢 basicHttp,那么总有 TCP、MSMQ、命名管道等。您应该查找它以获得完整列表。

I don't think you want custom binding, as much as you want to customize the out of the box bindings. Unless your intention was to create a propietary communication protocol, outside the the TCP/IP etc.

For the security issue, you would want to look into setting Security.Mode properties as well as assigning the right transport and/or message security properties. eg. use certificate or password challenge, encrypt, encrypt and sign etc.

You'll also need to do the same on the client side. The binding should be almost identical to that of the server side.

If you don't like basicHttp, there's always TCP, MSMQ, named piped and so on. You should look it up to get the full list.

橪书 2024-11-09 11:59:22

您是否尝试过使用 SvcUtil 从 Web 服务生成 WSDL。一旦您拥有客户端代理和配置,它将具有客户端连接到服务所需的配置。

您还提到您想使用 CustomBinding 进行连接,但在客户端代码中您使用的是 BasicHttpBinding。

您需要您的代码设置的另一件事是 AuthenticationScheme,因为服务器需要 NTLM 方案,而您的客户端代码没有设置该方案,并且默认情况下它是匿名的。

Have you tried to generate the WSDL from the web service using SvcUtil. Once you have the client proxy and the config which will have the configuration needed by client to connect to the service.

Also you did mention you want to connect using CustomBinding but in the client code you are using BasicHttpBinding.

One more thing you need your code it set is the AuthenticationScheme since the server is expecting scheme NTLM and your client code doesnt set that and by default it is Anonymous.

遗忘曾经 2024-11-09 11:59:22

对我来说,这看起来像是一个身份验证错误,因此您需要授予自己更多权限或找出谁可以获得它,但例如提供用户名和密码。

This looks like an authentication error to me, so you need oto give yourself more permissions or find out who to get it but for example providing a user name and password.

陪你到最终 2024-11-09 11:59:22

我维护使用 Wcf 服务的本机 C++ 应用程序。我建议使用优秀的 gSoap 库,而不是处理原始连接和 XML。这从服务中获取 WSDL 并生成使用它进行操作的代码。使用 http://code.google.com/p/gsoapwininet/ 插件所有通信均通过 IE 进行,这意味着自动支持所有各种 Windows 身份验证方法,这应该可以解决您的特定问题。

I maintain native c++ applications that consume Wcf services. Rather than deal with the raw connections and XML I will recommend using the excellent gSoap library. This takes the WSDL from a service and generates code to operate with it. By using the http://code.google.com/p/gsoapwininet/ plug-in all communication is directed via IE, which means that all the various windows authentication methods are automatically supported, which should solve your specific issue.

○愚か者の日 2024-11-09 11:59:22

您可以创建一个customBinding并传入所需的绑定配置名称。

You can create a customBinding and pass in the required binding configuration name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文