gpt4 book ai didi

c# - 在 .net Core 中调用 SOAP 服务

转载 作者:IT王子 更新时间:2023-10-29 04:38:31 27 4
gpt4 key购买 nike

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

但我收到以下异常。

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

基于调用服务的旧配置,我缺少什么

<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>

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

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

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"'.

如果成功,我会尝试找出要做什么并在此处发布结果。

更新 2

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

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"。

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

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

显然,现在我的 SOAP“技能”非常欠缺,但我几乎尝试了所有我能想到的使用这种新方法的配置组合,但不幸的是。希望有人能指出我正确的方向。

最佳答案

好的,这个答案是为那些试图从 .net Core 项目连接到 WCF 服务的人准备的。

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

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

更新

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

This OperationContextScope is being disposed out of order.

这似乎是 something that is broken (或需要解决)WCF 团队。

所以我必须执行以下操作才能使其工作(基于此 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 服务:

这些测试对我帮助很大,但它们有点难找(我有帮助,谢谢 Zhenlan 的 answering my wcf github issue )

关于c# - 在 .net Core 中调用 SOAP 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com