This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Contract; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
namespace Products.API.Controllers | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
using Contract; | |
namespace MyHost.Controllers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp3.0</TargetFramework> | |
<EnableDefaultContentItems>false</EnableDefaultContentItems> | |
</PropertyGroup> | |
<ItemGroup> | |
<Content Include="Plugins\*.dll"> | |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Contract; | |
using Prise.Infrastructure; | |
namespace HelloWorldPlugin | |
{ | |
[Plugin(PluginType = typeof(IHelloWorldPlugin))] | |
public class HelloWorldPlugin : IHelloWorldPlugin | |
{ | |
public string SayHello(string input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Contract | |
{ | |
public interface IHelloWorldPlugin | |
{ | |
string SayHello(string input); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IServiceCollection UseConnectionPerTenant(this IServiceCollection services, IConfiguration configuration) | |
{ | |
services.AddScoped<ProductsDbContext>((serviceProvider) => | |
{ | |
var tenant = serviceProvider.GetRequiredService<TenantInfo>(); // Get from parent service provider (ASP.NET MVC Pipeline) | |
var connectionString = configuration.GetConnectionString(tenant.Name); | |
var options = new DbContextOptionsBuilder<ProductsDbContext>() | |
.UseSqlServer(connectionString) | |
.Options; | |
var context = new ProductsDbContext(options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data.Common; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.EntityFrameworkCore.Diagnostics; | |
namespace efcore_tenancy.Infrastructure | |
{ | |
public class DatabaseInterceptor : DbCommandInterceptor | |
{ | |
private readonly TenantInfo tenantInfo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using efcore_tenancy.Data; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Diagnostics; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace efcore_tenancy.Infrastructure | |
{ | |
public static class ServiceCollectionExtensions | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static IServiceCollection UseEFInterceptor<T>(this IServiceCollection services, IConfiguration configuration) | |
where T : class, IInterceptor | |
{ | |
return services | |
.AddScoped<DbContextOptions>((serviceProvider) => | |
{ | |
var tenant = serviceProvider.GetRequiredService<TenantInfo>(); | |
var efServices = new ServiceCollection(); | |
efServices.AddEntityFrameworkSqlServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace efcore_tenancy.Infrastructure | |
{ | |
public class TenantInfoMiddleware | |
{ | |
private readonly RequestDelegate _next; |