I am unable to see my tests in Test Explorer VS 2015 when creating CodedUI SpecFlow Tests - coded-ui-tests

I am unable to see my tests in Test Explorer VS 2015 when creating CodedUI SpecFlow Tests.
I have a CodedUI project that I have added SpecFlow into and unlike using a SpecFlow Project I am unable to see the SpecFlow tests to run in the Test Explorer.
This is all on a Windows 7 VS2015 Ultimate edition env.
I have created a .runsettings file as recommended for 2013.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<configuration>
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<specFlow>
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
<unitTestProvider name="MsTest" generatorProvider="Specflow.CodedUI.MsTestCodedUiGeneratorProvider, Specflow.CodedUI" runtimeProvider="TechTalk.SpecFlow.UnitTestProvider.MsTest2010RuntimeProvider, TechTalk.SpecFlow" />
<!-- <unitTestProvider name="SpecRun" />-->
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
<plugins>
</plugins>
</specFlow>
<runtime>
<assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.VisualStudio.QualityTools.CodedUITestFramework" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.VisualStudio.TestTools.UITest.Common" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.VisualStudio.TestTools.UITest.Extension" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.VisualStudio.TestTools.UITesting" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.VisualStudio.QualityTools.CodedUITestFramework" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
<dependentassembly>
<assemblyidentity culture="neutral" name="Microsoft.Services.TestTools.UITesting.Html" publickeytoken="b03f5f7f11d50a3a">
<bindingredirect newversion="14.0.0.0" oldversion="10.0.0.0"></bindingredirect>
</assemblyidentity>
</dependentassembly>
</assemblybinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6"/>
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6"
description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6"/>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite"
description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
</DbProviderFactories>
</system.data>
</configuration>
</RunSettings>
I have selected the Test settings file to point at this file.
Still hitting a brick wall when I build my solution locally.

Now resolved this issue
If you have your project setup to save into your network folder not a local folder then Test Explorer does not trust/recognize the DLL the project creates on build and therefore will not display the tests.
If you have the project either saved onto your local drive (C or D) or alternatively get the network drive to be recognized as trusted.

Related

Azure functions deploy issue [duplicate]

I have an Azure Website that I use to host my MVC + Entity Framework project. When I run the site locally using the same connection to my SQL Azure database, everything works perfectly, but when I deploy the site to my Azure Website, I get the following error:
Format of the initialization string does not conform to specification starting at index 0.
I followed the steps in several other posts and added the connection string to the thrown error and this is what the connection string comes out to be:
Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test#abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;
This string matches the connection string that is used locally.
Why would this work locally with the same string, but not remotely?
EDIT
Here is the full web.config for the site:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=tcp:abcde1fgh2.database.windows.net,1433;Database=SalesLeads;User ID=username_test#abcde1fgh2;Password=Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
</connectionStrings>
<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>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="102400" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<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.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</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.WebPages" 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.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNet.SignalR.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.2.0" newVersion="2.1.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
You should FTP into your Azure website and also check your connection string there in your web.config.
When you publish your site your web.config gets transformed/modified by Azure so it can be that this process messes up something with your connection string which leads to this error. For example if you setup your connection string through the web portal maybe during some copy/pasting the encoding is messed up, etc.
10/2/17 - Using Azure Web App + ASP.NET MVC (.NET 4.6.1) + Entity Framework (latest). For a quick fix, I put the connection string in code and it worked:
public class MyDbContext : DbContext
{
private const string connectionString = "Server=tcp:MYDB.database.windows.net,1433;Initial Catalog=MYDBNAME;Persist Security Info=False;User ID=MYUSERID;Password=MYPW;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
public MyDbContext() : base(connectionString)
{
}
}
By some research around this, I found out the error occurs in the following possible cases :
Possibility 1: You are probably using the wrong DbConnection class. If you are using Access then make sure you use OleDbConnection, not SqlConnection ... and vice versa.
Possibility 2: It indicates that the issue is in some way database related, But the issue is linked to the APP POOL identity way of running web sites on IIS. There is some background on what this means here in Application Pool Identities. We have noticed that we tend not to see the issue on sites which are configured in the more old fashioned way, which is to create a new 'IUSR' user account on the server, then add this user to the IIS users group, and then set the site to use this user rather than the APP POOL identity. Ensure the password expiry issue is sorted on your main admin account on the server. And if this does not resolve the issue, to create a new 'IUSR' Windows user account, and set the site up to use this rather than the APP POOL identity. On shared hosting, this may not be possible, but dealing with the admin password expiry is something the host may still be able to address.
In my case, I forgot to wrap the SQL_URI in
#Microsoft.KeyVault(SecretUri=SQL_URI)
That happened in my Web App > Configuration > Connection String > Value

MVC 5 Deploying IIS7 403 Access Denied

I'm upgrading MVC and .Net versions. I have a working upgraded MVC 5 Website in development that is running fine on IIS7 and windows 10.
.Net 4.6.1
MVC 5.2.3.0
EntityFramework 5.0.0.0
System.Web 3.0.0.0
IIS is set to v4.0 in App Pool.
I have checked file rights for the website folder (also was previously working anyway)
Upon deploying to Windows Server 2008 R2 which was previous working, and getting error 403 - Forbidden on the root of the site (I do not get this on my local machine)
I have tried what I could find so far on stack overflow. Any ideas?
This is my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
</connectionStrings>
<appSettings>
</appSettings>
<system.web>
<customErrors mode="Off" />
<globalization culture="auto" />
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" executionTimeout="45" maxRequestLength="2048000" requestLengthDiskThreshold="512" useFullyQualifiedRedirectUrl="true" enable="true" maxQueryStringLength="1000000" />
<sessionState mode="InProc" timeout="239" />
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="240" />
</authentication>
</system.web>
<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.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</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.WebPages" 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.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2200000000" />
<!--2GB-->
</requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
</handlers>
<staticContent>
remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Deployment directory listing not found. Please check the directory structure on deployment server.
Make sure below URL is correct.
<forms loginUrl="~/Account/LogOn" timeout="240" />

Azure Website ASP.net WebPages "The page cannot be displayed because an internal server error has occurred."

I've developed an application in WebPages and im hosting the website on Azure. Its giving me the error "The page cannot be displayed because an internal server error has occurred."
I'm not sure what i'm doing wrong.
Heres the Web.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Webpages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<customErrors mode="Off"/>
</system.web>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add invariant="System.Data.SqlServerCe.4.0" name="Microsoft® SQL Server® Compact 4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="2.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings><add name="SQLAzure" connectionString="Data Source=###;Initial Catalog=###;User ID=### ;Password=###;Encrypt=true;Trusted_Connection=false;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
If I remove the assemblyBindings "Could not load file or assembly 'System.Web.Helpers'" error pops up. Any help would be great, thanks.
If you are using Visual Studio, you should be able to determine the error by remote debugging your Azure web site
Here is another troubleshooting reference if you aren't using Visual Studio
Azure Websites have System.Web.Helpers, Version=1.0.0.0 and your web.config refers to Version=2.0.0.0.
Yes you can copy the correct version from your GAC to bin , but this is not optimal solution, because you have to upload binaries to Azure and that is not you job.
The best solution is use NuGet package manager.
You can do it from your visual studio
run the following command in the Package Manager Console Install-Package microsoft-web-helpers

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