OperationContext.Current是采用异步时,首先计谋后空/等待在WCF服务

编程入门 行业动态 更新时间:2024-10-26 00:26:33
本文介绍了OperationContext.Current是采用异步时,首先计谋后空/等待在WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用异步/等待在.NET 4.5模式来实现一些服务方法WCF。 例如服务:

I am using async/await pattern in .NET 4.5 to implement some service methods in WCF. Example service:

合同:

[ServiceContract(Namespace = "async.test/")] public interface IAsyncTest { Task DoSomethingAsync(); }

执行:

MyAsyncService : IAsyncTest { public async Task DoSomethingAsync() { var context = OperationContext.Current; // context is present await Task.Delay(10); context = OperationContext.Current; // context is null } }

我遇到的问题是,经过第一个等待 OperationContext.Current 返回空,我无法访问 OperationContext.Current.IncomingMessageHeaders 。

The problem I am having is that after first await OperationContext.Current returns null and I can't access OperationContext.Current.IncomingMessageHeaders.

在这个简单的例子,这不是一个问题,因为我可以捕捉在计谋的范围内。但在现实世界的情况下 OperationContext.Current 从调用堆栈深处被访问,我真的不想改变很多code只是通过范围内进一步。

In this simple example this is not a problem since I can capture the context before the await. But in the real world case OperationContext.Current is being accessed from deep inside the call stack and I really don't want to change lots of code just to pass the context further.

有没有办法在来获取操作方面等待点,而无需手动传递下来的堆?

Is there a way to get operation context after await point without passing it down the stack manually?

推荐答案

我觉得你最好的选择是真正抓住它,然后手动将它传递。您可能会发现这提高了你的code中的可测性。

I think your best option is to actually capture it and pass it manually. You may find this improves the testability of your code.

这是说,有一对夫妇的其他选项:

That said, there are a couple of other options:

  • 将其添加到 LogicalCallContext 。
  • 在安装自己的的SynchronizationContext 这将设置 OperationContext.Current 时,它做了发布;这是怎么了ASP.NET preserves其 HttpContext.Current 。
  • 在安装自己的的TaskScheduler 这台 OperationContext.Current 。
  • Add it to the LogicalCallContext.
  • Install your own SynchronizationContext which will set OperationContext.Current when it does a Post; this is how ASP.NET preserves its HttpContext.Current.
  • Install your own TaskScheduler which sets OperationContext.Current.
  • 您可能还需要在Microsoft Connect上提出这个问题。

    You may also want to raise this issue on Microsoft Connect.

    更多推荐

    OperationContext.Current是采用异步时,首先计谋后空/等待在WCF服务

    本文发布于:2023-11-05 04:15:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1559874.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:计谋   OperationContext   Current   WCF

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!