Why does my Unity DependencyResolver not work on shared hosting but work locally?

I am trying to deploy an ASP.NET MVC 3 application using Unity as an IoC container. The application works fine on the local server, but when deployed, it throws an exception: for it there is no constructor without parameters. And this is thrown for the controller, which should get some repository introduced by my Unity DependencyResolver.

I installed Unity with NuGet, so it must be specified directly, and I checked that it was copied to the bin folder.

Edit:

Here's the stack trace:

[MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 System.Activator.CreateInstance(Type type, Boolean nonPublic) +69 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67 [InvalidOperationException: An error occurred when trying to create a controller of type 'nBlog.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +196 System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49 System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13 System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841400 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +18 

Anyone have an idea what could be the problem?

Edit 2:

I solved the problem by adding links to the Mvc libraries (System.Web.Mvc, etc.) in the GAC instead of direct links.

Previously, the application directly referenced Mvc libraries, and they were deployed to the hosting server because .net 4.0 and mvc 2 were installed on the server, but since the hosting provider installed Mvc 3, I changed the links to the GAC. I suppose this was a security issue, but I'm new to this application security in hosted environments, and I would really like to know what exactly was the problem? Links to resources will also be appreciated.

+4
source share
1 answer

If Unity uses Reflection internally to create classes and dependencies, then you will have an implied dependency on your application that runs in Full Trust .

Many shared hosting providers allow up to medium trust , so it may be that Unity cannot reflect your code at runtime. You may be able to request the launch of your application in full trust mode, but it depends on your hosting provider.

Some related events from Rick Straggle's blog are here.

[EDIT] Try testing locally by editing the web.config file:

 <location allowOverride="false"> <system.web> <!-- level="[Full|High|Medium|Low|Minimal]" --> <trust level="Medium" originUrl=""/> </system.web> </location> 
0
source

Source: https://habr.com/ru/post/1343375/


All Articles