繁体   English   中英

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

我们得到的回应:

接收对 http:///OBASEMDM.svc 的 HTTP 响应时出错。 这可能是由于服务端点绑定未使用 HTTP 协议。 这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。

但是当我们使用 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 - 异步编程的最佳实践

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

应该重构该方法以返回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