aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
959 stars 331 forks source link

Could not load file or assembly Microsoft.Live.Base exception #500

Closed albanx closed 7 months ago

albanx commented 1 year ago

Hi team I have adopted the OWIN library for migrating to ASP.NET (OWIN) web API and I was able to test everything locally and it works as expected, decode tokens and authenticate a user consuming JWT tokens. Important to note I was not using OWIN previously, but we are a .NET 4.7.2 Framework web app version

However, when I deploy to testing environment I get this exceptions, caused by OWIN:

[FileNotFoundException: Could not load file or assembly 'Microsoft.Live.Base, Version=17.302.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]
   System.ModuleHandle.ResolveMethod(RuntimeModule module, Int32 methodToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount) +0
   System.ModuleHandle.ResolveMethodHandleInternalCore(RuntimeModule module, Int32 methodToken, IntPtr[] typeInstantiationContext, Int32 typeInstCount, IntPtr[] methodInstantiationContext, Int32 methodInstCount) +158
   System.ModuleHandle.ResolveMethodHandleInternal(RuntimeModule module, Int32 methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +93
   System.Reflection.CustomAttributeData..ctor(RuntimeModule scope, CustomAttributeRecord caRecord) +77
   System.Reflection.CustomAttributeData.GetCustomAttributes(RuntimeModule module, Int32 tkTarget) +115
   System.Reflection.CustomAttributeData.GetCustomAttributesInternal(RuntimeAssembly target) +80
   Owin.Loader.DefaultLoader.SearchForStartupAttribute(String friendlyName, IList`1 errors, Boolean& conflict) +217
   Owin.Loader.DefaultLoader.GetDefaultConfiguration(String friendlyName, IList`1 errors) +68
   Owin.Loader.DefaultLoader.LoadImplementation(String startupName, IList`1 errorDetails) +89
   Owin.Loader.DefaultLoader.Load(String startupName, IList`1 errorDetails) +30
   Microsoft.Owin.Host.SystemWeb.OwinBuilder.GetAppStartup() +165
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +37
   System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +135
   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +160
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +581
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +168
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +277
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +369

[HttpException (0x80004005): Could not load file or assembly 'Microsoft.Live.Base, Version=17.302.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +532
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +714

Looking at the code seems that the issue comes from this line

https://github.com/aspnet/AspNetKatana/blob/dbe159e43e2eee44f315f26268943e8ab5a4f60d/src/Owin.Loader/DefaultLoader.cs#L194

So far I load the startup class using

<add key="owin:appStartup" value="Microsoft.OSG.Services.Membership.WebRole.Startup" />

This dll is a transient dependency of some random dependency in the codebase (nothing to do with OWIN itself).

I did find a github issue describing the same behaviour (OWIN instantiating all assembly attributes to try find the OwinStartup class attribute) that I am seeing. They even claim to have fixed the issue in the latest version, but I am on that version and still see the issue.

I did try and follow the OWIN docs for defining what class is the OWIN Startup class, which I hoped would prevent OWIN from doing the assembly search, but it did not work.

Tratcher commented 1 year ago

owin:appStartup should disable the assembly scanning, but you need to specify the full type and assembly: (guessing at your assembly name) <add key="owin:appStartup" value="Microsoft.OSG.Services.Membership, Microsoft.OSG.Services.Membership.WebRole.Startup" />

https://github.com/jinek/AspNetKatana/blob/dc7a59a5c0d2b79551a43455d2d1261800fe8eee/src/Owin.Loader/DefaultLoader.cs#L88-L91

Tratcher commented 1 year ago

As for why this wasn't fixed by the prior change, I think you're failing earlier than the previous issue because of a different type situation.

albanx commented 1 year ago

owin:appStartup should disable the assembly scanning, but you need to specify the full type and assembly: (guessing at your assembly name) <add key="owin:appStartup" value="Microsoft.OSG.Services.Membership, Microsoft.OSG.Services.Membership.WebRole.Startup" />

https://github.com/jinek/AspNetKatana/blob/dc7a59a5c0d2b79551a43455d2d1261800fe8eee/src/Owin.Loader/DefaultLoader.cs#L88-L91

It does not actually. The way we managed to disable the assembly scanning was adding the config like this

    <add key="owin:appStartup" value="Microsoft.OSG.Services.Membership.WebRole.Startup, Microsoft.OSG.Services.Membership.WebRole" />

We came to this conclusion looking at the source code here https://github.com/aspnet/AspNetKatana/blob/dbe159e43e2eee44f315f26268943e8ab5a4f60d/src/Owin.Loader/DefaultLoader.cs#L88

where it clearly skips the assembly scanning only if there is a comma in the value. The above trick fixed our issue, but I believe it is still a bug in general.