asp.net mvc is not using the controller factory specified - asp.net-mvc

I am using ASP.NET MVC Preview 2 that is installed with VS2010 Beta 2. I am hooking up WindsorControllerFactory like so:
Container = new WindsorContainer();
Container.RegisterControllers(typeof(HomeController).Assembly);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
My HomeController has one dependency that is passed into the constructor, when running I am getting the following error:
No parameterless constructor defined for this object.
[MissingMethodException: No parameterless constructor defined for
this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType
type, Boolean publicOnly, Boolean
noCheck, Boolean& canBeCached,
RuntimeMethodHandle& ctor, Boolean&
bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean
publicOnly, Boolean fillCache) +86
System.RuntimeType.CreateInstanceImpl(Boolean
publicOnly, Boolean
skipVisibilityChecks, Boolean
fillCache) +230
System.Activator.CreateInstance(Type
type, Boolean nonPublic) +67
System.Activator.CreateInstance(Type
type) +6
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext, Type controllerType)
+522
[InvalidOperationException: An error occurred when trying to create a
controller of type
'Xenotar.WedPlannerPro.Controllers.HomeController'.
Make sure that the controller has a
parameterless public constructor.]
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext, Type controllerType)
+659
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
requestContext, String controllerName)
+198
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase
httpContext) +225
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext
httpContext) +86
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext
httpContext) +36
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+181
System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously)
+75
By looking at this stack trace it does not look like MVC is using the WindsorControllerFactory but instead it still uses the DefaultControllerFactory?
When doing this in MVC 1.0 it works.
SOLUTION
The MvcContrib is not compatible with ASP.NET MVC 2.

You should be able to modify MvcContrib to work with ASP.NET MVC 2. The code for controller registration is pretty minimal.

Related

Site is working in port 80 but the same site is not working while changing the port to any other number

i have to change the port of website for ssl proxy implementation but the site is not working when im trying to listen to any other port than 80 (ex:81,82,90,8443, 9443 etc).
The site works fine in port 80, other ports getting below error,
**
Server Error in '/' Application.
**
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
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, StackCrawlMark& stackMark) +159
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +256
System.Activator.CreateInstance(Type type, Boolean nonPublic) +127
System.Activator.CreateInstance(Type type) +11
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +92
[InvalidOperationException: An error occurred when trying to create a controller of type 'WRIPMS.Controllers.LoginController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +256
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +81
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +280
System.Web.Mvc.<>c__DisplayClass6.b__2() +80
System.Web.Mvc.<>c__DisplayClassb1.<ProcessInApplicationTrust>b__a() +19
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func1 func) +128
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12650919
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
The error indicates that one of your controllers has a constructor that accepts a parameter. This is normal for an application with dependency injection. To make it work, you must create a custom IControllerFactory and inject it at application startup using
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
Since your application is working on port 80, I suspect that you have a controller factory (or a dependency of controller factory) that has something hard coded to work with port 80 only.
Im sorry it was my mistake there was some redundant entries in my web.config like Keys having path to save error log and all. My understanding was it wont make any troubles. It was working in local servers too. Well it got worked when i cleaned web.config redundant entries.
thank you.

error constructor controller MvC 4

public partial class RegistrationController : Controller
{
private readonly IPartnerRepository _partnerRepository;
public RegistrationController(IPartnerRepository partnerRepository)
{
_partnerRepository =partnerRepository;
}}
I use autoface to the dependancy injection and i have this error:
` No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
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, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: Une erreur s'est produite lors de la tentative de création d'un contrôleur de type « Registration.Front.Web.Controllers.RegistrationController ». Assurez-vous que le contrôleur a un constructeur public sans paramètre.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
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) +197
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +49
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() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
`
when i delete from the constructor, i haven't this error:
public partial class RegistrationController : Controller
{
private readonly IPartnerRepository _partnerRepository;
public RegistrationController()
{
}}
How can i resolve this?
ASP.NET MVC has special service that is responsible for creating an instance of controller - this service should implement IControllerFactory interface. Default implementation of the IControllerFactory expects that your controller has a parameterless constructor and invokes it.
If you want to use DI with controllers you have two possible options:
Write your own implementation of IControllerFactory which uses your DI container for instantiating a controller. You can register it in such way:
var factory = new CustomControllerFactory(container);
ControllerBuilder.Current.SetControllerFactory(factory);
Implement IDependencyResolver which can resolve any piece of MVC infrastructure including controllers.
Second option is preferrable.
You can read about this stuff with more details here (it's about Unity container but I think it should give you enough knowledge to do this with Autofac as well.
This can happen for number of reasons, one of them is that the dependency injection is not configured to inject a concrete class of IPartnerRepository.
The second is that if the concrete class itself has a constructor that depends on another dependency being injected, this second constructor is no configured for dependency injection.
And finally this can also happen if the DLL that contains the class being injected cannot be found, but usually the injection framework will tell you that it can't find the DLL.
If you are using Dependency Injection with Unity in an ASP.NET application
I got this problem resolved after I added
UnityConfig.RegisterComponents();
in application start in global.asax

Strange error on one web server in our production environment: MissingMethod exception, though I don't think a method is missing

We're running a web application distributed across load balanced 3 web servers. The exact same code base & configuration is deployed across the 3 servers, and since about 1 hour ago I'm getting the following error on one of them, but not the other two.
System.InvalidOperationException: An error occurred when trying to create a controller of type 'XXX.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor. ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
--- End of inner exception stack trace ---
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
We are running this with ASP.NET MVC (with Output Caching), NHibernate (with NHibernate caching) & StructureMap. The cache is not shared, so each web server manages its own cache, though the cache dependencies are the same across the 3 servers.
I don't really know where to start describing what/where it could be going wrong or the circumstances, because it's so strange to me.
I've seen this once before about 2 weeks ago, but it went away on its own after about an hour and I haven't seen it since. Has anyone else seen an error like this before?
Well, it seems your HomeController is missing a parameterless constructor. Are you sure it has one?
The default controller factory needs one to intialize your constructor.
Unless you are using your own ControllerFactory, in which case you need to register it during Application_Start:
protected void Application_Start()
{
//other code
ControllerBuilder.Current.SetControllerFactory(new MyCustomControllerFactory());
}
Assuming you're resolving controllers with structuremap, it may be your components are registered at the wrong place/time.
Are you registering components in Application_Start ?

error deploying asp.net mvc3 application with System.Data.Objects.ObjectSet

Maybe you can give a clue about this error, I have been googling and testing things for hours without result. I am using Mysql and Mysql connector 6.3.5. The error only happens on server, locally everything goes well.
The error:
Method not found:
'System.Data.Objects.ObjectSet`1
MyModelEntities.get_my_aspnet_Users()'.
StackTrace:
[MissingMethodException: Method not
found:
'System.Data.Objects.ObjectSet`1
MyModelEntities.get_my_aspnet_Users()'.]
Utils.GetCurrentUserId() +0
Project.Controllers.ApplicationController..ctor()
+70 Project.Controllers.HomeController..ctor()
+29
[TargetInvocationException: Exception
has been thrown by the target of an
invocation.]
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
'Project.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.b_2()
+49 System.Web.Mvc.<>c__DisplayClassb1.<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(Func1
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)
+184
A little bit more background info on your model would be needed to pinpoint the issue.
If I had to infer with only the stack trace... It seems that in your HomeController constructor there is a call to get_my_aspnet_Users on your model class - MyModelEntities. For some reason the code is not able to find that method.
Is it possible that you somehow have an older version of the model file that does not have the particular method your code is looking for?
Hope this helps.

Why doesn't my Unity DependencyResolver work on shared hosting but works locally?

I'm trying to deploy ASP.NET MVC 3 application wich uses Unity as a IoC container. Application works fine on local server, but when deployed it throws an exception: No parameterless constructor defined for this object. And this is thrown for a controller that should get some repository injected by my Unity DependencyResolver.
I've installed Unity with NuGet so it should be referenced directly, and I've checked that it gets copied to 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 might be the problem?
Edit 2:
I've solved the problem by adding references to Mvc libraries (System.Web.Mvc, etc.) in GAC instead of direct references.
Previously the application was referencing Mvc libraries directly and they were bin deployed to hosting server, because server had .net 4.0 and mvc 2 installed, but since hosting provider installed Mvc 3, I've changed to references to GAC. I guess it was some 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? Resource links would also be appreciated.
If Unity uses Reflection internally to construct classes and dependencies, then you will have an implicit dependency on your application being run in Full Trust.
Many shared hosting providers only allow up to Medium Trust, so it could be that Unity is being prevented from reflecting your code at runtime. You might be able to request that your application be run in Full Trust, but it depends on your hosting provider.
Some similar experiences from Rick Strahl's blog here
[EDIT]
Try testing locally by editing your web.config file:
<location allowOverride="false">
<system.web>
<!-- level="[Full|High|Medium|Low|Minimal]" -->
<trust level="Medium" originUrl=""/>
</system.web>
</location>

Resources