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