I am trying to create an blazor assembly that should be installed as a nuget package.
The idea is to load the blazor pages as if they where on an endpoint route, and this should be done by extension methods on startup.cs (net5)/ program.cs (net6) only.
The regular solution is to add AdditionalAssemblies on Router component inside App.razor.
<Router
AppAssembly="typeof(Program).Assembly"
AdditionalAssemblies="new[] { typeof(Component1).Assembly }">
...
</Router>
Is there a way to achieve the same from an extension method called by startup.cs or program.cs?
Check my repo, shows different ways to load RCL to a project, the only limitation today is to register dynamically a DI from the component library
https://github.com/elgransan/BlazorPluginComponents
I added an example for loading a RCL as a Page dynamically
Context
I'm working on a 'plugin-like' system that runs as a windows service. The windows service creates a generic IHost (Let's call this the root Host) which I want to create as web server. The root Host will then look for plugin dll's to load, and it'll create individual IHost's for each plugin (call these child hosts).
Each of these child hosts can run their own kestrel server. I have it set up so that the root host server will act as a reverse proxy to these child host servers. This way only the root host server has to be exposed publicly.
I want to make it so both the root and child host servers can have Api Controllers and serve MVC razor pages.
My Question
It seems that razor pages cannot be used unless the project Sdk is either Microsoft.NET.Sdk.Web or Microsoft.NET.Sdk.Razor. It also seems like the Windows service project (One that starts the root host) has to specifically be Microsoft.NET.Sdk.Web or it wont add the needed application parts.
At the same time though, Microsoft says to use the Microsoft.NET.Sdk.Worker for Worker Service Template's. So I'm wondering what the consequences of using the Microsoft.NET.Sdk.Web sdk for a windows service project are? I assume it causes different MSBuild tasks to be setup, plus different DLL's to be loaded in, but is this stuff that can be done in other ways?
If it's not a good idea to use Microsoft.NET.Sdk.Web, would it be a smarter idea to move all the root host web server stuff to its own project? The windows service project would then just get the IHost from this project and then could go back to being a Microsoft.NET.Sdk.Worker project?
Edit
I ended up going with a restructure of my projects. It's now split up into 3 distinct projects instead of the 2 I originally had.
Core Project
The core project is just a standard Microsoft.NET.Sdk. It has an extension method for IHostBuilder called ConfigureHostRoot which adds the IHostedService's needed to look for and load the 'plugins'.
Http Project
This project is of the type Microsoft.NET.Sdk.Web. It has an extension method for IHostBuilder called ConfigureWebHostProxy which will add the proxy server. Because this project is the web sdk, it will allow for razor pages to be added.
Windows Service Project
I needed a third project to bring those 2 separate parts together. This is the job of the Windows Service. This project is of the type Microsoft.NET.Sdk.Worker, and is the only entry point for the entire system.
All it has is the standard Program.cs with a Main(). All the main does it builds the Host like this:
Host.CreateDefaultBuilder(args)
.ConfigureHostRoot(integrationBuilder =>
{
// Configure the Host root
})
.ConfigureWebHostProxy(proxyBuilder =>
{
// Configure the reverse proxy
});
Then it'll just build that host and run it for the lifetime of the windows service. Seems like this will work for me, and it means that my core project doesn't have to be a Microsoft.NET.Sdk.Web sdk which I like.
I still don't really know if there is any 'downside' to putting it all into the 'core' project and setting it's sdk to Microsoft.NET.Sdk.Web.
I am creating a Blazor WASM project and would like to separate my Blazor components and pages from the hosting model.
This would make it easy to change hosting model to for example a Server based application, where I just would need to create a new project and reference my components and pages library.
It would also allow me to separate my components and pages library from specific implementations (services or data models) by using interfaces and DI. Only the hosting project would need to know my implementation details.
The issue is that I cannot get it to work properly with visual studio. I have tried to create a .NET Standard 2.1 project for my components/pages but then I cannot create new razer components. Do I need to configure the project in some way? How should I make my shared components/pages library?
You need to select a Razor Class Library template:
It will contain a wwwroot directory where you can put your images, css or other static web resources. You can also use Library Manager (libman.json) if you add the json.
More information here: https://learn.microsoft.com/en-us/aspnet/core/blazor/class-libraries?view=aspnetcore-3.1&tabs=visual-studio
We have a big project developed in Asp.net MVC5. Our models and business logic are defined in separate class libraries. Now we need to add another module to an existing project but we want a separate dll.
This module also shares the most javascripts, css files and other files. That is the reason we don't want to separate MVC project.
Is there any why we can create separate dll for module basis. so we don't want deploy or touch other dlls.
From your description, you say that the projects share CSS and JS files. This leads me to believe you are talking about a separate MVC website (possibly part of the larger corporate website). This can be easiest with the use of Areas. If you are not familiar with Areas, please read the following: https://msdn.microsoft.com/en-us/library/ee671793(VS.100).aspx
Of course using Areas will require you to deploy the whole site everytime one of the areas change, and you have mentioned that you want to avoid doing so.
If you don't want to use areas, and instead want to create another MVC project in the same solution, you can do that easily too. You can right click on the solution, add new project > ASP.NET web application > MVC to add the project. To share JS and CSS files between these two MVC projects, you will have to create a new solution folder (right click solution > Add new solution folder), and move your resource files to that folder. Inside each MVC project in your solution, you will add existing items and select those js/css resource files. This way if you change the css file, it will be reflected in both the projects.
For more information, read the following:
How do you share scripts among multiple projects in one solution?
Yes you can, just add the logic classes to other class library project (you can have as many as you want), then add references of those class librarys to the mvc project.
Don't forget to import the classes after in your code
Edit: I'm assuming you are using Visual Studio, if yes, you can go to File -> Create Project, this will create another project in the same solution.
I don't know whether you tried with Managed Extensibility Framework (MEF) or not.. this framework works as you required ... I think below link will help you more
ASP.NET MVC Extensibility with MEF
How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API
http://www.codeproject.com/Articles/167321/MEF-with-ASP-NET-Hello-World
Other people have posted answers regarding the use of Areas. Areas are great and good and helpful. They really benefit the project structure.
This module also shares the most javascripts, css files and other file
The title of your question is about .dlls, but I suspect the client-side resources are the main concern.
If you consider your webapp as having two distinct parts: server-side and client-side, you can use appropriate strategies to modularize each. Areas a great for organizing server-side code, but don't help the front-end.
Front-end package management options have expanded for ASP.NET 5. In addition to the traditional NuGet package manager, Bower and NPM are now supported. For example, consider how this article demonstrates installing jQuery via NPM. Here's another good article about setting up NPM, Bower, and Gulp in Visual Studio.
What to do: Take your existing client-side code and make a custom NPM or Bower package, and then use that package from one or more Asp.NET projects.
I can suggest you two ways to organize your multi-module project.
Option 1 - Create Area per module, within same web project
One way to do it is, create separate Area within the same MVC project. So each module will have a separate area, with separate controllers, views, scripts etc. But,
(1) This will still create a single dll for the whole MVC project
(2) Sharing files across areas might not be very easy in some scenarios (you can keep all the scripts for all the modules in one shared directory though)
Option 2 - Create class library per module, merge after build
Another way is to create a single class library project per module. Add reference to the System.Web.Mvc and other libraries so that it can have controllers etc. Create your own views, scripts and other folders and populate with files as you need them.
Now, all your modules will build as separate projects, with the dll file and the javasvripts, htmls, csss, images etc. To make them all work as a single web application you can create a (only one) MVC web project, which will go to the IIS virtual directory and will be published as web.
To use all your separate modules from the same web, you can write post build events in all those libraries to copy the artifacts (dll, scripts etc.) into the main web, into corresponding folders (dll to \bin, javascript to \scripts etc.). So, after successful build, all the artifacts are available in the same web project, and that can be deployed as a single web with all the modules. Your post build scripts should look something like this
XCOPY "$(ProjectDir)$(OutDir)*.*" "$(ProjectDir)..\YourMainWebDirectory\Bin\" /Y
XCOPY "$(ProjectDir)Content" "$(ProjectDir)..\YourMainWebDirectory\Content\" /S /Y
XCOPY "$(ProjectDir)Scripts" "$(ProjectDir)..\YourMainWebDirectory\Scripts\" /S /Y
XCOPY "$(ProjectDir)Views" "$(ProjectDir)..\YourMainWebDirectory\Views\" /S /Y
XCOPY "$(ProjectDir)Images" "$(ProjectDir)..\YourMainWebDirectory\Images\" /S /Y
Now,
(1) You have separate dlls for separate modules
(2) Can directly share scripts and other files, as they will be in same location (after build)
(3) If you decide to remove a specific module from the web, just remove the post build event from that module (project) without affecting anything else. You can add that back at any time you please.
Your overall solution will look like
Module01.csproj => post build copy to main
\Controllers
\Scripts
\Views
\Contents
\Images
Module02.csproj => post build copy to main
\Controllers
\Scripts
\Views
\Contents
\Images
Models.csproj
\...
Application.csproj
\...
Main.Web.csproj => main web application hosted in IIS
\Controllers
\Scripts
\Views
\Contents
\Images
I have a custom MVC 5 solution separated into 3 main projects, Data, Admin, and Public.
I need to add .NET Identity and it's related utilities.
I have read articles showing how to add it to an existing MVC project and I think I can handle that (basically add the dependencies/files).
My question is:
Does it make sense to add a Security project and put the related Identity stuff in there and reference from Admin/Web projects?
Or should it reside in the Data project since that's already referenced and handles the data?
Either way, how to implement Identity across the two sites? The Identity will be modified to include extra info about the user so it might make sense to be a part of the Data project...?
Also, how do I implement the identity/security project so i don't need to add Identity to each project? (Architecture is where I really need the help here)
Basically, how would I implement security as a separate project using asp.net Identity?
When I build an app like this I typically am using Dependency Injection, and have a project that defines my services call it Core (perhaps in your case Data?). In this project I'll typically create a ISecurityService interface that defines methods needed to get the logged in user:
public interface ISecurityService {
string GetCurrentUserName();
}
You might want to return more than a string, an object etc.. Then in when asp.net project that defines the functionality for ASP.net Identity I simply create an ASPNetSecurityService that implements ISecurityService and wire this into my IOC / Dependency Injection system. That way any class that has a need to get this info Can just request a reference to ISecurityService and the IOC system will provide them the registered ASPNetSecurityService.
Alternatively you can use this same technique and place the ASP.net in a separate project if you wanted to, but by using this DI technique you can keep the Identity Stuff in asp.net mvc but still make use of the functionality from anywhere you like.
Does this help?
I did something similar this year and it works as I had anticipated. I have the ASP.Net Identity as a project of it's own. It has been customized as well for my company to handle all business logic/rules around authenticating users. I set it up as a NuGet package and can install it into any web project to handle authentication to a common user store. The consuming applications do not need to know any of the details about authentication. The developer just calls provided methods.
The way I started was I created a project from the default MVC template and made note of all the dependencies that identity needed. Then I included those dependencies in my custom NuGet package.
You can create your own NuGet packages using NuGet Package Explorer: https://npe.codeplex.com/
I checked my notes and here are all the dependencies the I wrote down (it's possible I overlooked one) that would need to be added to the NuGet Package:
NuGet Packages:
ASP.Net Identity Core
ASP.Net Identity Owin
ASP.Net Identity EntityFramework
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.Security.Facebook
Microsoft.Owin.Security.Google
Microsoft.Owin.Security.MicrosoftAccount
Microsoft.Owin.Security.Twitter
Add references:
System.Configuration
System.Web
System.Web.Helpers
Hope this helps!