IIS Express HttpContext身份来自异步调用

时间:2014-07-30 14:28:26

标签: c# iis

我在使用IIS Express在本地调试项目时遇到问题,它包含两个项目:Web服务和网站。

该网站提供.html页面,其中包含一个数据网格,可以调用Web服务获取数据。其中一个操作是使用POST请求更新数据行,但Web服务httpcontext标识为空。

如果我在网站上放置.aspx页面,则httpcontext标识可见。如果我将代码部署到服务器,则httpcontext标识是可见的。反正是通过IIS Express可以看到它吗?假设httpcontext应该通过异步调用可见,我错了吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我编写了一个名为CompleteOnBackgroundThread的实用程序函数来解决这个问题:

它很乱,但它确实有效。

可以像这样调用:

ThreadingUtils.CompleteOnBackgroundThread(() => { this.MgrPlayerGroups.DeletePlayerGroup(id).Wait(); });

以下是实施:

public static void CompleteOnBackgroundThread(this Action action)
{
    ManualResetEvent mre = new ManualResetEvent(false);
    Exception exception = null;

    WindowsIdentity appId = System.Security.Principal.WindowsIdentity.GetCurrent();
    Action surroundingAction = () =>
    {
        try
        {
            using (WindowsImpersonationContext wi = appId.Impersonate())
            {
                action();
            }
        }
        catch (AggregateException ex)
        {
            if (ex.InnerExceptions.Count == 1)
            {
                // Get the wrapped exception
                exception = ex.InnerExceptions.First();
            }
            else
            {
                exception = ex;
            }
        }
        catch (Exception ex)
        {
            exception = ex;
        }

        finally
        {
            mre.Set();
        }
    };
    surroundingAction.BeginInvoke(null, null);
    mre.WaitOne(TimeSpan.FromSeconds(1.0));
    if (exception != null)
    {
        throw exception;
    }
}