Rob*_*ler 4 c# asp.net dll unmanaged nhunspell
我有一个使用NHUnspell NuGet包的ASP.NET/MVC Web角色.当我尝试运行它时,我收到以下错误消息:
Could not load file or assembly 'Hunspellx64.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为据我所知,我的Web角色项目不应该尝试加载非托管的Hunspellx64.dll.这应该由托管的NHUnspell DLL处理.该DLL将作为构建步骤复制到Web角色的/ bin目录中.
更新: 感谢Thomas关于WebActivator过时的评论,我能够解决这个问题.我正在将我的回复评论复制到他接受的答案,以确保有此问题的其他人看到修复:
在此错误开始发生之前,我已成功运行NHUnspell.破坏的是用NuGet安装AttributeRouting.AttributeRouting在旧版WebActivator(1.0.0.0)中拖拽.不幸的是,当您执行Update操作时,NuGet不建议对其进行更新.您必须根据此网页的说明通过程序包管理器控制台手动执行更新:
这是修复收到之前原始帖子的其余部分:
我已经搜索了我的项目,直接引用/链接到Hunspellx64.dll,包括我的NuGet包配置,packages.config,web.config,我的引用列表,原始项目文件等.我找不到任何对该DLL的直接引用.我还能在哪里查看或者我还能尝试阻止我的项目直接加载该非托管DLL?这是ASP.NET错误转储:
[BadImageFormatException: Could not load file or assembly 'Hunspellx64.dll' or one of its dependencies. The module was expected to contain an assembly manifest.]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) +102
System.Reflection.Assembly.LoadFrom(String assemblyFile) +34
WebActivator.PreApplicationStartCode.Start() in D:\Code\Bitbucket\WebActivator\WebActivator\PreApplicationStartCode.cs:11
[InvalidOperationException: The pre-application start initialization method Start on type WebActivator.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'Hunspellx64.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +550
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath) +90
System.Web.Compilation.BuildManager.ExecutePreAppStart() +135
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516
[HttpException (0x80004005): The pre-application start initialization method Start on type WebActivator.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'Hunspellx64.dll' or one of its dependencies. The module was expected to contain an assembly manifest..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9874568
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
Run Code Online (Sandbox Code Playgroud)
WebActivator Start方法不处理bin文件夹中的非托管DLL.正如您在代码文件中看到的那样:https: //bitbucket.org/dfowler/webactivator/src/4c558d93cf3a/WebActivator/PreApplicationStartCode.cs
public static void Start() {
lock (initLock) {
if (!hasInited) {
// Go through all the bin assemblies
foreach (var assemblyFile in GetAssemblyFiles()) {
var assembly = Assembly.LoadFrom(assemblyFile);
// Go through all the PreApplicationStartMethodAttribute attributes
// Note that this is *our* attribute, not the System.Web namesake
foreach (PreApplicationStartMethodAttribute preStartAttrib in assembly.GetCustomAttributes(
typeof(PreApplicationStartMethodAttribute),
inherit: false)) {
// If it asks to be called after global.asax App_Start, keep track of the method. Otherwise call it now
if (preStartAttrib.CallAfterGlobalAppStart && HostingEnvironment.IsHosted) {
attribsToCallAfterStart.Add(preStartAttrib);
}
else {
// Invoke the method that the attribute points to
preStartAttrib.InvokeMethod();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Start()方法将所有DLL作为程序集加载,以探测其PreApplicationStartMethodAttribute.对于非托管DLL,这是失败的,因为没有程序集清单.
它看起来你正在使用分支(dfolwler?)或过时版本的WebActivator,因为当前版本可以通过忽略程序集加载的所有异常来处理这个问题.
private static IEnumerable<Assembly> Assemblies
{
get
{
if (_assemblies == null)
{
// Cache the list of relevant assemblies, since we need it for both Pre and Post
_assemblies = new List<Assembly>();
foreach (var assemblyFile in GetAssemblyFiles())
{
try
{
// Ignore assemblies we can't load. They could be native, etc...
_assemblies.Add(Assembly.LoadFrom(assemblyFile));
}
catch
{
}
}
}
return _assemblies;
}
}
Run Code Online (Sandbox Code Playgroud)
将WebActivator更新到最新版本.