i'm trying to inject stuff into a custom ViewPage (ModelViewPage, from MvcContrib)
public class ValidatedModelViewPage<T> : ModelViewPage<T> where T : class
{
public ValidatedModelViewPage(IEnumerable<IBehavior<IMemberElement>> elementBehaviors)
: base(elementBehaviors.ToArray()) { }
}
and my Autofac registrations look like this:
builder.RegisterCollection<IBehavior<IMemberElement>>().As<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<NotNullBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.Register<StringLenghBehavior>().MemberOf<IEnumerable<IBehavior<IMemberElement>>>();
builder.RegisterGeneric(typeof(ValidatedModelViewPage<>));
but i get this error when i try to access a view:
Compiler Error Message: CS1729: 'UKFS.Web.Views.ValidatedModelViewPage<UKFS.Data.Entities.Skadeanmälan>' does not contain a constructor that takes '0' arguments
Source Error:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
Line 198: string[] dependencies;
Source File: c:\Users\Carl\AppData\Local\Temp\Temporary ASP.NET Files\root\be9ddc15\a84d5058\App_Web_edit.aspx.b2d4184a.thgwih90.0.cs Line: 196
i were clueless, but then i looked at App_Web_edit.aspx.b2d4184a.thgwih90.0.cs and found this:
Line 190: public class views_skadeanmälan_edit_aspx : UKFS.Web.Views.ValidatedModelViewPage<Skadeanmälan>, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 191:
Line 192: private static bool #__initialized;
Line 193:
Line 194: private static object #__fileDependencies;
Line 195:
Line 196: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
Line 197: public views_skadeanmälan_edit_aspx() {
of course, the generated class views_skadeanmälan_edit_aspx inheritates from my UKFS.Web.Views.ValidatedModelViewPage, and when it tries to instance it with the default construct.. so can you solve it?
You won't be able to constructor inject into ViewPages because the aspx compiler generates an empty ctor and as it derives from your base-class, your base-class must also have a empty ctor.
I'd look for property injection instead, otherwise try to accomplish the following:
Find the place where viewpages are instantiated, and get the Autofact there to instantiate the page
get the aspx compiler to not generate the empty ctor
I don't know how to do those things, so I'd aim for property injection instead
Related
Severity Code Description Project Path File Line Column Suppression State
Error CS0102 The type 'IOnReceiverListenerImplementor' already contains a definition for 'LcdDisplayHandler'
Error says "Class which required to acccess is internal sealed"
[global::Android.Runtime.Register ("mono/com/idtechproducts/device/OnReceiverListenerImplementor")]
internal sealed partial class IOnReceiverListenerImplementor : global::Java.Lang.Object, IOnReceiverListener {
public EventHandler<LcdDisplayEventArgs> LcdDisplayHandler;
}
I need to change name of "LcdDisplayHandler" , Please any Suggestion appreciate.
We were getting a NullReferenceException from line 135 until I added the if statement on line 134.
It shouldn’t have been necessary because of the check on line 124.
I couldn’t reproduce it locally. But it happened every time on live for certain scenarios. This extension method is called from the SearchResultsViewModel constructor, but I wouldn’t think that would matter.
Am I missing something obvious?
This is how I call the extension method
ParseParentCategory(SearchKey)
This is how the SearchResultsViewModel is defined
Public Class SearchResultsViewModel
Inherits ViewModelBase
Public ReadOnly _ConstructedProperly As Boolean
Public Property SearchKey As String
Public Property ParentCategoryId As Integer = -1
Public Property ParentCategoryName As String
Public Sub New(Request As HttpRequestBase, pPartiallyPopulatedStupidInputModel As SearchResultsViewModel, pSearchBy As IProductsSearch.SearchBy)
I want to create instance of PerRequestResourceProvider using ninject InRequestScope:
public class PerRequestResourceProvider: IPerRequestResourceProvider
{
priavte readonly _perRequestResorceInstance;
public PerRequestResourceProvider()
{
_perRequestResorceInstance = new PerRequestResource();
}
public PerRequestResource GetResource()
{
return _perRequestResorceInstance;
}
}
public interface IPerRequestResourceProvider
{
PerRequestResource GetResource();
}
In my NinjectDependencyResolver:
.....
kernel.Bind<IPerRequestResourceProvider>.To<PerRequestResourceProvider>().InRequestScope();
I inject IPerRequestResourceProvider in few classes. But when I add breakpoint to PerRequestResourceProvider constructor I see that PerRequestResourceProvider is created three times during one request and not single per request. What's wrong?
Update: source code ttps://bitbucket.org/maximtkachenko/ninjectinrequestscope/src
There are two issues with your code:
Ninject is not getting initialized correctly.
You need one of the Ninject.MVCx packages (according to the MVC version you are using). To configure it correctly, see: http://github.com/ninject/ninject.web.mvc
You are injecting PerRequestResourceProvider (the class type), and not IPerRequestResourceProvider (the interface type) into HomeController, thus the .InRequestScope() defined on the IPerRequestResourceProvider binding is not taking any effect. Change the HomeController constructor to get the inteface type injected and you're good.
Ninject does not require bindings for instantiatable (non-abstract,..) classes. This is why it is not obvious when the wrong binding is used.
In a brand new MVC5 project, I have a single html helper:
public static IHtmlString Localized(this HtmlHelper html, string url)
{
return /* code here */
}
In an empty page, I attempt to call this:
#{ ViewBag.Title = "Home Page"; }
#Html.Localized("~/content/images/mobile/hero.png");
And I get this error:
CS0121: The call is ambiguous between the following methods or properties: 'EUCA.HtmlHelpers.Localized(System.Web.Mvc.HtmlHelper, string)' and 'EUCA.HtmlHelpers.Localized(System.Web.Mvc.HtmlHelper, string)'
How is one method conflicting with itself?
This question has been answered here:
The call is ambiguous between the following methods or properties (bug??)
App_Code causes the code to be compiled twice, which doesn't seem to cause a problem in some basic cases. Extension methods an edge case that does not work with App_Code.
I've started using MvcContrib's Portable Areas and everything works fine for the very simple views, but when I want to use a custom model in my view i get the error saying the namespace doesn't exist.
The view is set to be embedded as resource. And intellisense in the view recognizes the model just fine.
Does anybody have any idea what might cause the problem?
UPDATE
I think it might have to do with the fact that i'm using MEF to load the plugins. I had a similar problem when loading the controllers. I had to build a custom ControllerFactory that would look in the MEF Controllers list if no suitable controller was found by the default controllerfactory.
UPDATE 2
I managed to get rid of the error by providing the RazorBuildProvider with my MEF-loaded assemblies. However, now the view is not found anymore. If the view is not strongly typed it IS found.
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
foreach (var module in ExternalComponents)
{
provider.AssemblyBuilder.AddAssemblyReference(module.GetType().Assembly);
}
};
Source Code
The Model
namespace Isis.Plugins.TextArea.TextArea.Models
{
public class TextAreaModel
{
[Required(ErrorMessage = "Field is required")]
public string Message { get; set; }
}
}
The Controller:
namespace Isis.Plugins.TextArea.TextArea.Controllers
{
[Export(typeof(IController))]
public class IndexController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new TextAreaModel() { Message = "Hallow!" });
}
[HttpGet]
public ActionResult Editor()
{
return View(new TextAreaModel() { Message = "EDITOR CONTENT" });
}
}
}
The View
#model Isis.Plugins.TextArea.TextArea.Models.TextAreaModel
#Model.Message
The error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'Plugins' does not exist in the namespace 'Isis' (are you missing an assembly reference?)
Source Error:
Line 27:
Line 28:
Line 29: public class _Page_Areas_TextArea_Views_Index_Index_cshtml : System.Web.Mvc.WebViewPage<Isis.Plugins.TextArea.TextArea.Models.TextAreaModel> {
Line 30:
Line 31: #line hidden
I'm facing similar issue with MEF & razor view engine (trying similar approach you have described). When i load my strongly typed razor views, i get "are you missing an assembly/reference" error.
I tried deploying my assemblies under Bin but that didnt help either.
Only way to avoid it is to do a loadFrom assembly on RazorBuildProvider.
I couldn't find any documentation on RazorBuildProvider other than "not intended to be used directly from your code"
You're code snippet is quite interesting... can you please explain how it works? Where is this expected to be registered - at AppStart?
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
foreach (var module in ExternalComponents)
{
provider.AssemblyBuilder.AddAssemblyReference(module.GetType().Assembly);
}
};
Any clarity would be much appreciated...
I eventually decided to place all the plugins in the Bin directory instead of a custom Plugins directory. It's not the solution I was aiming for, but it works for now.