0

I need to understand how I get the HttpResponseMessageProperty from an async web service request.

I'm attempting to migrate a C# Console App into an Azure Function App and the only methods now available in the connected service are async.

The error I'm getting is:

System.Private.ServiceModel: This OperationContextScope is being disposed out of order.

From what I've read, the error is likely to be a result of the OperationContextScope running on a different thread and OperatonContextScope is thread specific.

There is advice on the MS docs site which states:

If you need to call "await" for an async call, use it outside of the OperationContextScope block.

Not sure how I achieve this though, hence my question here.

Here's my code:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
        {

            AllowCookies = true,
            SendTimeout = new TimeSpan(0, httpTimeout, 0),
            ReceiveTimeout = new TimeSpan(0, httpTimeout, 0),
            MaxBufferSize = int.MaxValue,
            MaxReceivedMessageSize = int.MaxValue

        };

        /* set the https endpoint and create the service client object */
        EndpointAddress serviceEndpoint = new EndpointAddress(serviceBaseUrl + serviceName);
        AuthenticationServicePortTypeClient service = new AuthenticationServicePortTypeClient(binding, serviceEndpoint);

        /* set the request attributes and login */
        Login loginRequest = new Login()
        {

            UserName = authUser,
            Password = authPass,
            DatabaseInstanceId = databaseInstance,
            DatabaseInstanceIdSpecified = true

        };

        using (new OperationContextScope(service.InnerChannel))
        {

            LoginResponse1 loginResponse = await service.LoginAsync(loginRequest);


            if (loginResponse.LoginResponse.Return)
            {

                HttpResponseMessageProperty response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
                sessionCookie = response.Headers["Set-Cookie"];

            }

        }

The exception is throw on the HttpResponseMessageProperty line.

I just need to be able to obtain the cookie from the header.

Any help would be extremely appreciated.

Lymedo
  • 576
  • 9
  • 21
  • Found a solution here: https://stackoverflow.com/questions/18284998/pattern-for-calling-wcf-service-using-async-await – Lymedo May 02 '19 at 18:29

1 Answers1

0

Without seeing the full method and the calling code I call only speculate that you are not awaiting that code and something has already been disposed when the line throwing the exception is executed.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59