c# - 使用 NHUnspell NuGet 包时无法加载文件或程序集 Hunspellx64.dll?

标签 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.

这很奇怪,因为据我所知,我的 Web 角色项目根本不应该尝试加载非托管的 Hunspellx64.dll。这应该由托管的 NHUnspell DLL 处理。该 DLL 作为构建步骤被复制到 Web 角色的/bin 目录。

更新:多亏了 Thomas 关于 WebActivator 已过时的评论,我才能够解决这个问题。我将我的回复评论复制到他接受的答案中,以确保遇到此问题的其他人看到修复:

I had NHUnspell working successfully before this error started occurring. What broke things was installing AttributeRouting with NuGet. AttributeRouting drags in an old version of WebActivator (1.0.0.0). Unfortunately NuGet does not suggest an update to it when you execute an Update operation. You have to manually do the update via the Package Manager Console as per this web page's instructions:

http://www.nuget.org/packages/WebActivator

这是收到修复程序之前原始帖子的其余部分:

我已经搜索了我的项目以获取对 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

最佳答案

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();
                            }
                        }
                    }

Start() 方法将所有 DLL 作为程序集加载,以探测其 PreApplicationStartMethodAttribute。对于非托管 DLL,这会失败,因为没有程序集 list 。

看起来您正在使用分支(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;
    }
}

将 WebActivator 更新到最新版本。

关于c# - 使用 NHUnspell NuGet 包时无法加载文件或程序集 Hunspellx64.dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18325799/

相关文章:

c# - Xamarin 表单 HTTPClient 调用崩溃

c# - 从 C# 调用 libyahoo2 函数

mysql - ASP.NET如何在div中显示sqldatasource行?

mysql - 排除某行数据的SQL语句

c++ - 如何在Visual Studio代码中使用第三方DLL

c# - 在 C# 中使用 COM dll

无法在 firefox 插件中使用 ctypes.open 打开库(错误 126)

c# - 如何从 .NET 中的 m4a 文件中读取标签?

c# - Asp.net MVC 休眠 : Can not modify more than one base table through a join view

c# - 如何使用 Web 处理程序创建 PDF?