WCF 服务无效方法 Will operation complete with Windows Impersonation

WCF service void method Will operation complete with Windows Impersonation

我有一个在 Windows 身份验证上配置的 WCF。这意味着只有经过身份验证的 Windows 用户才能访问。我有一个方法 return 除了启动一个线程,该线程在调用的 windows 用户模拟下执行一些长 运行 任务。

我的代码如下:

public void SampleTask();
{
    Task.Factory.StartNew(this.Activity);
}

private void Activity()
{
    WindowsIdentity identity = ServiceSecurityContext.Current.WindowsIdentity;
    using (WindowsImpersonationContext ctx = identity.Impersonate())
        {
        // Log status in a log database as "In Progress"
            // Do long running task (accessing database as current user)
        // retreive the results and save in a file
        // Update the log database as "Complete"
        }
}

我的问题是检索结果和保存结果的任务是否仍然完成,并将状态设置为应有的状态。或者由于没有开放会话,模拟将无法正常工作。还是我记错了

此致, 吉里贾香卡

只要方法正在执行,会话就应该保持打开状态。即使该方法正在返回 void,启动该方法执行的请求也会被回复。

如果您不需要服务的回复,您可以在方法上使用 IsOneWay = true 属性,这将告诉服务不要向客户端发送回复。由于这是一种不向客户端返回数据的长 运行ning 方法,因此它很适合作为单向方法。在这种情况下,我不确定会话是否会保持打开状态,但这并不重要,因为模拟上下文将限定在服务范围内,并且不依赖于客户端。

在你的方法中,你可以看到这个是因为声明:

WindowsIdentity identity = ServiceSecurityContext.Current.WindowsIdentity;

变量 identity 的范围是 Activity 方法。

using (WindowsImpersonationContect ctx = identity.Impersonate())

变量 ctx 同样限定在 Activity 方法中的 using 块。

你唯一 运行 我能想到的问题是如果服务抛出异常并崩溃 - 那么方法当然不会完成。

总而言之,由于模拟是基于服务 运行 所使用的身份,并且您不会向客户端返回任何数据,会话应该不会对方法 运行 服务的完成或身份 运行ning。