简体   繁体   中英

Error calling a SOAP api from .Net Core 2.0

We are using .Net Core 2.0 version to call an external SOAP service.

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

But whwen we use SOAPUI to cal the service, we get a success response:

SOAPUI Request:

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:

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

How can we call this service using .Net Core 2.0?

async void outside of an event handler is fire and forget

Reference Async/Await - Best Practices in Asynchronous Programming

which is probably why the context may have gone out of scope and aborted

The method should be refactored to 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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