在 .net Core 中调用 SOAP 服务 [英] Calling a SOAP service in .net Core

查看:81
本文介绍了在 .net Core 中调用 SOAP 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 .net 4.6.2 代码移植到一个 .net Core 项目,该项目调用 SOAP 服务.在新代码中,我使用 C#(由于某些配置原因,我现在不记得为什么).

I´m porting a .net 4.6.2 code to a .net Core project, that calls a SOAP service. In the new code I´m using C# (because of some config reasons I just can´t remember why right now).

但我收到以下异常.

接收对 https://someurl.com/ws 的 HTTP 响应时出错/Thing.pub.ws:某事.这可能是由于服务端点绑定未使用 HTTP 协议.这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭).有关详细信息,请参阅服务器日志.

An error occurred while receiving the HTTP response to https://someurl.com/ws/Thing.pub.ws:Something. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

抛出它的代码是

try
{
    var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    var endpoint = new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService"));

    var thing= new TheEndpoint.AService_PortTypeClient(binding, endpoint);
    thing.ClientCredentials.UserName.UserName = "usrn";
    thing.ClientCredentials.UserName.Password = "passw";

    var response = await thing.getSomethingAsync("id").ConfigureAwait(false);

}
finally
{
    await thing.CloseAsync().ConfigureAwait(false);
}

基于它调用服务的旧配置是这样的,我错过了什么?

Based on the old config where it works calling the service is like this, what am I missing?

<bindings>
  <basicHttpsBinding>
    <binding name="TheEndpoint_pub_ws_AService_Binder" closeTimeout="00:02:00"
        openTimeout="00:02:00" receiveTimeout="00:03:00" sendTimeout="00:03:00">
      <security mode="Transport">
        <transport clientCredentialType="Basic" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpsBinding>
</bindings>
<client>
  <endpoint address="https://someurl.com/ws/Thing.pub.ws:AService"
      binding="basicHttpsBinding" bindingConfiguration="TheEndpoint_pub_ws_AService_Binder"
      contract="TheEndpoint.AService_PortType" name="TheEndpoint_pub_ws_AService_Port" />
</client>

我只是无法在网上找到很多关于此的信息.希望你能帮助我.

I´m just unable to find lot of information on this online. Hope you can help me.

更新根据 Sixto Saez 的建议,我得到了一个端点来揭示它的错误,它是

UPDATE Per Sixto Saez suggestion I got the endpoint to reveal its error and it is

HTTP 请求未经授权,客户端身份验证方案为基本".从服务器收到的身份验证标头是Basic realm="Integration Server", encoding="UTF-8"'.

The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic realm="Integration Server", encoding="UTF-8"'.

如果成功,我会尝试找出该怎么做并在此处发布结果.

I´ll try to find out what to do and post the result here if successful.

更新 2

好的,现在我试着用这里的代码转移到新的语法

Ok now I tried to move to the new syntax with this code here

ChannelFactory<IAService> factory = null;
IAService serviceProxy = null;
Binding binding = null;

try
{
   binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);

   factory = new ChannelFactory<IAService>(binding, new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService")));            
   factory.Credentials.UserName.UserName = "usrn";
   factory.Credentials.UserName.Password = "passw";

   serviceProxy = factory.CreateChannel();

   var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);

    factory.Close();
    ((ICommunicationObject)serviceProxy).Close();  
}
catch (MessageSecurityException ex)
{
    //error caught here
    throw;
}

但我仍然遇到相同(略有不同)的错误.它现在有 'Anonymous' 而不是 'Basic' 并且现在在末尾缺少 ", encoding="UTF-8".

but I still get the same (slightly different) error. It now has 'Anonymous' instead of 'Basic' and is now missing ", encoding="UTF-8" at the end.

HTTP 请求未经授权,客户端身份验证方案为匿名".从服务器收到的身份验证标头是Basic realm="Integration Server"".

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="Integration Server"'.

是我这边的问题还是服务器的问题?

显然,我的 SOAP技能"现在非常缺乏,但我几乎尝试了我能想到的所有配置组合,但没有运气.希望有人能指出我正确的方向.

Obviously my SOAP "skills" are greatly lacking now days, but I have just about tried every config combo I can think of with this new approach without luck. Hope somebody can point me in the right direction.

推荐答案

好的,这个答案适用于那些尝试从 .net Core连接到 WCF 服务的人> 项目.

Ok this answer is for those who are trying to connect to a WCF service from a .net Core project.

这是我的问题的解决方案,使用新的 .net Core WCF 语法/库.

Here is the solution to my problem, using the new .net Core WCF syntax/library.

BasicHttpBinding basicHttpBinding = null;
EndpointAddress endpointAddress = null;
ChannelFactory<IAService> factory = null;
IAService serviceProxy = null;

try
{
    basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
    basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    endpointAddress = new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService"));
    factory = new ChannelFactory<IAService>(basicHttpBinding, endpointAddress);

    factory.Credentials.UserName.UserName = "usrn";
    factory.Credentials.UserName.Password = "passw";
    serviceProxy = factory.CreateChannel();

    using (var scope = new OperationContextScope((IContextChannel)serviceProxy))
    {
        var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);
    }

    factory.Close();
    ((ICommunicationObject)serviceProxy).Close();
}
catch (MessageSecurityException ex)
{
     throw;
}
catch (Exception ex)
{
    throw;
}
finally
{
    // *** ENSURE CLEANUP (this code is at the WCF GitHub page *** \
    CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}

更新

我使用上面的代码得到以下异常

I got the following exception using the code above

此 OperationContextScope 被无序处理.

This OperationContextScope is being disposed out of order.

这似乎是 WCF 损坏的东西(或需要解决)团队.

Which seems to be something that is broken (or needs addressing) by the WCF team.

因此我必须执行以下操作才能使其正常工作(基于此 GitHub 问题)

So I had to do the following to make it work (based on this GitHub issue)

basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

factory = new ChannelFactory<IAService_PortType>(basicHttpBinding, new EndpointAddress(new Uri("https://someurl.com/ws/TheEndpoint.pub.ws:AService")));
factory.Credentials.UserName.UserName = "usern";
factory.Credentials.UserName.Password = "passw";
serviceProxy = factory.CreateChannel();
((ICommunicationObject)serviceProxy).Open();
var opContext = new OperationContext((IClientChannel)serviceProxy);
var prevOpContext = OperationContext.Current; // Optional if there's no way this might already be set
OperationContext.Current = opContext;

try
{
    var result = await serviceProxy.getSomethingAsync("id").ConfigureAwait(false);

    // cleanup
    factory.Close();
    ((ICommunicationObject)serviceProxy).Close();
}
finally
{
  // *** ENSURE CLEANUP *** \
  CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
  OperationContext.Current = prevOpContext; // Or set to null if you didn't capture the previous context
}

但您的要求可能会有所不同.因此,以下是帮助您连接到 WCF 服务可能需要的资源:

But your requirements will probably be different. So here are the resources you might need to help you connecting to your WCF service are here:

这些测试对我帮助很大,但有些地方很难找到(我得到了帮助,感谢振兰 回答我的 wcf github 问题)

The tests helped me a lot but they where somewhat hard to find (I had help, thank you Zhenlan for answering my wcf github issue)

这篇关于在 .net Core 中调用 SOAP 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆