Azure Function “Could not load file or assembly”

2019-09-02 15:40发布

So we have been loving everything about functions except the fact that we have to keep certain libraries behind as Azure Functions like the idea of certain libraries updating. Microsoft.Owin is one of them. We would love to be on version 4.

Could not load file or assembly 'Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I have tried everything such as forcing via Project.json as well as consolidating across the entire solution. Locally debugging in VS same error happens. So deployed as well as local this is happening.

We get the similar issues with other libraries such as AMQP.

We are running the latest SDK as well - Microsoft.NET.SDK.Functions version 1.0.13

2条回答
劫难
2楼-- · 2019-09-02 15:47

Possible duplicate of the following question.

Azure Functions - Could not load file or assembly ''Microsoft.WindowsAzure.Storage'

Try to find whether Owin is native library of Azure functions or not. If yes, you can refer it directly. #r "Library.Namspace". Otherwise, you need to follow the suggestions in above thread.

查看更多
三岁会撩人
3楼-- · 2019-09-02 16:10

Thanks Ashokan for your fast response. I think I may have found a solution to this problem.

https://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/ Rather in particular this post : https://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

Look at "npiasecki commented on May 2, 2017" and he explains there. That pretty much did the trick and all is working now - locally, debugging via Visual Studio.

 private static void ConfigureBindingRedirects()
{
   BindingRedirect.RedirectAssembly("Microsoft.Owin", new Version("4.0.0"), "31bf3856ad364e35");

}

private static void RedirectAssembly(
    string shortName,
    Version targetVersion,
    string publicKeyToken)
{
    ResolveEventHandler handler = null;

    handler = (sender, args) =>
    {
        var requestedAssembly = new AssemblyName(args.Name);

        if (requestedAssembly.Name != shortName)
        {
            return null;
        }

        var targetPublicKeyToken = new AssemblyName("x, PublicKeyToken=" + publicKeyToken)
            .GetPublicKeyToken();
        requestedAssembly.Version = targetVersion;
        requestedAssembly.SetPublicKeyToken(targetPublicKeyToken);
        requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;

        AppDomain.CurrentDomain.AssemblyResolve -= handler;

        return Assembly.Load(requestedAssembly);
    };

    AppDomain.CurrentDomain.AssemblyResolve += handler;
}
查看更多
登录 后发表回答