Areas not displaying in MVC 4 using MvcExtensions - c#

I am using ASP.NET MVC 4 RC and the latest version of MvcExtensions and MvcExtensions.Autofac.
I don't know if MVC 4 works differently to MVC 3, but my areas aren't displaying at all when using it with MvcExtensions. The code below is how I used it in my MVC 3 application. I have just copied and pasted it into my MVC 4 app. If I use the default Global.asax.cs file that came with the MVC 4 application then my areas display properly. Must this be done differently?
I replaced the Global.asax.cs file to look like this:
public class MvcApplication : AutofacMvcApplication
{
public MvcApplication()
{
Bootstrapper.BootstrapperTasks
.Include<RegisterAreas>()
.Include<RegisterControllers>()
.Include<RegisterRoutesBootstrapper>()
.Include<AutoMapperBootstrapper>()
.Include<FluentValidationBootstrapper>();
}
protected override void OnStart()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
base.OnStart();
}
}
RegisterRoutesBootstrapper, AutoMapperBootstrapper and FluentValidationBootstrapper are my custom bootstrapper classes.

I've just created a test Mvc4 app with MvcExtensions (v2.5.0) and with a custom area. Everything works fine for me.
Please make sure that you have bindingRedirects in your root web.config file:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Without these binding redirects areas will not work.

Related

Project MetadataReferences is not populated when ProjectReferences are present

I'm loading in a solution in the MSBuildWorkspace:
var msWorkspace = MSBuildWorkspace.Create();
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
Projects without ProjectReferences show all MetadataReferences, including mscorlib. Projects with ProjectReferences have an empty collection of MetadataReferences.
As compilation works, I guess the compiler platform for some reason is not populating this collection, and I'm wondering why?
If I remove the ProjectReferences, the MetadataReferences collection gets populated correctly.
EDIT: Diagnostics contains errors for missing mscorlib-types like Object and DateTime, so the project seems to not compile because of these missing references.
I finally figured out what was going on by checking the .Diagnostics on MSBuildWorkspace object after attempting to open the solution:
[Failure] Msbuild failed when processing the file 'c:\xxx\someproject.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets: (84, 5): The "Csc" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'.
It seems different versions of MSBuild were being used. Adding assembly redirects (copied from C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config) to my projects app.config solved it:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Tasks.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Utilities.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Engine" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
All references are now correctly populated on every project in the solution.
I'm on VS2017 15.7.1 and targeting .NET 4.7.1 so no NuGet packages were needed on top of the Microsoft.CodeAnalysis packages (I'm using 2.8.0).

AutoFixture with NUnit and AutoData throws TargetParameterCountException

In my unit test project I have installed AutoFixture (v3.40.0), NUnit (v2.6.4.) and AutoFixtrue.NUnit2(v3.39.0).
I'm using AutoData attribute on one of the dummy test cases
[Test, AutoData]
public void IntroductoryTest(
int expectedNumber)
{
}
, but when running the test I get the
System.Reflection.TargetParameterCountException : Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at NUnit.Core.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
at NUnit.Core.TestMethod.RunTestMethod()
at NUnit.Core.TestMethod.RunTestCase(TestResult testResult)
Is there anything I haven't installed or I'm missing?
That exception is caused by NUnit not loading the AutoFixture add-in at runtime so the test parameters don't get any arguments.
The reason is that AutoFixture.NUnit2 is compiled against version 2.6.2 so if you want to use it with 2.6.4 you'll have to add the following assembly binding redirects to the configuration file of your test project:
<configuration>
<runtime>
<dependentAssembly>
<assemblyIdentity
name="nunit.core.interfaces"
publicKeyToken="96d09a1eb7f44a77"
culture="neutral" />
<bindingRedirect
oldVersion="0.0.0.0-2.6.4.14350"
newVersion="2.6.4.14350" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity
name="nunit.core"
publicKeyToken="96d09a1eb7f44a77"
culture="neutral" />
<bindingRedirect
oldVersion="0.0.0.0-2.6.4.14350"
newVersion="2.6.4.14350" />
</dependentAssembly>
</runtime>
</configuration>
Note that the version of NUnit you need to redirect to is the one used by the test runner, which could be different than the version used during compilation.
So, while you might be compiling your tests against version 2.6.4, if your test runner uses version 2.6.3, then you need to redirect to 2.6.3 instead:
<configuration>
<runtime>
<dependentAssembly>
<assemblyIdentity
name="nunit.core.interfaces"
publicKeyToken="96d09a1eb7f44a77"
culture="neutral" />
<bindingRedirect
oldVersion="0.0.0.0-2.6.3.13283"
newVersion="2.6.3.13283" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity
name="nunit.core"
publicKeyToken="96d09a1eb7f44a77"
culture="neutral" />
<bindingRedirect
oldVersion="0.0.0.0-2.6.3.13283"
newVersion="2.6.3.13283" />
</dependentAssembly>
</runtime>
</configuration>

Why can I have multiple identical binding redirects in web.config?

Assuming I have one web.config with the following two identical sections:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
Why does this work, and what will happen if someone changes one of the redirects, such as to newVersion="4.0.0.0" instead of newVersion="6.0.0.0"?
If you have more than one binding redirect defined for one assembly, it uses the first one that it found and ignores all others.
So if you change the newVersion of the first one to 4.0.0.0 the runtime will try to load Version 4.0.0.0 of the assembly. The second redirect is ignored.
See also How the Runtime Locates Assemblies. When I understand it correctly it tooks the first element that has the matching assemblyIdentity
The elements are order-sensitive.....In case of a
conflict in redirection, the first matching redirection statement in
the configuration file is used.
Taken from https://msdn.microsoft.com/en-us/library/433ysdt1(v=vs.100).aspx

Tests in Visual Studio not running when using CreateController method of TestControllerBuilder

I am trying to user mvccontrib test helper and rhino mocks to test mvc controller.
But my test doesnt load.First two lines of the test are given below
TestControllerBuilder builder = new TestControllerBuilder();
var custCtrl = builder.CreateController<CustController>();
I am new to rhino mock and mvc contrib.
Just found the help here
kevinlabranche.com
The fix is a binding direct.
Add an app.config to your test project and paste in the following:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

StyleCopCmd: Run StyleCopCmd with StyleCop 4.4.*

Is there a way to run StyleCopCmd with StyleCop 4.4.*?
I have tried to simply exchange to old stylecop assemblies with the new ones but this leads to an exception that the assembly with the specific version 4.3.0.8 could not be loaded. Is there a hack?
Thanks in advance.
I have solved the problem by my own.
Here is my solution:
1.) Create a file named StyleCopCmd.exe.config in the StyleCopCmd.exe directory
2.) Make an assembly redirect from version 4.3.0.8 to 4.4.0.14
Configuration File:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.StyleCop.CSharp" publicKeyToken="31bf3856ad364e35" culture="Neutral" />
<bindingRedirect oldVersion="4.3.0.8" newVersion="4.4.0.14" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.StyleCop.CSharp.Rules" publicKeyToken="31bf3856ad364e35" culture="Neutral" />
<bindingRedirect oldVersion="4.3.0.8" newVersion="4.4.0.14" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.StyleCop" publicKeyToken="31bf3856ad364e35" culture="Neutral" />
<bindingRedirect oldVersion="4.3.0.8" newVersion="4.4.0.14" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I hope anyone will suck from profit out of it ;-)

Categories

Resources