Question

I'm having difficulty publishing a website to Windows Azure and as such decided to turn on application logging.

Although the publishing of the site completes, an exception is thrown during application startup on the Azure cloud; The following event in the event log results:

Basically there are a few things of note from the just this one specific event from the eventlog

TargetInvocationException

Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Owin.Loader.DefaultLoader.<>c__DisplayClass12.b__b(IAppBuilder builder) at Owin.Loader.DefaultLoader.<>c__DisplayClass1.b__0(IAppBuilder builder) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.<>c__DisplayClass2.b__0(IAppBuilder builder) at Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action1 startup) at Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action1 startup) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) Could not load file or assembly 'Microsoft.Owin.Security, Version=2.0.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) at Owin.AppBuilderExtensions.UseExternalSignInCookie(IAppBuilder app, String externalAuthenticationType) at MySiteOnline.Startup.ConfigureAuth(IAppBuilder app) in c:\Users\Andre\Dropbox\Malty IT\Active Projects\MySiteOnline\MySiteOnline\App_Start\Startup.Auth.cs:line 20 at MySiteOnline.Startup.Configuration(IAppBuilder app) in c:\Users\Andre\Dropbox\Malty IT\Active at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Owin.Loader.DefaultLoader.<>c__DisplayClass12.<MakeDelegate>b__b(IAppBuilder builder) at Owin.Loader.DefaultLoader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.<>c__DisplayClass2.<InitializeBlueprint>b__0(IAppBuilder builder) at Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action1 startup) at Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action1 startup) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) at Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

A couple of things caught my eye with regards to this message: 1. The application logs contain references to files on my hard drive/dropbox folder. This makes no sense as this is a deployment running of the cloud! 2. For some reason unknown to me, the application is trying to load Microsoft.Owin.Security version 2.0.0 assembly, although I have installed version 2.1.0.0 in my project with copy local = true. I have also updated my assembly bindings in web.config as follows:

<dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security"            publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
      </dependentAssembly>

As per the stacktrace above, here is the code which results in the error being thrown:

 public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        //app.UseMicrosoftAccountAuthentication(
        //    clientId: "",
        //    clientSecret: "");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        //app.UseGoogleAuthentication();
    }
}

The system works perfectly on my development environment.

I think my mind might have been broken trying to solve this puzzle. I also tried digging around in the manifest file and dabbled with different nuget versions but alas... no success.

Was it helpful?

Solution

It seems the problem related to project / manifest files being corrupted due to issues with versions control. My hypothesis is that the following sequence of events messed thing up:

  1. Updated a couple of Nuget packages

  2. Pushed code to Source Control.

  3. Restored code to a previous version from source control.

I believe that somewhere during this process config/manifest/dll files were not handled in a consistent fashion and lead to the clusterbeep I had.

My solution was to revert to an earlier, working version of my project and to subsequently manually merge codechanges into the solution (using WinMerge).

Although a painful and time-consuming activity, this seems to have resolved all of the problems.

ADDITIONAL TIPS FOR ANYONE ELSE STRUGGLING WITH SIMILAR ISSUES:

  1. Enable "Wanrings" in your Error List window.

  2. Set build output verbosity to "Detailed". In VS2013, go to Tools > Options > Projects and Solutions > Build and Run. The set the option for "MSBuild project buil output verbosity" to "Detailed". Examine the output after build for any conflicts as this will also point out exactly where conflicting assemblies are located. (Projects in your solution are assigned a number and actions relating to each project is tagged accordingly with that number).

  3. Set up detailed application logging for your website on Azure. To do this select your site from the Azure management portal, click "Configure", browse down to "Application Diagnostics"

OTHER TIPS

Local file references in your stack trace is normal - that is the location your solution was built in.

Put a try / catch around the troublesome code and then walk the Exceptions (using InnerException) until you find the actual source. TargetInvocationException is just telling you code threw an exception but that the source was invoked via some otehr mechanism (in you case I assume via an IoC container).

I'd make sure you're deploying all related assemblies required by Owin to function - part of the reason it would work locally is you have all assemblies either in your GAC or on disk.

Most of these errors occur because the used Azure-SDK is not up-to-date. I would suggest that you try to upgrade your site to the latest Azure-SDK, which is currently version 2.3. And I would suggest you update all of your NuGet packages to the latest version. Please do not use any pre-release versions.

Then check all of your assembly references again, to see if every single assembly was set to "copy local".

Enable debugging for your website and debug the website using a new deployment slot, that will enable you to create a staging environment (if your website is currently live without the changes you posted here), if not push the site to the server and start to debug. Here is an article (latest, published 9 hours ago) on how to debug an Azure Website remotely:

How to Remotely Debug Azure Websites

Here another work-around (which is not very nice, but helped me sometimes), if assemblies could not be found on Azure:

If you have any dependent assemblies that could not be found for whatever reason, you can try to create a new folder in your web-site project, add those assemblies to the folder and set their "Build Action" to "content" and "copy always". Then you just reference those assemblies from within the folder in your Visual Studio solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top