IIS中未执行线程处理任务 [英] Threading Task not executing in IIS

查看:374
本文介绍了IIS中未执行线程处理任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用表单身份验证模式的.Net应用程序,并且具有调用异步任务的功能.以下是该任务的摘要:

I have a .Net application that uses Forms Authentication mode and has a function that calls an asynchronous task. Below is the snippet of the task:

Task.Factory.StartNew(() => { UploadFunction(); }, tokenSource.Token, TaskCreationOptions.LongRunning,TaskScheduler.Default);

当我在Visual Studio IDE中进行测试时,线程工作正常.问题是当我部署它时,线程任务似乎被系统跳过或未执行.

The thread is working fine when I test in on the Visual Studio IDE. The issue is when I deploy it, the threading task seems to be skipped or not executed by the system.

我读过一篇文章,这可能是由IIS中的权限引起的:

I've read an article that this can be caused by permissions in IIS:

http://codemine.net/部署到IIS后,在aspnet中发布/线程不工作

对以上文章中的解决方案进行调整,以在Forms身份验证中实现,以下是该代码:

Tweaking the solution on above article to be implemented in Forms authentication, below is the code for that:

[System.Runtime.InteropServices.DllImport("advapi32.dll", EntryPoint = "LogonUser")]
private static extern bool LogonUser(
         string lpszUsername,
         string lpszDomain,
         string lpszPassword,
         int dwLogonType,
         int dwLogonProvider,
         ref IntPtr phToken);

    public JsonResult uploadimages(){

         try{

            IntPtr token = new IntPtr(0);
                        token = IntPtr.Zero;

                        bool returnValue = LogonUser("Administrator", "WIN-82CH4949B3Q", "Neuron14",
                                         3,
                                         0,
                                         ref token);

            WindowsIdentity newId = new WindowsIdentity(token);
            WindowsImpersonationContext impersonatedUser = newId.Impersonate();

            var task2 =     Task.Factory.StartNew(() => { UploadFunction(); }, tokenSource.Token, TaskCreationOptions.LongRunning,TaskScheduler.Default);

            impersonatedUser.Undo();

            return Json(new { message = "Success" }, "text/plain; charset=utf-8");

         }

         catch (ex Exception)
         {
            return Json(new { error= ex.Message }, "text/plain; charset=utf-8");
         }
    }

将上述代码实施到系统后,一旦部署在IIS中,仍然无法执行该线程.我真的很难解决这个问题,因为我真的不知道为什么部署时线程没有执行.

Implementing the above code to the system still does not execute the thread once deployed in IIS. I'm really having a hard time fixing this since I do not really know why the thread is not executing when deployed.

推荐答案

当您尝试模拟Windows身份时出现问题:

There is a problem when you attempt to impersonate a windows identity:

WindowsIdentity newId = new WindowsIdentity(token);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

var task2 =     Task.Factory.StartNew(() => { UploadFunction(); }, tokenSource.Token, TaskCreationOptions.LongRunning,TaskScheduler.Default);

impersonatedUser.Undo();

问题是Impersonate函数仅适用于该线程,而UploadFunction在另一个线程上运行.

The problem is that the Impersonate function only applies for that thread, and your UploadFunction is running on a different thread.

这是对您的代码的修改,在后台任务的线程上调用了Impersonate方法:

Here's a modification to your code which has the Impersonate method called on the background task's thread:

WindowsIdentity newId = new WindowsIdentity(token);
var task2 = Task.Factory.StartNew(() => 
                                 {
                                     using(WindowsImpersonationContext wi = newId.Impersonate())
                                     {
                                          UploadFunction();
                                     }
                                 },
                                 tokenSource.Token,
                                 TaskCreationOptions.LongRunning,
                                 TaskScheduler.Default
                               );

我的猜测是,它在您的本地计算机上有效,因为您正在上载到允许匿名访问的文件夹.生产服务器上的权限会更严格.

My guess is that it worked on your local machine because you were uploading to a folder which allowed anonymous access. The permissions on your production server would be much tighter.

这篇关于IIS中未执行线程处理任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆