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

Error calling a SOAP api from .Net Core 2.0

我们正在使用.Net Core 2.0 版本来调用外部SOAP 服务。

相关调用方法为:

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

我们得到的响应:

An error occurred while receiving the HTTP response to http:///OBASEMDM.svc. 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).

但是当我们使用 SOAPUI 调用服务时,我们得到了成功响应:

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 响应:

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

根据 SO 上的一些问答,我们尝试实现由 WCF 创建的部分 ConfigureEndpoint 方法,如下所示。仍然得到同样的错误。

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

这是相关服务的 WSDL 架构:http://213.14.68.91:83/OBASEMDM.svc?WSDL

我们如何使用 .Net Core 2.0 调用此服务?

async void 在事件处理程序之外即触发即忘

引用Async/Await - Best Practices in Asynchronous Programming

这可能就是上下文可能超出范围并中止的原因

该方法应重构为 return 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;
    }
}