尽管添加了绑定重定向,但仍无法加载文件或程序集

SKL*_*LAK 5 c# asp.net reference .net-assembly

调用 Web API 时出现以下错误:

Could not load file or assembly 'Microsoft.CommonSchema.Services, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=872fbc9102191257' or one of its dependencies. The located assembly's manifest definition does not match
the assembly reference. (Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)

尽管在 web.config 中有这个:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.CommonSchema.Services" publicKeyToken="872fbc9102191257" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
  </dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里做错了什么。我的packages.config 中有4.2.0.0 版本,上面的绑定不应该处理我遇到的错误吗?

小智 0

对于我的 .NET 应用程序,我遇到了类似的问题。我自己实现了解决。我不确定这种方式对于 WebApi 是否也可行。

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string[] parts = args.Name.Split(',');
        if (parts.Length > 2)
        {   //A satellite assembly ends with .resources and uses a specific culture ?
            if (parts[0].Contains(".resources") && parts[2].EndsWith("neutral") == false)
                return null;
        }
        //If the assembly name does not qualify a satellite assembly, try to resolve it...
        string simpleName = args.Name.TrimEnd('\\');
        if (args.Name.Contains("="))
        {
            try
            {
                // args.Name looks like this:
                // "<AssemblyName>, Version=<major>.<minor>.<build>.<revison>, Culture=neutral, PublicKeyToken=<16 hexit number>"
                // We need to parse it to extract the version number and the simple name
                string versionStr = args.Name.Substring(args.Name.IndexOf("=", StringComparison.Ordinal) + 1);    // Don't look for "Version" it may vary by locale
                versionStr = versionStr.Substring(0, versionStr.IndexOf(",", StringComparison.Ordinal));
                simpleName = args.Name.Substring(0, args.Name.IndexOf(','));
                // Load the assembly using just the name
                AssemblyName assemblyName = new AssemblyName
                {
                    Name = simpleName
                };
                Assembly assembly = Assembly.Load(assemblyName);
                // Now replace the assembly's version number with the required version and return it.
                assembly.GetName().Version = new Version(versionStr);
                return assembly;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        else
        {   // search for given assembly under current loaded
            foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                if (ass.FullName.Contains(simpleName))
                    return ass;
            throw new RheoException("Could not find Assembly with name: " + simpleName);
        }
    }
Run Code Online (Sandbox Code Playgroud)