This OperationContextScope is being disposed out of order

Viewed 1502

I am calling WCF service in ASP.NET Core and everything is working fine, but whenever end of using gets executed, I get an error:

This OperationContextScope is being disposed out of order

I believe I am using wrong pattern to call WCF service using async/await but I am not sure what I am doing wrong.

Below is the code I am using to call a service.

[HttpPost]
public async Task<IActionResult> Runcase(IFormCollection formCollection)
{
    if (ModelState.IsValid)
    {
        var runnumber = formCollection["Run number"];
        await CallServiceasync();
        return RedirectToAction("", "");
    }
    else
    {
        return View(formCollection);
    }
}
public async Task CallServiceasync()
{
    var product = p1.Value;
    var a = product.first;
    foreach (int Age in a.age)
    {
        foreach (int Gender in a.sex)
        {
            foreach (int Healthclass in a.uclass)
            {
                RequestData requestData = new RequestData()
                {
                    ProductID = 534,
                    STATE = "CO",
                    AGE1 = Age,
                    SEX1 = Gender,
                    UND_CLASS1 = Healthclass,

                };
                RecieveResponseasync(requestData);
               }
        }
    }
}
public async Task RecieveResponseasync(InputValues inputValues)
{
    string reqedata = "";
    string apikey = "001010iZno7001010L";
    QuoteEngineService.MarketingSoftwareClient Service = new QuoteEngineService.MarketingSoftwareClient();
    await Service.OpenAsync();
    try
    {
        using (OperationContextScope scope = new OperationContextScope(Service.InnerChannel))
        {
            HttpRequestMessageProperty httpRequestMessage = new HttpRequestMessageProperty();
            httpRequestMessage.Headers.Add("apikey", apikey);
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestMessage;
            reqedata = inputValues.XmlSerializetoString();
            var result = await Service.ProcessRequestAsync(reqedata, "4fa2-ae27");
            var outputvalues = new OutputvaluesViewModel();
            outputvalues = result.DeserializeToObject();
            List<OutputValue> outputs = new List<OutputValue>();
            if (outputvalues.InitialPremium != null)
                outputs.Add(new OutputValue {  Name = "InitialPremium", Result = outputvalues.InitialPremium});
            if (outputvalues.TargetPremium != null)
                outputs.Add(new OutputValue {  Name = "TargetPremium", Result = outputvalues.TargetPremium });
            foreach (var output in outputs)
            {
                await _context.outputValues.AddAsync(output);
                await _context.SaveChangesAsync();
            }
            await Task.Delay(500);
        }
    }// **At this point I am getting error**
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        if (Service.State == System.ServiceModel.CommunicationState.Opened)
        {
            await Service.CloseAsync();
        }
    }
}

Error

1 Answers

From the docs:

Warning

Do not use the asynchronous "await" pattern within a OperationContextScope block. When the continuation occurs, it may run on a different thread and OperationContextScope is thread specific. If you need to call "await" for an async call, use it outside of the OperationContextScope block.

Related