ASP.NET 访问在单独的线程中被拒绝

发布于 2024-12-05 08:15:28 字数 1040 浏览 1 评论 0原文

// test.ashx
public class test : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test.txt", "Test");
            Thread t = new Thread(write);
            t.Start();
            context.Response.Write("OK");
        }
        catch (Exception ex) 
        {
            context.Response.Write(ex.Message);
        }
    }

    private void write()
    {
        try
        {
           File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test2.txt", "Test2");
        }
        catch (Exception ex)
        {
            LogManager.Instance.Write(ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

我有一个简单的 ashx 文件,它将两个文本文件写入服务器磁盘。一种是直接写,另一种是在单独的线程中写。

问题是,在单独的线程中执行的 File.WriteAllText 方法会导致访问被拒绝异常。换句话说,“test.txt”被写入,但“test2.txt”导致访问被拒绝异常。

有没有办法给线程主会话线程的读/写权限?

// test.ashx
public class test : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test.txt", "Test");
            Thread t = new Thread(write);
            t.Start();
            context.Response.Write("OK");
        }
        catch (Exception ex) 
        {
            context.Response.Write(ex.Message);
        }
    }

    private void write()
    {
        try
        {
           File.WriteAllText(@"D:\inetpub\site.net\www\gs\data\test2.txt", "Test2");
        }
        catch (Exception ex)
        {
            LogManager.Instance.Write(ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

I have a simple ashx file that writes two text files to the disk of server. One is written directly and another is written in a separate thread.

The problem is, File.WriteAllText method which is executed in separate thread causes an Access Denied exception. In other words "test.txt" is written but "test2.txt" causes Access Denied exception.

Is there a way to give a thread Read/Write rights of main Session thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-12-12 08:15:28

执行 ASP.NET 请求的线程是 I/O 线程之一或 ASP.NET 工作进程 (aspnet_wp.exe) 中的工作线程之一。这些线程都是多线程单元 (MTA) 线程。如果您以编程方式在 .aspx 或 .asmx 代码中模拟帐户,或者通过在 Web.config 或 Machine.config 中使用来模拟,则模拟令牌将保留在此 MTA 线程上。如果随后调用单线程或单元线程 COM 组件,则该组件将由完全不同的线程访问,该线程是其单线程单元 (STA) 中的单个线程。由于该 STA 线程没有自己的模拟令牌,并且 MTA 线程的模拟令牌没有传递给 STA 线程,因此 STA 线程会在进程标识下执行。

为了解决这个问题,请看这里:
http://support.microsoft.com/kb/325791#top

The thread that executes your ASP.NET request is one of the I/O threads or one of the workerThreads in the ASP.NET worker process (aspnet_wp.exe). These threads are all Multi-Threaded Apartment (MTA) threads. If you programmatically impersonate an account in your .aspx or .asmx code, or if you impersonate by using in Web.config or Machine.config, then the impersonation token is held on this MTA thread. If you then make a call into a single-threaded or an apartment-threaded COM component, that component is accessed by an entirely different thread, which is the single thread in its Single-Threaded Apartment (STA). Because this STA thread does not have an impersonation token of its own, and the impersonation token of the MTA thread is not passed to the STA thread, the STA thread then executes under the process identity.

In order to solve this problem, take a look here :
http://support.microsoft.com/kb/325791#top

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文