razorpage constructor injection does not work, while view injection does work - razor-pages

This is my code:
*.cshtml
#page
#model WebSite.Feature.Debugging.Areas.Debug.Pages.ListFeaturesModel
#{
}
<h1>Currently loaded features:</h1>
#foreach (var item in Model.Features)
{
<li>#item.Name</li>
}
Codebehind:
public class ListFeaturesModel : PageModel
{
public IEnumerable<IFeature> Features { get; }
public ListFeaturesModel(IEnumerable<IFeature> features)
{
Features = features;
}
public void OnGet()
{
}
}
The exception i get when navigating to the page:
Oddly enough if i use #inject in the cshtml it works just fine. Has anyone run into this issue before?
The razorpage is placed inside a dynamically loaded Razor class library.
Repo to reproduce: https://github.com/taori/Sandbox.git
Build whole solution, run web application, navigate to MyFeature/Page1

The underlying issue was that i was loading plugins through Assembly.LoadFrom, instead of AssemblyLoadContext.Default.FromPath - While Load from works fine most of the time it struggles in netcore scenarios where dependencies are an issue.
Unfortunately the exception i got wasn't helpful so further digging was necessary.

Related

Register ModelListener<JournalArticle> in Liferay 7

I'm trying to implement a ModelListener in Liferay 7. When I'm updating a JournalArticle in the UI, I would like to add a small behaviour and some logging.
#Component(immediate = true, service = ModelListener.class)
public class DownloadsListener extends BaseModelListener<JournalArticle> {
#Reference
private StructureService structureService;
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
private static final Log LOG = LogFactoryUtil.getLog(DownloadsListener.class);
#Override
public void onAfterUpdate(JournalArticle model) throws ModelListenerException {
LOG.error("UPDATES!");
LOG.error("UPDATES!");
super.onAfterUpdate(model);
if(structureService.isNieuwsArticle(model)) {
}
getFileFromURL("/documents/82177/0/file.pdf/0d10338c-8ca1-c5b7-cc4b-011bef1ee759");
}
private DLFileEntry getFileFromURL(String url) {
String[] splittedURL = StringUtil.split(url, '/');
return null;
}
}
But this code is never triggered when I'm updating an article. I'm not hitting my debug point in the onAfterUpdate method, and I'm not seeing any logging. The OSGI Module is deployed correctly. What am I missing?
The issue was that my ModelListener was not being correctly deployed in the OSGi container. (Although Eclipse / Apache Gogo told everything was fine).
OSGi could not resolve:
#Reference
private DLFileEntryLocalServiceUtil fileEntryLocalService;
Changed it to
#Reference
private DLFileEntryLocalService fileEntryLocalService;
Now the behaviour is correctly. It's a shame that there is ZERO feedback by Apache GoGo.

c# MVC Site Map - very slow when using roles - very slow

I've installed MVC Site Map Provider for MVC5 and just used everything out of the the box. It works fine. Now I want to implement roles based menu trimming so assuming my controller:
public class Home: Controller
{
[Authorize(Roles="Admin")]
public ActionResult Index()
{
return View();
}
}
Now basically only Admin role users can see the menu. Perfect works fine.
Also to implement this I added to my web.config this line:
<add key="MvcSiteMapProvider_SecurityTrimmingEnabled" value="true" />
The problem is that it works but it's slow. It takes about 7 seconds for the page to load. If I remove the web.config line, basically removing menu trimming based on roles it takes ~300ms for the page to load. Something is wrong in here.
Any ideas why my menu trimming based on roles is slow? I haven't done any customizations.
The security trimming feature relies on creating a controller instance for every node in order to determine if the current user context has access.
The most likely cause of this slowness is that your controllers (or their base class) have too much heavy processing happening in the constructor.
public class HomeController
{
public HomeController() {
// Lots of heavy processing
System.Threading.Thread.Sleep(300);
};
}
The above example will add 300 ms to the page load time for every node that represents an action method in the HomeController. If your other controllers also have heavy processing during instantiation, they will also add additional time to each page load.
When following DI best practices, this is not an issue because heavy processing takes place in external services after the controller instance is created.
public interface IHeavyProcessingService
{
IProcessingResult DoSomethingExpensive();
}
public class HeavyProcessingService : IHeavyProcessingService
{
public HeavyProcessingService() {
}
public IProcessingResult DoSomethingExpensive() {
// Lots of heavy processing
System.Threading.Thread.Sleep(300);
}
}
public class HomeController
{
private readonly IHeavyProcessingService heavyProcessingService;
// The constructor does no heavy processing. It is deferred until after
// the instance is created by HeavyProcessingService.
// The only thing happening here is assignment of dependencies.
public HomeController(IHeavyProcessingService heavyProcessingService) {
if (heavyProcessingService == null)
throw new ArgumentNullException("heavyProcessingService");
this.heavyProcessingService = heavyProcessingService;
};
public ActionResult Index()
{
var result = this.heavyProcessingService.DoSomethingExpensive();
// Do something with the result of the heavy processing
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
Notice in the above example that no heavy processing happens in the constructor? This means that creating an instance of HomeController is very cheap. It also means that action methods that don't require the heavy processing to happen (as in About() and Contact() in the example) won't take the hit of heavy processing required by Index().
If not using DI, MVC still requires that a new controller instance be created for each request (controller instances are never shared between users or action methods). Although, in that case it is not as noticeable on a per user basis because only 1 instance is created per user. Basically, MvcSiteMapProvider is slowing down because of a pre-existing issue with your application (which you can now fix).
Even if you are not using DI, it is still a best practice to defer heavy processing until after the controller instance is created.
public class HomeController
{
private readonly IHeavyProcessingService heavyProcessingService;
public HomeController() {
this.heavyProcessingService = new HeavyProcessingService();
};
public ActionResult Index()
{
var result = this.heavyProcessingService.DoSomethingExpensive();
// Do something with the result of the heavy processing
return View();
}
}
But if moving heavy processing into external services in your application is not an option, you can still defer processing until its needed by moving the processing into another method so it is not too expensive to create controller instances.
public class HomeController
{
public HomeController() {
};
private IProcessingResult DoSomethingExpensive() {
// Lots of heavy processing
System.Threading.Thread.Sleep(300);
}
public ActionResult Index()
{
var result = this.DoSomethingExpensive();
// Do something with the result of the heavy processing
return View();
}
}
Although there is a bug posted for Route values not preserved correctly in v4?
But looks like it was fixed in version 4 next release.
Another Workaround to fix this problem is cache here is a related article.
MVC siteMap provider cache

How to setup Autofac registration

Im new to dependency injection and Ive decided to use autofac as it seems to have the best 'out of the box' support for MVC5 (others might be better but im a newbie to this)
Im creating simple use scenarios and from the wiki ive got the following code in application_start in global.asax
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<ArtistController>().InstancePerHttpRequest();
builder.RegisterType<ArtistService>().As<IArtistService>().SingleInstance();
builder.RegisterType<ArtistRepository>().As<IArtistRepository>().SingleInstance();
builder.RegisterType<BandMemberRepository>().As<IBandMemberRepository>).SingleInstance();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
and in my ArtistController I have this
private IArtistService _artistService;
I then have some code the retrieves and updates data, all very simple. This works ok and Im starting to get my head around the whole concept.
My question is this, do I have to register all the concrete classes Im using manually ? My app could eventually grow and I would have many, many classes so this will be a pain to manage. I did come across this
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
which as far as Im aware should register everything for me but it didnt work. Am I doing something wrong ?
ok, thanks for the advice.
the autofac website shows an example using lambdas, so I added this in global.asax
builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces();
but that didnt work, any idea why ?
I do most (90+%) of my registrations by tagging them with this attribute:
[AttributeUsage(AttributeTargets.Class)]
[JetBrains.Annotations.MeansImplicitUse]
public class AutoRegisterAttribute : Attribute {}
Then I use this module to register those classes:
public class AutoRegisterModule : Module
{
private readonly Assembly[] _assembliesToScan;
public AutoRegisterModule(params Assembly[] assembliesToScan)
{
_assembliesToScan = assembliesToScan;
}
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(_assembliesToScan)
.Where(t => t.GetCustomAttribute<AutoRegisterAttribute>(false) != null)
.AsSelf()
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
}
public static AutoRegisterModule ForCallingAssembly()
{
return new AutoRegisterModule(Assembly.GetCallingAssembly());
}
}
So when I'm building my container, I typically just do:
builder.RegisterModule(AutoRegisterModule.ForCallingAssembly());

BeforeFeature/AfterFeature does not work using SpecFlow and Coded UI

I am not able to define a [BeforeFeature]/[AfterFeature] hook for my feature file. The application under test is WPF standalone desktop applications.
If I use [BeforeScenario]/[AfterScenario] everything works fine, the application starts without any problem, the designed steps are performed correctly and the app is closed.
Once I use the same steps with [BeforeFeature]/[AfterFeature] tags the application starts and the test fails with:
The following error occurred when this process was started: Object reference not set to an instance of an object.
Here is an example:
[Binding]
public class Setup
{
[BeforeScenario("setup_scenario")]
public static void BeforeAppScenario()
{
UILoader.General.StartApplication();
}
[AfterScenario("setup_scenario")]
public static void AfterAppScenario()
{
UILoader.General.CloseApplication();
}
[BeforeFeature("setup_feature")]
public static void BeforeAppFeature()
{
UILoader.General.StartApplication();
}
[AfterFeature("setup_feature")]
public static void AfterAppFeature()
{
UILoader.General.CloseApplication();
}
}
StartApplication/CloseApplication were recorded and auto-generated with Coded UI Test Builder:
public void StartApplication()
{
// Launch '%ProgramFiles%\...
ApplicationUnderTest Application = ApplicationUnderTest.Launch(this.StartApplicationParams.ExePath, this.StartApplicationParams.AlternateExePath);
}
public class StartApplicationParams
{
public string ExePath = "C:\\Program Files..."
public string AlternateExePath = "%ProgramFiles%\\..."
}
Noteworthy: I'm quite new with SpecFlow.
I can't figure it out why my test fails with [BeforeFeature] and works fine with [BeforeScenario].
It would be great if somebody could help me with this issue. Thanks!
I ran into a similar problem recently. Not sure if this can still help you, but it may be of use for people who stumble upon this question.
For BeforeFeature\AfterFeature to work, the feature itself needs to be tagged, tagging just specific scenarios will not work.
Your feature files should start like this:
#setup_feature
Feature: Name Of Your Feature
#setup_scenario
Scenario: ...

ServiceStack SelfHosted Site Default page

I have tried all the Razor self-hosted and the servicestack templates and in those projects it is possible to serve static html and cshtml if you have the razorFormat installed. I don't know what I am doing wrong and I have just copied code into new solutions and I cannot get html responses to work. I can get the metadata page always and the services work. I am not able to add default.htm and get it to work.
If I type in http://localhost:1337 I should get directed to the default page. Maybe I am missing something but I don't know what it is. If someone can give me a hand I would appreciate it greatly!
Here is my default.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
SOMETEXT
</body>
</html>
Here is my Program
class Program
{
[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
public class AppHost : AppHostHttpListenerBase {
public AppHost() : base("StarterTemplate HttpListener", typeof(HelloService).Assembly) {
}
public override void Configure(Funq.Container container) {
container.Register(new HelloService());
}
}
static void Main(string[] args)
{
var listeningOn = args.Length == 0 ? "http://*:1337/" : args[0];
var appHost = new AppHost();
appHost.Init();
appHost.Start(listeningOn);
Console.WriteLine("AppHost Created at {0}, listening on {1}", DateTime.Now, listeningOn);
Console.ReadKey();
}
}
Edit:
I should have added what the response I am getting is.
Handler for Request not found:
Request.HttpMethod: GET
Request.HttpMethod: GET
Request.PathInfo: /default.htm
Request.QueryString:
Request.RawUrl: /default.htm
One more thing that I tried was to put a request filter in at AppHostHttpListenerBase.Configure. I do not ever even hit that in my project but I do in the template projects.
EDIT:
I found out that the problem is that my Default.html and app.Config files are not getting moved to the bin/Debug directory. Has anyone experienced that problem?
Here is a good blog post on serving Razor pages for a self-hosted/console app. I don't think you need an app.config or web.config file, though I could be wrong.
The two things I think that you're missing:
1) Add Plugins.Add(new RazorFormat()) to the Configure method in your AppHost
2) Change 'Copy to Output Directory' value to 'Copy if newer' for your Razor files (this is within the Properties settings of the file). Also, change your default.htm file to default.cshtml.
For xamarin in OS X 'Copy to Output Directory' will definitely be a pain in the ass, because you should select this option on each file, and sometimes twice. So another option is create symbolic link
ln -s /path_to_project/assets /path_to_project/bin/Debug/assets
ln -s /path_to_project/Views /path_to_project/bin/Debug/Views
It's very useful for hundreds of files in assets and just a little bit easier for Views folder.
The same can be done on Linux hosting to hide selfhost.exe and *.dll in bin folder

Resources