10

I'm getting this error when running an MVC3 site on my locahost. It is a fresh MVC3 site just newly created, the HomeController Index method is where it's being thrown from, on the ViewBag.Message assignment.

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}

Stack trace:

at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at MVC3.Web.UI.Controllers.HomeController.Index() in C:\Users\mccarthy\Documents\Visual Studio 2010\Projects\MVC3\MVC3.Web.UI\Controllers\HomeController.cs:line 13
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

I have no idea what this error is and haven't seen it before. When I start up a new MVC2 project (what I've been using up until this point), the site runs just fine.

Is there some difference in how Microsoft treats security between the MVC3 and MVC2 frameworks?

David Clarke
  • 12,888
  • 9
  • 86
  • 116
Michael McCarthy
  • 1,502
  • 3
  • 18
  • 45
  • 1
    This [related question](http://stackoverflow.com/questions/4230909/odd-exception-in-mvc-3-project) might shed some light – PHeiberg Jan 06 '12 at 17:27
  • PHeiberg. Thanks for pointing me towards that link. My fix was a little different though. No where in my web.config did I explicitly set ``. So I looked in my machine.config file, and lo and behold, there it was! I commented it out, and the MVC3 application runs now. Thanks! – Michael McCarthy Jan 06 '12 at 19:16

1 Answers1

14

Okay, it seems that if you have the following line in your web.config:

<trust legacyCasModel="true" level="Full" />

this type of error could happen. Yet in the link that PHeiberg provided to me, it said the line could be in web.config. My web.config did not have it.

So I looked in machine.config, and there was the code line! I commented it out, and I can now run my MVC3 app.

I'm not too sure how it ended up in machine.config, or what else I might have broken as a result of me commenting it out, but this immediate problem is fixed.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Michael McCarthy
  • 1,502
  • 3
  • 18
  • 45
  • 10
    If you are having similar problems but in a Windows Forms based project, comment this line in your app.config file: – David Mar 01 '12 at 17:42
  • I have this problem in a Forms project... but unfortunately I can't change the security policy setting, because I also use POS .NET, which doesn't work without it. Don't suppose you have any idea what else to try? – Ashley Sep 08 '13 at 01:58
  • @Ashley Did you ever figure out what you could do? – skaz May 18 '14 at 17:25
  • @skaz My solution in the end was due to the fact that you can set the legacy security policy option per AppDomain. So I removed the option from my app.config, and create a second AppDomain with the option set at runtime purely for hosting POS .NET. I'd post my code, but it's not really relevant to the original post. The important part is to create your own AppDomain using an AppDomainSetup, on which you have called 'SetCompatibilitySwitches(new[] {"NetFx40_LegacySecurityPolicy"})'. – Ashley May 18 '14 at 20:55
  • @Ashley Thank you for your response. Do you setup and strip down that AppDomain with each call you make, or one time at the beginning? – skaz May 19 '14 at 07:03
  • 1
    @skaz I set it up during startup, because that's how it was before I made the change: I obtain an instance of PosExplorer (wrapped up in another class) and keep it around for the program's lifetime. The only difference now is that I create it in a remote AppDomain and access it via a proxy (of the wrapper class' interface). It introduced one or two marshalling headaches, but overall wasn't too much trouble, for my case. I had to change the wrapper class to inherit from MarshalByRefObject, and use ClientSponsor to make sure any device objects I retrieved stayed alive. – Ashley May 19 '14 at 11:11
  • Just in case anyone finds this like I did when using POS for .Net 1.14 you no longer need to use NetFx40_LegacySecurityPolicy as Microsoft added .Net 4 support among other things. https://msdn.microsoft.com/en-us/library/dn638335(v=winembedded.4).aspx – Greg Sep 07 '15 at 02:26