Owin packages version conflict - owin

I have two projects in my solution, project1 and project2.
Project2 tends to self host with Owin.
I have enable CORS using Microsoft.Owin.Cors package.
Both project have Microsoft.OWIN 3.0.1 and Microsoft.AspNet.Cors
5.2.3.
Then, I copy my project2.exe to project1's debug folder and call
prorject2.exe from project1.
Then, then the exception box rises when trying to call project2.exe.
The message is that it requires System.Web.Cors version 5.0.0.0
assembly.
When I downgrade the Microsoft.AspNet.Cors to 5.0.0.0, it
again says it requires Microsoft.Owin version 2.0.2.
Why does this occur and how do I fix this version conflict? Both the projects is built under .Net Framewrok 4.5.2.

In order to resolve to conflict you must tell your code which version of OWIN to use. this can be done from your application's app.config.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Related

Azure WebApps - cannot load ServiceRuntime after upgrade to Azure 2.6

After upgrading to Azure 2.6 (from 2.5) and publishing web into Azure Web Apps, the web fails to start with exception below. It seems Azure 2.6 is not yet available at Web Apps. Anyone experiences this?
Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Try add the reference manually from the SDK directory and create a binding redirects.
C:\Program Files\Microsoft SDKs\Azure.NET SDK\v2.6\ref\Microsoft.WindowsAzure.ServiceRuntime.dll
<dependentAssembly>
<assemblyIdentity name="WindowsAzureTelemetryEvents" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.6.0.0" newVersion="2.6.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WindowsAzureEventSource" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.6.0.0" newVersion="2.6.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="msshrtmi" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.6.0.0" newVersion="2.6.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.ServiceRuntime" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.6.0.0" newVersion="2.6.0.0" />
</dependentAssembly>

how to change web.config when upgrading to Windows Azure.2.5

I'd like to know how to change web.config when upgrading to Windows Azure 2.5. Current web.config is like
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.ServiceRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.8.0.0" newVersion="1.8.0.0" />
</dependentAssembly>
You can easily edit your bindingRedirect section like this:
<bindingRedirect oldVersion="0.0.0.0-2.4.0.0" newVersion="2.5.0.0" />

HttpClient failing

When I run my app on the emulator for iOS it works fine. When I run it on a device the first call to HttpClient fails. My class containing the HttpClient calls is in a PCL. It looks like an instance of this bug.
However, the documented workarounds of adding the following to your iOS project app.config does not work for me (the system.net.http addition);
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
......
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.0.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
EDIT:
With this in my app.config, the following method successfully retrieves the data from the server, but fails to read it. The bottom line containing ReadAsAsync(..) fails and throws an exception. The exception is 'null' and nothing is output to the terminal either. It works fine on the emulator, but not the device.
public async Task<List<ExternalLoginViewModel>> GetExternalLoginsAsync()
{
using (var client = GetNewHttpClient(false))
{
client.DefaultRequestHeaders.Remove("Authorization");
var response = await client.GetAsync("api/account/externalLogins?returnUrl=/&generateState=true");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsAsync<List<ExternalLoginViewModel>>();
}
}
Your assembly binding redirection is wrong. Your iOS project should have an app.config file with this content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="2.0.5.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
The reason for the redirect is that the Microsoft.Net.HttpClient packages contains the System.Net.HttpClient namespace but that already exists on iOS and Android (with slightly different features even). At runtime it will resolve to the wrong assembly and not use the (Xamarin-)native iOS one.
You can find a demo project for iOS, Android and WP8 based on HttpClient in a PCL at my Github repo.
This was resolved by uninstalling the "Microsoft HTTP Client Libraries" nuget package from both the PCL and the iOS project. Then reinstalling on both. The iOS project has references now to 'System.Net.Http.Extensions' and to 'System.Net.Http.Primatives' but does not contain one to 'System.Net.Http'. The following binding redirect is used in the app.config file in the iOS project as well;
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />

Async controller method displays type name of Task<ActionResult>

I am playing around with ASP.NET MVC5 and RavenDB. I wanted to use Raven's asynchronous API but for the life of me can't get it to work.
My controller (the actual query result is faked since there is no data in the DB)
public class BooksController : Controller
{
public async Task<ActionResult> IndexAsync()
{
var books = this.documentSession.Query<Book>();
Book book = await books.FirstOrDefaultAsync() ??
new Book { Title = "Programming WCF service", ASIN = "B0043D2DUK" };
return this.View(book);
}
}
And the view IndexAsync.cshtml
#using Hydra.FubuConventions
#model Hydra.Models.Book
#{
ViewBag.Title = "Books Asynchronous";
}
<h2>Books Asynchronous</h2>
#Html.FormBlock(x => x.Title)
#Html.FormBlock(x => x.ASIN)
The Web.config
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<customErrors mode="On" defaultRedirect="Error.html"/>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
It works perfectly fine when I use a synchronous controller method to feed it with a model. But with the asynchronous method it only displays
System.Threading.Tasks.Task1[System.Web.Mvc.ActionResult]`
I found plenty of posts here on stackoverflow that indicate that an older version of MVC might be the problem. But I checked the version of the MVC dlls (System.Web.Helpers v3.0.11001.0, System.Web.Mvc v5.0.11001.0 and System.Web.WebPages v3.0.11001.0) in my output folder which seem to be correct.
Any ideas what I am missing?
UPDATE
I compared the list of loaded assemblies. Other than the obvious differences by assemblies loaded in only one of the projects they were a match. The web.config is identical. I replaced the call to RavenDB with the same test stub that I used in the new project. Still get the type name rather than the view.
If you use a custom ControllerActionInvoker, you'll have to inherit it from AsyncControllerActionInvoker.
You must target .NET 4.5 and set httpRuntime.targetFramework to 4.5 in your web.config.
Your web.config lacks the assembly redirection directives found on a new MVC project. I suspect older MVC assemblies are installed in the GAC and are picked up by your app.
In any case, the behavior is a typical version mismatch. You need to find why the older assemblies are loaded by comparing the differences between your project and a new, clean MVC 5 project.
On a new MVC 5.2 project, the <runtime> section in web config contains these assembly redirections:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
You should copy the redirections from the clean project to your old project and see whether this solves the problem

he type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception

After moving my Azure web app to SDK 2.1 I have started getting the
type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception.
error message. The same is working fine in the local environment but gives above error on Azure.
My web.config already has following entries:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.StorageClient"
publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.7.0.0" newVersion="1.7.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.ServiceRuntime"
publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.8.0.0" newVersion="1.8.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.Diagnostics"
publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
and
<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>
still it's not working in live Azure. Can someone please help me out?
It is possible that you may be running different Windows Azure DLLs than the ones installed (by default) on live Azure instance roles. Right click the Microsoft.WindowsAzure.[any] references in your project and check the version number in the Properties window and try to match them with the version numbers in your config files.

Resources