如果我将 IHttpModule 安装到 GAC 缓存,则找不到它
Could not find the IHttpModule if I install it to the GAC cache
我有一个简单的 IHttpModule
namespace System.Web.Extensions.Resource
{
public class MyHttpModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestContent += PreSend_RequestContent;
}
private void PreSend_RequestContent(object sender, EventArgs e)
{
HttpResponse response = ((HttpApplication)sender).Response;
response.AddHeader("MyHttpModule", "Running");
}
}
}
然后我用 powershell 安装到 GAC(完全没有错误):
$name = "c:\MyHttpModule.dll";
[System.Reflection.Assembly]::Load('System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a');
$publish = New-Object System.EnterpriseServices.Internal.Publish;
$publish.GacInstall($name);
$type = 'System.Web.Extensions.Resource.MyHttpModule,' + [System.Reflection.AssemblyName]::GetAssemblyName($name).FullName;
C:\Windows\System32\inetsrv\Appcmd.exe add module /name:MyHttpModule /type:"$type"
但是当我访问 IIS 站点时,我得到了
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
所以Windows不再支持安装到GAC缓存?
编辑
我添加了强名称并确认程序集已安装到 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\MyHttpModule\v4.0_1.0.0.0__4959579d21f18138.
现在 IIS 有不同的错误
System.TypeLoadException: Could not load type 'System.Web.Extensions.Resource.MyHttpModule' from assembly 'MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4959579d21f18138'.
但是从 ILSpy 我可以看到这个类型是可用的,那到底是哪里出了问题?
再次编辑
突然有用了,我看到模块添加的header了。不知道为什么,但现在关闭它。
PublicKeyToken 为空,这意味着 DLL 未使用强名称签名。需要对Dll进行签名,然后放到GAC,请参考MSDN文章here,文章还告诉你如何在VS中进行签名:
我有一个简单的 IHttpModule
namespace System.Web.Extensions.Resource
{
public class MyHttpModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestContent += PreSend_RequestContent;
}
private void PreSend_RequestContent(object sender, EventArgs e)
{
HttpResponse response = ((HttpApplication)sender).Response;
response.AddHeader("MyHttpModule", "Running");
}
}
}
然后我用 powershell 安装到 GAC(完全没有错误):
$name = "c:\MyHttpModule.dll";
[System.Reflection.Assembly]::Load('System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a');
$publish = New-Object System.EnterpriseServices.Internal.Publish;
$publish.GacInstall($name);
$type = 'System.Web.Extensions.Resource.MyHttpModule,' + [System.Reflection.AssemblyName]::GetAssemblyName($name).FullName;
C:\Windows\System32\inetsrv\Appcmd.exe add module /name:MyHttpModule /type:"$type"
但是当我访问 IIS 站点时,我得到了
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
所以Windows不再支持安装到GAC缓存?
编辑
我添加了强名称并确认程序集已安装到 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\MyHttpModule\v4.0_1.0.0.0__4959579d21f18138.
现在 IIS 有不同的错误
System.TypeLoadException: Could not load type 'System.Web.Extensions.Resource.MyHttpModule' from assembly 'MyHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4959579d21f18138'.
但是从 ILSpy 我可以看到这个类型是可用的,那到底是哪里出了问题?
再次编辑
突然有用了,我看到模块添加的header了。不知道为什么,但现在关闭它。
PublicKeyToken 为空,这意味着 DLL 未使用强名称签名。需要对Dll进行签名,然后放到GAC,请参考MSDN文章here,文章还告诉你如何在VS中进行签名: