ASMX Web 服务在单独的线程中与 Windows 服务通信时出现问题

发布于 2024-10-10 04:53:19 字数 714 浏览 0 评论 0原文

Windows Server 2003(Amazon 虚拟机)上有一个 Windows 服务。有些应用程序可以与它通信(使用管道,但有一个包装器可以做到这一点)。它经过测试并且有效。此外,还有一个用 C# (ASP.NET) 编写的 Web 服务,它与上述 Windows 服务进行通信。当调用 Web 方法时,它会创建该类的一个实例并调用一个函数 - 该函数“连接”到 Windows 服务并向其发布作业。但是,如果在 Web 方法内部创建了一个线程,并且在线程内部调用了“连接”到 Windows 服务的函数,则连接会失败。与 Windows 服务的连接使用管道。 Web 服务在 IIS7 上运行。值得一提的是,所有这些都在我的本地计算机上工作,无论是从调试器(由 VS 2010 启动的本地服务器)还是从 IE 调用在本地 IIS7 上工作的 Web 服务上的 Web 方法。在本地一切正常 - 但在亚马逊实例上则不行。我不是网络程序员,所以我认为存在一些安全问题。有什么提示吗?谢谢。

编辑:Uwe 的评论提醒了我 - Web 方法首先尝试使用 http 下载一些文件,并将它们保存到路径 C:\intetpub\wwwroot\files\"。如果从 Web 方法下载文件,但下载失败,它可以工作。如果是从 Web 方法中创建的另一个线程完成的,则异常是:访问被拒绝。因此,我更改了上述文件夹的安全设置,并明确允许 IIS7 (IIS_IUSRS) 创建的用户读取/写入该文件夹,现在可以读取文件。下载的似乎这些问题的根源是相同的

编辑:解决方案已移至威尔的建议的答案。

There is a Windows Service on Windows Server 2003 (Amazon Virtual Machine). Some applications can communicate with it (using pipes, but there is an wrapper to do it). It is tested and it works. Also, there is a Web Service written in C# (ASP.NET) which communicates with mentioned Windows Service. When Web Method is called, it creates an instance of the class and calls a function - the function "connects" to Windows Service and post a job to it. But, if inside Web Method is created a thread and the function which "connects" to Windows Service is called inside a thread - connection fails. Connection to Windows Service uses pipes. Web Service works on IIS7. It is worth to mention that all works on my local machine, either from debugger (local server started by VS 2010) or from IE when I call Web Method on Web Service which works on local IIS7. In local all works - but on Amazon Instance doesn't. I'm not a web programmer, so I think there is some issue with security. Any hint? Thanks.

EDIT: Uwe's comment reminded me - the Web Method at first tries to download some files using http and it saves them to path C:\intetpub\wwwroot\files\". It works if files are downloaded from the web method, but download fails if it is done from another thread created in Web Method. Exception was: Access is denied. So, I changed security settings on mentioned folder and explicitly allowed user created by IIS7 (IIS_IUSRS) to read/write the folder, and now files can be downloaded. It seems that the source of these problems is the same.

EDIT: The solution is moved to an answer on Will's suggestion.

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

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

发布评论

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

评论(1

哭泣的笑容 2024-10-17 04:53:19

好的,伙计们,解决方案已经找到,正如威尔建议的那样,我将其发布在这里,作为我自己问题的答案。所以,解决方案:

问题是因为必须模拟 Web Method 创建的线程。因此,在调用者方法中,我必须这样做:

[WebMethod]
public void Fnc()
{
    ...
    ...
    System.Security.Principal.WindowsIdentity wi =    System.Security.Principal.WindowsIdentity.GetCurrent();
    System.Threading.Thread postJobThread = new Thread(PostJobThread);
    postJobThread.Start(wi);
    ...
}

...

private void PostJobThread(object ob)
{
    System.Security.Principal.WindowsIdentity wi = (System.Security.Principal.WindowsIdentity)ob;
    System.Security.Principal.WindowsImpersonationContext ctx = wi.Impersonate();

    ...
    // Do some job which couldn't be done in this thread
    ...
    // OK, job is finished
    if(ctx != null)
    ctx.Undo();
}

仅此而已。感谢对我的问题发表评论的人,我希望它对其他人有用。

Ok guys, the solution is found and as Will recomends, I'll post it here, as an answer to my own question. So, the solution:

The problem was because the thread created by Web Method has to be impersonated. So, in the caller method I had to something like:

[WebMethod]
public void Fnc()
{
    ...
    ...
    System.Security.Principal.WindowsIdentity wi =    System.Security.Principal.WindowsIdentity.GetCurrent();
    System.Threading.Thread postJobThread = new Thread(PostJobThread);
    postJobThread.Start(wi);
    ...
}

...

private void PostJobThread(object ob)
{
    System.Security.Principal.WindowsIdentity wi = (System.Security.Principal.WindowsIdentity)ob;
    System.Security.Principal.WindowsImpersonationContext ctx = wi.Impersonate();

    ...
    // Do some job which couldn't be done in this thread
    ...
    // OK, job is finished
    if(ctx != null)
    ctx.Undo();
}

That's all. Thanks to guys who commented my question and I hope it'll be useful for somebody else.

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