0

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? 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.

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. The code for AutoMapperBootstrapper looks like this:

public class AutoMapperBootstrapper : BootstrapperTask {
     public override TaskContinuation Execute()
     {
          const string mappingNamespace = "MyProject.DomainModel.Mappings";

          IEnumerable<Type> mappingTypes = typeof(IEntity).Assembly
               .GetTypes()
               .Where(
                    type =>
                    type.IsPublic &&
                    type.IsClass &&
                    !type.IsAbstract &&
                    !type.IsGenericType &&
                    type.Namespace == mappingNamespace);

          mappingTypes.ForEach(t => Activator.CreateInstance(t));

          return TaskContinuation.Continue;
     } }

It underlined IEnumerable in blue with the error:

The type 'System.Web.Mvc.Controller' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Ok so when I compiled my project it is looking for an ASP.NET MVC 3 reference:

The type 'System.Web.Mvc.IViewPageActivator' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.  There about 10 such errors relating to the AutoMapperBootstrapper.cs file.

I didn't bother with this reference and added the MVC 4 reference. I thought that this would have solved my areas issue but it did not.

Any ideas why it is asking for an the MVC reference?

hazzik
  • 13,019
  • 9
  • 47
  • 86
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234

3 Answers3

1

Adding binding redirects should help you. Add at least following redirects:

<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>

An addition, consider using type loading suggested by hazzik.

AlexBar
  • 2,028
  • 1
  • 19
  • 13
0

I guess that the MvcExtensions and MvcExtensions.Autofac NuGet packages are compiled against ASP.NET MVC 3 and have strong references to System.Web.Mvc V3.0.0.0. They are not compatible with ASP.NET MVC 4. You will have to contact the author of those packages to provide you with an updated version that is compiled against ASP.NET MVC 4. Remember that ASP.NET MVC 4 hasn't hit RTM yet, so don't expect all NuGet packages to immediately get updated versions for it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Any particular reason for ruling out Binding Redirects being capable of curing the issue at hand? Ninject.Web and stuff happens to work (or am I confused and is the Ninject.MVC3 bit source code only...) ? (I know an answer saying "just use Binding Reidrects" without any qualifications would definitely be wrong too...) – Ruben Bartelink Jul 27 '12 at 20:49
0

The problem is in these lines:

typeof(IEntity).Assembly
    .GetTypes()

Please see this answer by Jon Skeet where he is explaining why this is happens and what to do with it.

Also you could look at the article "Get All Types in an Assembly" by Phil Haack

Community
  • 1
  • 1
hazzik
  • 13,019
  • 9
  • 47
  • 86