Dependency Injection fo-dicom v5 ASP.NET DI Container and DI additional objects into IDicomServiceProvider #1774
-
Hi All, Firstly to all contributors, Bravo! what a terrific library you have developed. I have never used this library but in a matter of an hour I had something up and working. I am wanting to go beyond the Proof of concept stage and host fo-dicom v5.x in an ASP.NET dotnet8 microservice. I am however really struggling with the dependency injection configuration. Can anyone point me to where I am going wrong??? Any assistance is greatly appreciated Here is my builder.Services
.AddFellowOakDicom()
.AddImageManager<ImageSharpImageManager>();
var app = builder.Build();
DicomSetupBuilder.UseServiceProvider(app.Services); Now in my Hosted Service. This is where I am "Creating" my DICOM Server. Am I correct to still use the static method using FellowOakDicom.Network;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.Options;
namespace API.HostedServices;
public class CStoreScpAndCEchoScpHostedService : IHostedService
{
private readonly ILogger<CStoreScpAndCEchoScpHostedService> _logger;
private IDicomServer _dicomServer;
private readonly IOptions<CStoreScpAndCEchoScpHostedServiceOptions> _options;
public CStoreScpAndCEchoScpHostedService(
IOptions<CStoreScpAndCEchoScpHostedServiceOptions> options,
ILogger<CStoreScpAndCEchoScpHostedService> logger)
{
_logger = logger
?? throw new ArgumentNullException(nameof(logger));
_options = options
?? throw new ArgumentNullException(nameof(options));
}
public Task StartAsync(CancellationToken cancellationToken)
{
_dicomServer = DicomServerFactory.Create<CStoreScpAndCEchoScpProvider>
(
_options.Value.IPAddress,
_options.Value.DicomPort,
null,
System.Text.Encoding.UTF8, // Use UTF-8 as fallback encoding
_logger,
null,
_ => new DicomServerOptions()
{
MaxClientsAllowed = _options.Value.MaxClientsAllowed // Allow an unlimited number of clients
}
);
_logger.LogInformation("DICOM Service started.");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
// Stop the DICOM server
_dicomServer.Stop();
_dicomServer.Dispose();
_logger.LogInformation("DICOM Service stopped.");
return Task.CompletedTask;
}
} Here is my "DICOM Service" - please note the constructor as I have additional parameters in the constructor. Noteably an ef Core Database Context. and some configuration options using System.Collections.Generic;
using FellowOakDicom;
using FellowOakDicom.Network;
using Microsoft.Extensions.Options;
namespace API.DICOM;
/// <summary>
/// Represents a provider for the C-STORE DICOM service.
/// </summary>
public class CStoreScpAndCEchoScpProvider : DicomService, IDicomServiceProvider, IDicomCStoreProvider, IDicomCEchoProvider
{
private readonly MyDbContext _context;
private readonly ILogger<CStoreScpAndCEchoScpProvider> _logger;
private readonly IOptions<CStoreScpAndCEchoScpProviderOptions> _options;
public CStoreScpAndCEchoScpProvider(
INetworkStream stream,
Encoding fallbackEncoding,
ILogger<CStoreScpAndCEchoScpProvider> logger,
MyDbContext context,
IOptions<CStoreScpAndCEchoScpProviderOptions> options)
: base(stream, fallbackEncoding, logger, null)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_context = context ?? throw new ArgumentNullException(nameof(context));
_options = options ?? throw new ArgumentNullException(nameof(options));
_studies = new ConcurrentDictionary<string, (List<DicomDataset> dicomDataSets, Timer timer)>();
}
} As soon as I get an inbound connection my application crashes. System.InvalidOperationException: A suitable constructor for type 'API.DICOM.CStoreScpAndCEchoScpProvider' could not be located. Ensure the type is concrete and all parameters of public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& matchingParameterMap)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactoryInternal(Type instanceType, Type[] argumentTypes, ParameterExpression& provider, ParameterExpression& argumentArray, Expression& factoryExpressionBody)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes)
at FellowOakDicom.Network.DicomServer`1.CreateScp(INetworkStream stream)
at FellowOakDicom.Network.DicomServer`1.ListenForConnectionsAsync()
Disconnected client cleanup manually terminated
[00:30:36 INF] Disconnected client cleanup manually terminated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I suspect you are running into this issue: #1611 (comment) Can you check? |
Beta Was this translation helpful? Give feedback.
I suspect you are running into this issue: #1611 (comment)
Can you check?