【发布时间】:2014-04-16 19:50:01
【问题描述】:
我没有开发 asp.net 应用程序的经验。
我想开发一个使用 Componente COM 且 SxS 免注册的 Web 应用程序。
按照这个MSDN Article, Http Module,我实现了一个HttpModule
public class SyncModule : IHttpModule
{
private MyEventHandler _eventHandler = null;
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(OnBeginRequest);
// Code that enable COM SxS registration-free
EnableSxSForThisThread_with_CreateActCtx();
int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
Debug.WriteLine("MyModule Thread Id: " + id);
}
public void Dispose() { }
public delegate void MyEventHandler(Object s, EventArgs e);
public event MyEventHandler MyEvent
{
add { _eventHandler += value; }
remove { _eventHandler -= value; }
}
public void OnBeginRequest(Object s, EventArgs e)
{
HttpApplication app = s as HttpApplication;
int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
Debug.WriteLine("OnBeginRequest Thread Id: " + id);
Debug.WriteLine("OnBeginRequest: Hello from OnBeginRequest in custom module.<br>");
if (_eventHandler != null)
_eventHandler(this, null);
}
注意打印线程 ID。
public void Init(HttpApplication app)
{
...
int id = System.Threading.Thread.CurrentThread.ManagedThreadId;
Debug.WriteLine("MyModule Thread Id: " + id);
}
我链接到我的网络应用程序,把它放在'Web.confg'中
<httpModules>
<add name="MyModule" type="MyModule.SyncModule, MyModule" />
</httpModules>
我运行我的 Web 应用程序,并且可以成功调用 COM 组件,但仅限于运行 HttpModule Init 的同一线程。
如果我单击按钮再次运行使用 COM 的线程更改并失败,因为未调用 HttpModule Init()。
如何检测所有新线程以正确调用 Init() 和 CreateActCtx? 可以吗?
【问题讨论】:
标签: c# asp.net httpmodule side-by-side