How to separate asp.net core mvc project into multiple assembly (.dll)?
I have 3 projects
HR Project
ACC Project
I want to compile into dll
I want to add reference those dll (HR.dll, HR.Views.dll, ACC.dll, ACC.Views.dll) into MyApp Project.
And then run MyApp project can access Employee & Chart Account module too.
It shouldn't! That's why it's designed in a modular way. In most web applications out there, we version and deploy all these assemblies (Web, BLL and DAL) together. So, separating a project into 3 projects does not add any values.
An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.
If you want to do this you'll need to do the following 2 steps:
There is already a solution for this on stackoverflow: How to use a controller in another assembly in ASP.NET Core MVC 2.0?
It says the following:
Inside the
ConfigureServices
method of theStartup
class you have to call the following:services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
There is also already a solution for this on stackoverflow:
How CompiledRazorAssemblyPart should be used to load Razor Views?
Loading Razor Class Libraries as plugins
This is one possible solution from the links above:
What you need to do is:
services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .ConfigureApplicationPartManager(ConfigureApplicationParts);
and configure the parts like this
private void ConfigureApplicationParts(ApplicationPartManager apm) { var rootPath = HostingEnvironment.ContentRootPath; var pluginsPath = Path.Combine(rootPath, "Plugins"); var assemblyFiles = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories); foreach (var assemblyFile in assemblyFiles) { try { var assembly = Assembly.LoadFile(assemblyFile); if (assemblyFile.EndsWith(".Views.dll")) apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly)); else apm.ApplicationParts.Add(new AssemblyPart(assembly)); } catch (Exception e) { } } }
If you also have javascript and css files in our separate MVC projects, you will need to embed this to your dll otherwise your main app wont see it. So in your HR and ACC project, you'll need to add this in your .csproj file:
<ItemGroup>
<EmbeddedResource Include="wwwroot\**" />
</ItemGroup>
And just to be clear, I agree with the other comments, I don't think it is a good architecture, but it is possible to do it if you want.
Just a step:
Edit Startup.cs in ConfigureServices method
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).ConfigureApplicationPartManager(ConfigureApplicationParts);
and method:
private void ConfigureApplicationParts(ApplicationPartManager apm)
{
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assemblyFiles = Directory.GetFiles(rootPath , "*.dll");
foreach (var assemblyFile in assemblyFiles)
{
try
{
var assembly = Assembly.LoadFile(assemblyFile);
if (assemblyFile.EndsWith(this.GetType().Namespace + ".Views.dll") || assemblyFile.EndsWith(this.GetType().Namespace + ".dll"))
continue;
else if (assemblyFile.EndsWith(".Views.dll"))
apm.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
else
apm.ApplicationParts.Add(new AssemblyPart(assembly));
}
catch (Exception e) { }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With