I was using my unit tests on .Net5(or lower) with DependencyResolverHelper like this below. This is my base test class
public abstract class BaseTestClass
{
protected DependencyResolverHelper _serviceProvider;
public BaseTestClass()
{
var webHost = WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.Build();
_serviceProvider = new DependencyResolverHelper(webHost);
}
}
and my DependencyResolverHelper
public class DependencyResolverHelper
{
private readonly IWebHost _webHost;
/// <inheritdoc />
public DependencyResolverHelper(IWebHost webHost) => _webHost = webHost;
public T GetService<T>()
{
var serviceScope = _webHost.Services.CreateScope();
var services = serviceScope.ServiceProvider;
try
{
var scopedService = services.GetRequiredService<T>();
return scopedService;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
but im bit confused with new .NET 6 Dependency things. Does anyone tried it without startup class?
I tried to change it with
WebApplicationBuilder
but it gave the
No service for type 'MinimalAPI.Services.TokenService.ITokenService' has been registered. error.
Just Because this part of codes .UseStartup<Startup>() called startup class and registed the services for you,If you try with WebApplicationBuilder in .net 6,You have to regist the services yourself,
I tried as below:
public void Test1()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { });
builder.Services.AddSingleton<ISomeService,SomeService>();
var app = builder.Build();
var serviceProvider = new DependencyResolverHelper(app);
var someservice = serviceProvider.GetService<ISomeService>();
Assert.Equal(typeof(SomeService), someservice.GetType());
}
DependencyResolverHelper class:
public class DependencyResolverHelper
{
private readonly WebApplication _app;
/// <inheritdoc />
public DependencyResolverHelper(WebApplication app) => _app = app;
public T GetService<T>()
{
var serviceScope = _app.Services.CreateScope();
var services = serviceScope.ServiceProvider;
try
{
var scopedService = services.GetRequiredService<T>();
return scopedService;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
Result:
Related
Based on that link I'm trying to create EasyNetQ Dispatcher for my messages. For some reason my Consumer is not triggered when my message appears in queue and I've no idea what the reason might be.
public class Program
{
static void Main(string[] args)
{
var config = LoadConfiguration();
ConfigureServices(config);
Console.ReadLine();
}
public static IConfiguration LoadConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return builder.Build();
}
private static void ConfigureServices(IConfiguration config)
{
var services = new ServiceCollection()
.AddSingleton(config)
.AddEasyNetQ("host=127.0.0.1:5672;username=guest;password=guest")
.AddSingleton<AutoSubscriber>(provider =>
{
// When I put breakpoint below - is never reached. Is that correct behavior?
var subscriber = new AutoSubscriber(provider.GetRequiredService<IBus>(), "SomePrefix")
{
AutoSubscriberMessageDispatcher = provider.GetRequiredService<IAutoSubscriberMessageDispatcher>()
};
subscriber.Subscribe(new Assembly[] { Assembly.GetExecutingAssembly() });
subscriber.SubscribeAsync(new Assembly[] { Assembly.GetExecutingAssembly() });
return subscriber;
});
services.BuildServiceProvider();
}
}
Below is the rest of nested code, though it seems working fine - so probably the problem is in Program.cs
EasyNetQExtension
public static class EasyNetQExtension
{
private static void InternalInitEasyNetQ(IServiceCollection service, string rabbitMqConnection)
{
service.AddSingleton(RabbitHutch.CreateBus(rabbitMqConnection));
service.AddSingleton<IAutoSubscriberMessageDispatcher, ConsumerMessageDispatcher>(serviceProvider => new ConsumerMessageDispatcher(serviceProvider));
var consumerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.IsClass && !x.IsAbstract && !x.IsInterface)
.Where(x => x.GetInterfaces().Any(t => t.Name == typeof(IConsume<>).Name));
foreach (var consumerType in consumerTypes)
{
service.AddTransient(consumerType);
}
// My consumer is found here, so this works properly
var consumerAsyncTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.IsClass && !x.IsAbstract && !x.IsInterface)
.Where(x => x.GetInterfaces().Any(t => t.Name == typeof(IConsumeAsync<>).Name));
foreach (var consumerAsyncType in consumerAsyncTypes)
{
service.AddTransient(consumerAsyncType);
}
}
public static IServiceCollection AddEasyNetQ(this IServiceCollection service, string rabbitMqConnectionString)
{
InternalInitEasyNetQ(service, rabbitMqConnectionString);
return service;
}
}
ConsumerMessageDispatcher
public class ConsumerMessageDispatcher : IAutoSubscriberMessageDispatcher
{
private readonly IServiceProvider _serviceProvider;
public ConsumerMessageDispatcher(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void Dispatch<TMessage, TConsumer>(TMessage message, CancellationToken cancellationToken = new CancellationToken()) where TMessage : class where TConsumer : class, IConsume<TMessage>
{
try
{
TConsumer consumer = _serviceProvider.GetRequiredService<TConsumer>();
consumer.Consume(message);
}
catch (Exception exception)
{
throw;
}
}
public async Task DispatchAsync<TMessage, TConsumer>(TMessage message, CancellationToken cancellationToken = new CancellationToken()) where TMessage : class where TConsumer : class, IConsumeAsync<TMessage>
{
try
{
TConsumer consumer = _serviceProvider.GetRequiredService<TConsumer>();
await consumer.ConsumeAsync(message);
}
catch (Exception exception)
{
throw;
}
}
}
First, you have to implement IConsumer<> interface in your console app, so that it can be registered via InternalInitEasyNetQ method.
using EasyNetQ.AutoSubscribe;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
namespace MyApp
{
public class ConsumeTextMessage : IConsume<string>
{
private readonly ILogger _logger;
public ConsumeTextMessage(ILogger<ConsumeTextMessage> logger)
{
_logger = logger;
}
public void Consume(string message, CancellationToken cancellationToken = default)
{
_logger.LogInformation("Logging the message: " + message);
Console.WriteLine("Reading the message: " + message);
}
}
}
Second, you are missing part where you write message using IAutoSubscriberMessageDispatcher. You can resolve IAutoSubscriberMessageDispatcher interface using IServiceProvider or dependency injection. Something along those lines:
var dispatcher = _provider.GetRequiredService<IAutoSubscriberMessageDispatcher>();
dispatcher.Dispatch<string, ConsumeTextMessage>("Dispatch my message - " + DateTime.Now);
I fixed that by moving Autosubscriber to separate method so the correct looks:
public class Program
{
static void Main(string[] args)
{
var config = LoadConfiguration();
var provider = ConfigureServices(config);
ConfigureConsumers(provider);
Console.ReadLine();
}
public static IConfiguration LoadConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
return builder.Build();
}
private static ServiceProvider ConfigureServices(IConfiguration configuration)
{
var services = new ServiceCollection()
.AddTransient<IEmailSender, EmailSender>()
.Configure<AuthMessageSenderOptions>(options => configuration.GetSection("SendGridEmailSettings").Bind(options))
.AddEasyNetQ(configuration["QueueConnectionData"]);
return services.BuildServiceProvider();
}
private static void ConfigureConsumers(ServiceProvider provider)
{
var autoSubscriber = new AutoSubscriber(provider.GetRequiredService<IBus>(), "SomePrefix")
{
AutoSubscriberMessageDispatcher = provider.GetRequiredService<IAutoSubscriberMessageDispatcher>()
};
autoSubscriber.Subscribe(new[] { Assembly.GetExecutingAssembly() });
autoSubscriber.SubscribeAsync(new[] { Assembly.GetExecutingAssembly() });
}
}
Consider my simple controller class where I want to use a logger (ILogger is coming from Castle in this case).
[RoutePrefix("api/orders")]
public class SignalController : ApiController
{
public ILogger Logger { get; set; } = new NullLogger();
// POST api/orders/update
[HttpPost, Route("update")]
public virtual void UpdateHandler(ChangeStateDto update)
{
this.Logger.Info($"Received ChangeStateDto with status {update.Status}");
}
}
Then I've got a self-hosted webhost, that looks like this.
public class WebHost
{
private readonly string url;
private IDisposable disposable;
private readonly ILifetimeScope scope;
public WebHost(string url, ILifetimeScope scope)
{
this.url = url;
this.scope = scope;
}
public ILogger Logger { get; set; } = new NullLogger();
// ...
public void Start()
{
try
{
this.Logger.Info($"Starting web host at {url}");
this.disposable = WebApp.Start(this.url, app =>
{
var config = new HttpConfiguration
{
DependencyResolver = new AutofacWebApiDependencyResolver(scope)
};
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
});
// ...
}
catch (Exception e)
{
// ...
this.Logger.Error(e.Message, e);
}
}
}
And this is the calling type
public class SomeCallerClass
{
public SomeCallerClass()
{
var webHostLogger = new SomeILoggerImplementation(this, "WebHost");
var builder = new ContainerBuilder();
builder.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
var container = builder.Build();
this.webHost = new WebHost("http://localhost:9000", container)
{
Logger = webHostLogger
};
}
}
Now the issue I am having is that property injection is not working for my SignalController type. It always holds a reference to the NullLogger instance. I just can't figure out why.
I solved it by using .RegisterType<> instead of .RegisterApiControllers
So the following works as expected. However I still don't understand why my first approach wasn't working.
var builder = new ContainerBuilder();
builder.RegisterType<SignalController>()
.WithProperty("Logger", webHostLogger)
.InstancePerRequest();
var container = builder.Build();
It probably wasn't working because it wasn't registering any controllers.
In your code you have:
builder
.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
However, you have to tell the registration extension which assemblies your controllers are in. You can see that in the examples shown in the docs.
Try:
builder
.RegisterApiControllers(Assembly.GetExecutingAssembly())
.WithProperty("Logger", webHostLogger);
I have been trying to setup my component instance per each tenant using InstancePerTenant. However, the InstancePerTenant somehow behave like a SingleInstance. Is there something wrong with my setup?
I have this ContainerBuilder extension which configure the multitenant related dependencies.
public static ContainerBuilder AddMultitenancy(this ContainerBuilder builder)
{
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
builder.RegisterType<TenantStore>().As<ITenantStore>().SingleInstance();
builder.RegisterType<TenantResolverStrategy>().As<ITenantIdentificationStrategy>().SingleInstance();
return builder
}
The tenant is identified by hostname:
public class TenantResolverStrategy : ITenantIdentificationStrategy
{
private readonly IHttpContextAccessor httpContextAccessor;
public TenantResolverStrategy(
IHttpContextAccessor httpContextAccessor
)
{
this.httpContextAccessor = httpContextAccessor;
}
public bool TryIdentifyTenant(out object tenantId)
{
// hostname is the tenantId
tenantId = httpContextAccessor.HttpContext?.Request?.Host.Value;
return (tenantId != null || tenantId == (object)"");
}
}
TenantStore is just a class to resolve the tenant entity from database based on the tenantId (hostname)
public class TenantStore : ITenantStore
{
private readonly ITenantIdentificationStrategy tenantIdentificationStrategy;
private readonly MemoryCacheStore cacheStore;
private readonly ITenantService tenantService;
public TenantStore(
ITenantIdentificationStrategy tenantIdentificationStrategy,
MemoryCacheStore cacheStore,
ITenantService tenantService
)
{
this.tenantIdentificationStrategy = tenantIdentificationStrategy;
this.cacheStore = cacheStore;
this.tenantService = tenantService;
}
public async Task<TenantEntity> GetTenantAsync(object tenantId)
{
var hostName = (string)tenantId;
var tenant = cacheStore.Get<TenantEntity>(CacheType.Tenant, hostName);
if (tenant == null)
{
tenant = await tenantService.GetTenantByHostNameFromDatabaseAsync(hostName);
cacheStore.Set(tenant, CacheType.Tenant, hostName);
}
return tenant ?? new TenantEntity();
}
}
In Startup.cs, I am registering TenantSpecific with InstancePerTenant:
public void ConfigureContainer(ContainerBuilder builder)
{
builder.AddMultitenancy();
builder.RegisterType<TenantSpecific>().As<ITenantSpecific>().InstancePerTenant();
}
public static MultitenantContainer ConfigureMultitenantContainer(IContainer container
{
var strategy = container.Resolve<ITenantIdentificationStrategy>();
var multitenantContainer = new MultitenantContainer(strategy, container);
// Nothing important here
multitenantContainer.RegisterMultitenantSpecificStuff();
return multitenantContainer;
}
TenantSpecific.cs and TenantSpecificController.cs:
public class TenantSpecific
{
public Guid Id { get; set; }
public TenantSpecific()
{
this.Id = Guid.NewGuid();
}
}
public class TenantSpecificController : ApiController
{
private readonly ITenantSpecific tenantSpecific;
public TenantSpecificController(ITenantSpecific tenantSpecific)
{
this.tenantSpecific = tenantSpecific;
}
[HttpGet]
public IActionResult Get()
{
return Ok(tenantSpecific.Id);
}
}
In Program.cs
public static IHostBuilder CreateHostBuilder(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(Startup.ConfigureMultitenantContainer))
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(ConfigurationModule.GetConfiguration())
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
});
return host;
}
When I invoke http://tenant1.localhost/tenant-specific/ and http://tenant2.localhost/tenant-specific, the constructor of TenantSpecific is only called once like Singleton. The tenantSpecific.Id returns the same value. So I assume InstancePerTenant is not working here.
Is there something wrong with my setup?
As written in the documentation ASP.net core multitenant support
You should add a call to AddAutofacMultitenantRequestServices() to add the required middleware to the root container which is required for multitenancy to work.
public void ConfigureServices(IServiceCollection services)
{
// This will all go in the ROOT CONTAINER and is NOT TENANT SPECIFIC.
services.AddMvc();
services.AddControllers();
// This adds the required middleware to the ROOT CONTAINER and
// is required for multitenancy to work.
services.AddAutofacMultitenantRequestServices();
}
Let's assume that we have the following Program.cs:
public static class Program
{
public static async Task Main(string[] args)
{
await CreateWebHostBuilder(args).Build().RunAsync();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost
.CreateDefaultBuilder(args)
.ConfigureKestrel(options => { options.AllowSynchronousIO = false; })
.ConfigureFanciness()
.ConfigureLogging(ConfigureApplicationLogging)
.UseStartup<Startup>();
}
private static void ConfigureApplicationLogging(WebHostBuilderContext context, ILoggingBuilder loggingBuilder)
{
var loggingConfiguration = context.Configuration.GetSection("Logging");
loggingBuilder.AddConfiguration(loggingConfiguration);
// var fancyService = SomehowGet<IFancyService>();
// if (fancyService.IsEnabled)
// loggingBuilder.AddEventLog(loggingConfiguration);
}
public static IWebHostBuilder ConfigureFanciness(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(delegate (WebHostBuilderContext context, IServiceCollection services)
{
var fancinessConfiguration = context.Configuration.GetSection("Fanciness");
services.Configure<FancinessSettings>(fancinessConfiguration);
services.AddSingleton<IFancyService, FancyService>();
// var fancyService = SomehowGet<IFancyService>();
// fancyService.Initialize();
});
}
}
And the following FancyService.cs:
public sealed class FancyService : IFancyService
{
private readonly ILogger<FancyService> logger;
private readonly IOptions<FancinessSettings> settings;
public FancyService(ILogger<FancyService> logger, IOptions<FancinessSettings> settings)
{
this.logger = logger;
this.settings = settings;
}
public bool IsEnabled { get; private set; }
public void Initialize()
{
// do the initialization work and set IsEnabled to some value
}
}
As demonstrated in the example files, loggingBuilder.AddEventLog(loggingConfiguration) depends on IFancyService.IsEnabled that is set when IFancyService.Initialize() is called.
To do that, I need access to an instance of IFancyService; is there a way to achieve this?
Once services.AddSingleton<IFancyService, FancyService>() has been called, you can get the instance of FancyService by building a ServiceProvider:
private static void ConfigureApplicationLogging(WebHostBuilderContext context, ILoggingBuilder loggingBuilder)
{
var fancyService = loggingBuilder.Services.BuildServiceProvider().GetService<IFancyService>();
fancyService.Initialize();
var loggingConfiguration = context.Configuration.GetSection("Logging");
loggingBuilder.AddConfiguration(loggingConfiguration);
if (fancyService.IsEnabled)
loggingBuilder.AddEventLog(loggingConfiguration);
}
As discussed in comments, if your FancyService has no constructor dependency on ILogger<> :
public sealed class FancyService : IFancyService
{
public FancyService(IOptions<FancinessSettings> settings)
{ ... }
to initialize the IFancyService, simply use an implementation factory to create a IFancyService instance:
public static IWebHostBuilder ConfigureFanciness(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(delegate (WebHostBuilderContext context, IServiceCollection services)
{
var fancinessConfiguration = context.Configuration.GetSection("Fanciness");
services.Configure<FancinessSettings>(fancinessConfiguration);
services.AddSingleton<IFancyService, FancyService>(sp =>{
var fancy=ActivatorUtilities.CreateInstance(sp,typeof(FancyService)) as FancyService;
fancy.Initialize();
return fancy;
});
});
}
The same trick could also be used to register a logger provider. Since an ILogger<> service depends on IEnumerable<ILoggerProvider>, we could register an ILoggerProvider instance that provides an additional INullLogger in order to configure the logging behavior according to the current IFancyService :
private static void ConfigureApplicationLogging(WebHostBuilderContext context, ILoggingBuilder loggingBuilder)
{
var loggingConfiguration = context.Configuration.GetSection("Logging");
var descriptor=ServiceDescriptor.Singleton<ILoggerProvider,NullLoggerProvider>(sp =>{
var provider = NullLoggerProvider.Instance;
var fancy = sp.GetRequiredService<IFancyService>();
if(fancy.IsEnabled)
{
loggingBuilder.AddDebug();
loggingBuilder.AddEventLog(loggingConfiguration);
loggingBuilder.AddEventSourceLogger();
// ... add more configuration as you like
}else{
loggingBuilder.AddConsole();
}
return provider;
});
loggingBuilder.Services.TryAddEnumerable(descriptor);
loggingBuilder.AddConfiguration(loggingConfiguration);
}
As a side note, be aware once the logger has been built, don't change the IFancyService.IsEnabled. That's because the ILogger<> service is a registered as a singleton and never changed once created.
I have plagiarized the below code from the mightysoft docs site on integration testing and adapted it slightly to meet my needs:
public class CustomWebApplicationFactory<TStartup>
: WebApplicationFactory<TStartup> where TStartup : class
{
private readonly SeedDataClass _seed;
public CustomWebApplicationFactory(SeedDataClass seed)
{
_seed = seed;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseEnvironment("Development");
builder.ConfigureServices(services =>
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddSingleton(_seed);
services.AddDbContextPool<GatewayContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(serviceProvider);
options.EnableSensitiveDataLogging();
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<GatewayContext>();
var logger = scopedServices
.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
var seed = scopedServices.GetRequiredService<SeedDataClass>();
db.Database.EnsureCreated();
try
{
seed.InitializeDbForTests(db);
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the database with test messages. Error: {ex.Message}");
}
}
});
}
}
To be used like in a test like:
_client = new CustomWebApplicationFactory<Startup>(new SeedDataClass()).CreateClient();
And this all works but I am looking to add generics to the custom web app factory class and move this code into a nuget package I am working on for internal testing work.
Something like this:
public class CustomWebApplicationFactory<TStartup, TContext>
: WebApplicationFactory<TStartup>
where TStartup : class
where TContext : DbContext
I am stuck on how to provide/inject the SeedDataClass class instance into my new generic custom web app factory.
If you are just trying to adapt a similar constructor to the former implementation of your CustomWebApplicationFactory<TStartup> class
_client = new CustomWebApplicationFactory<Startup>(new SeedDataClass()).CreateClient();
then your new constructor would look like so:
public class CustomWebApplicationFactory<TStartup, TContext> : WebApplicationFactory<TStartup>
where TStartup : class
where TContext : DbContext
{
private readonly SeedDataClass _seed;
public CustomWebApplicationFactory(SeedDataClass seed)
{
if (seed == null) throw new ArgumentNullException(nameof(seed));
_seed = seed;
}
}
and then update your call to the constructor like so
new CustomWebApplicationFactory<Startup, YourDbContext>(new SeedDataClass()).CreateClient();
This is where I was going with this:
Ammended factory:
public class GenericWebApplicationFactory<TStartup, TContext, TSeed>
: WebApplicationFactory<TStartup>
where TStartup : class
where TContext : DbContext
where TSeed : class, ISeedDataClass
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.UseEnvironment("Development");
builder.ConfigureServices(services =>
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddSingleton<ISeedDataClass,TSeed >();
services.AddDbContextPool<TContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(serviceProvider);
options.EnableSensitiveDataLogging();
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<TContext>();
var logger = scopedServices.GetRequiredService<ILogger<GenericWebApplicationFactory<TStartup, TContext, TSeed>>>();
var seeder = scopedServices.GetRequiredService<ISeedDataClass>();
db.Database.EnsureCreated();
try
{
seeder.InitializeDbForTests();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the database with test messages. Error: {ex.Message}");
}
}
});
}
}
Ammended usage:
_client = new GenericWebApplicationFactory<Startup, GatewayContext, SeedDataClass>().CreateClient();
With example seed class:
public interface ISeedDataClass
{
void InitializeDbForTests();
}
public class SeedDataClass : ISeedDataClass
{
private readonly GatewayContext _db;
public SeedDataClass(GatewayContext db)
{
_db = db;
}
public void InitializeDbForTests()
{
_db.Users.AddRange(
// add some users here
);
_db.SaveChanges(true);
}
}
Now, I can seed the in memory database however I see fit, per project where it is employed and my GenericWebApplicationFactory can now be pushed into a helper lib/nuget package which be re-used in other projects.