简体   繁体   English

从 .Net Core 2.0 调用 SOAP api 时出错

[英]Error calling a SOAP api from .Net Core 2.0

We are using .Net Core 2.0 version to call an external SOAP service.我们使用 .Net Core 2.0 版本来调用外部 SOAP 服务。

Related call method is:相关调用方法为:

public async void Login(/*string username, string password*/)
{
    try
    {
        OBASEMDMClient client = new OBASEMDMClient();
        var loginCredential = new LoginCredential
        {
            UserNameOrEMail = "username",
            Password = "password"
        };
        var response = await client.LoginAsync(loginCredential);
    }
    catch (Exception e )
    {
        throw e;
    }
}

the response we get:我们得到的回应:

An error occurred while receiving the HTTP response to http:///OBASEMDM.svc.接收对 http:///OBASEMDM.svc 的 HTTP 响应时出错。 This could be due to the service endpoint binding not using the HTTP protocol.这可能是由于服务端点绑定未使用 HTTP 协议。 This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。

But whwen we use SOAPUI to cal the service, we get a success response:但是当我们使用 SOAPUI 来调用服务时,我们得到了一个成功的响应:

SOAPUI Request: SOAPUI 请求:

soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:obas="http://schemas.datacontract.org/2004/07/OBase.MDM.Entity">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Login>
         <tem:credential>
            <obas:Password>password</obas:Password>
            <obas:UserNameOrEMail>userame</obas:UserNameOrEMail>
         </tem:credential>
      </tem:Login>
   </soapenv:Body>
</soapenv:Envelope>

SOAPUI Response: SOAPUI 响应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <LoginResponse xmlns="http://tempuri.org/">
         <LoginResult xmlns:a="http://schemas.datacontract.org/2004/07/OBase.MDM.Entity" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Data i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">session-token</a:Data>
            <a:Message>İşlem başarı ile tamamlandı.
Login: 0,0069797Persistent login has been used</a:Message>
            <a:Result>true</a:Result>
            <a:ResultCode>300</a:ResultCode>
         </LoginResult>
      </LoginResponse>
   </s:Body>
</s:Envelope>

According to some Q&As on SO, we tried to implement partial ConfigureEndpoint method created by WCF as following.根据 SO 上的一些问答,我们尝试实现由 WCF 创建的部分ConfigureEndpoint方法,如下所示。 Still get the same error.仍然得到同样的错误。

static void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
{
    serviceEndpoint.Binding = new BasicHttpBinding();
    serviceEndpoint.Binding.Name = "BasicHttpBinding_IOBASEMDM";
    serviceEndpoint.Address = new EndpointAddress("http://<address>/OBASEMDM.svc");
}

This is the WSDL schema of the related service: http://213.14.68.91:83/OBASEMDM.svc?WSDL这是相关服务的 WSDL 架构: http : //213.14.68.91 : 83/OBASEMDM.svc? WSDL

How can we call this service using .Net Core 2.0?我们如何使用 .Net Core 2.0 调用此服务?

async void outside of an event handler is fire and forget事件处理程序之外的async void被触发并忘记

Reference Async/Await - Best Practices in Asynchronous Programming参考Async/Await - 异步编程的最佳实践

which is probably why the context may have gone out of scope and aborted这可能就是上下文可能超出范围并中止的原因

The method should be refactored to return Task应该重构该方法以返回Task

public async Task LoginAsync(string username, string password) {
    try {
        OBASEMDMClient client = new OBASEMDMClient();
        var loginCredential = new LoginCredential {
            UserNameOrEMail = username,
            Password = password
        };

        var response = await client.LoginAsync(loginCredential);

        //...
    } catch (Exception e ) {
        throw e;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM