Question

I have an ASP.NET/MVC Web Role that is using the NHUnspell NuGet package. When I try to run it I get 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.

This is strange because I as far as I know, my Web Role project should not be trying to load the unmanaged Hunspellx64.dll at all. That should be handled by the managed NHUnspell DLL. That DLL is copied over to the /bin directory of the Web Role as a build step.

UPDATE: Thanks to Thomas's comment about WebActivator being outdated I was able to fix the issue. I am duplicating my reply comment to his accepted answer to make sure others who have this problem see the fix:

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

Here is the rest of the original post before the fix was received:

I have scoured my project for direct references/linkages to Hunspellx64.dll including my NuGet packages configuration, packages.config, web.config, my References list, the raw Project File, etc. I can't find any direct reference to that DLL. Where else can I look or what else can I try to stop my project from trying to load that unmanaged DLL directly? Here is the ASP.NET error dump:

[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
Était-ce utile?

La solution

The WebActivator Start method doesn't handle unmanaged DLLs in the bin folder. As you see in the code file: 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();
                            }
                        }
                    }

The Start() method loads all DLLs as assemblies to probe for its PreApplicationStartMethodAttribute. That fails for an unmanaged DLL because there is no assembly manifest.

It looks you are using a branch (dfolwler?) or a outdated version of WebActivator because the current version can handle this by ignoring all exceptions on assembly load.

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

Update WebActivator to the most recent version.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top