C
C#2y ago
SWEETPONY

❔ How to use new dll during app build?

I have a Configure method that runs during the start of the service. The method looks like this:
public class ServicesConfiguration
: IServicesConfiguration
{
public void Configure(
HostBuilderContext hostContext,
IServiceCollection services )
{
services
.AddArchiveClient()
.AddSegmentStorageDepartmentServiceClient()
.AddSegmentStorageSubjectServiceClient()
.AddSegmentStorageTerritoryServiceClient()
.AddSegmentStorageProductionCalendarServiceClient()
.AddSegmentStorageWorktimeServiceClient()

.AddSingleton<EventsNormalizerFactory>()
.AddSingleton<DataSetGenerator>()

.AddSingleton<IReportTemplate, ArbitraryPeriod.ReportTemplate>()
.AddSingleton<IReportTemplate, LateForWork.ReportTemplate>()
.AddSingleton<IReportTemplate, Monthly.ReportTemplate>()
.AddSingleton<IReportTemplate, VariablePeriod.ReportTemplate>()
.AddSingleton<IReportTemplate, Weekly.ReportTemplate>();
}
}
public class ServicesConfiguration
: IServicesConfiguration
{
public void Configure(
HostBuilderContext hostContext,
IServiceCollection services )
{
services
.AddArchiveClient()
.AddSegmentStorageDepartmentServiceClient()
.AddSegmentStorageSubjectServiceClient()
.AddSegmentStorageTerritoryServiceClient()
.AddSegmentStorageProductionCalendarServiceClient()
.AddSegmentStorageWorktimeServiceClient()

.AddSingleton<EventsNormalizerFactory>()
.AddSingleton<DataSetGenerator>()

.AddSingleton<IReportTemplate, ArbitraryPeriod.ReportTemplate>()
.AddSingleton<IReportTemplate, LateForWork.ReportTemplate>()
.AddSingleton<IReportTemplate, Monthly.ReportTemplate>()
.AddSingleton<IReportTemplate, VariablePeriod.ReportTemplate>()
.AddSingleton<IReportTemplate, Weekly.ReportTemplate>();
}
}
3 Replies
SWEETPONY
SWEETPONYOP2y ago
Used in another class, in another method, like this:
public class ServicesConfiguration
: IServicesConfiguration
{
public void Configure(
HostBuilderContext hostContext,
IServiceCollection services )
{
LoadReportPlugins( hostContext, services );

services
.AddFileManagerClient()
.AddHostedService<ReportsService>();
}

static private void LoadReportPlugins(
HostBuilderContext hostContext,
IServiceCollection services )
{
var reportFolderPath = Path.Combine(
Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ),
"plugins" );

if ( Directory.Exists( reportFolderPath ).IsFalse() )
{
hostContext
.RootLoggerGet()
?.Error(
"plugins directory does not exist at {filePath}",
reportFolderPath );

return;
}

foreach ( var reportAssemblyFile in Directory.EnumerateFiles(
path: reportFolderPath,
searchPattern: "Report.*.dll",
searchOption: SearchOption.AllDirectories ) )
try
{
hostContext.PluginDirectoryPathsGet()
.Add( Path.GetDirectoryName( reportAssemblyFile ) );

var reportAssembly = Assembly.LoadFrom( reportAssemblyFile );
foreach ( var servicesConfigurationType in reportAssembly.GetTypes()
.Where( type => typeof( IServicesConfiguration ).IsAssignableFrom( type ) ) )
Activator.CreateInstance( servicesConfigurationType )
.As<IServicesConfiguration>()
.Configure( hostContext, services );
}
catch ( Exception ex )
{
hostContext
.RootLoggerGet()
?.Error( ex, reportAssemblyFile );
}
}
}
public class ServicesConfiguration
: IServicesConfiguration
{
public void Configure(
HostBuilderContext hostContext,
IServiceCollection services )
{
LoadReportPlugins( hostContext, services );

services
.AddFileManagerClient()
.AddHostedService<ReportsService>();
}

static private void LoadReportPlugins(
HostBuilderContext hostContext,
IServiceCollection services )
{
var reportFolderPath = Path.Combine(
Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ),
"plugins" );

if ( Directory.Exists( reportFolderPath ).IsFalse() )
{
hostContext
.RootLoggerGet()
?.Error(
"plugins directory does not exist at {filePath}",
reportFolderPath );

return;
}

foreach ( var reportAssemblyFile in Directory.EnumerateFiles(
path: reportFolderPath,
searchPattern: "Report.*.dll",
searchOption: SearchOption.AllDirectories ) )
try
{
hostContext.PluginDirectoryPathsGet()
.Add( Path.GetDirectoryName( reportAssemblyFile ) );

var reportAssembly = Assembly.LoadFrom( reportAssemblyFile );
foreach ( var servicesConfigurationType in reportAssembly.GetTypes()
.Where( type => typeof( IServicesConfiguration ).IsAssignableFrom( type ) ) )
Activator.CreateInstance( servicesConfigurationType )
.As<IServicesConfiguration>()
.Configure( hostContext, services );
}
catch ( Exception ex )
{
hostContext
.RootLoggerGet()
?.Error( ex, reportAssemblyFile );
}
}
}
I can write a new dll, add a dependency -thats all, I can use the logic of this dll Question: is it possible to do all this without rebuild application? I just want to "catch" new dll, it registered itself and I could use it
Cracker
Cracker2y ago
You can use reflection
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server