【问题标题】:This OperationContextScope is being disposed out of order此 OperationContextScope 被乱序处理
【发布时间】:2020-10-08 19:36:45
【问题描述】:

我在 ASP.NET Core 中调用 WCF 服务,一切正常,但每当 using 的结尾被执行时,我就会收到错误:

这个 OperationContextScope 被乱序处理

我相信我使用了错误的模式来使用 async/await 调用 WCF 服务,但我不确定我做错了什么。

下面是我用来调用服务的代码。

[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();
        }
    }
}

【问题讨论】:

  • 应用程序在哪一行被抛出?
  • 我在 Using() 关闭花括号时遇到错误
  • 如果您发布异常的详细信息(包括堆栈跟踪)会很有帮助。
  • 我刚刚发布了异常的详细信息。你能看一下吗?

标签: c# asp.net-mvc wcf asp.net-core async-await


【解决方案1】:

来自docs

警告

不要在 OperationContextScope 块中使用异步“等待”模式。当继续发生时,它可能在不同的线程上运行,并且 OperationContextScope 是特定于线程的。如果您需要为异步调用调用“等待”,请在 OperationContextScope 块之外使用它。

【讨论】:

  • 我从 using 块内的所有行中删除了 await 关键字,但随后我得到空结果作为响应。
  • 这不仅仅是删除await关键字,你不能在范围内使用那些XXXAsync方法
  • 我正在调用该服务,它只有 ProcessRequestAsync() 方法。
  • 你不能使用OperationContextScopeService 是 WCF 代理吗?如果是这样,您可以生成同步客户端。
  • 我不确定。有什么方法可以检查服务是否是 WCF 代理?
猜你喜欢
  • 2012-10-22
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
相关资源
最近更新 更多