Fixed multithread problem for aspnet core tenant resolvers.

pull/81/head
Halil İbrahim Kalkan 8 years ago
parent 8972ba8f69
commit ad80d628d3

@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class CookieTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
protected override string GetTenantIdFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
return httpContext.Request.Cookies[Options.TenantIdKey];
return httpContext.Request.Cookies[context.GetAspNetCoreMultiTenancyOptions().TenantIdKey];
}
}
}

@ -1,13 +1,32 @@
using Microsoft.AspNetCore.Http;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy;
using Volo.ExtensionMethods.Collections.Generic;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class HeaderTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
protected override string GetTenantIdFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
//TODO: Get first one if provided multiple values and write a log
return httpContext.Request.Headers[Options.TenantIdKey];
if (httpContext.Request.Headers.IsNullOrEmpty())
{
return null;
}
var tenantIdHeader = httpContext.Request.Headers[context.GetAspNetCoreMultiTenancyOptions().TenantIdKey];
if (tenantIdHeader == string.Empty || tenantIdHeader.Count < 1)
{
return null;
}
if (tenantIdHeader.Count > 1)
{
}
return tenantIdHeader.First();
}
}
}

@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.MultiTenancy;
using Volo.ExtensionMethods;
@ -8,12 +6,8 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
public abstract class HttpTenantResolverBase : ITenantResolver
{
protected AspNetCoreMultiTenancyOptions Options { get; private set; }
public virtual void Resolve(ITenantResolveContext context)
{
Options = context.ServiceProvider.GetRequiredService<IOptions<AspNetCoreMultiTenancyOptions>>().Value;
var httpContext = context.GetHttpContext();
if (httpContext == null)
{
@ -25,13 +19,13 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
private void ResolveFromHttpContext(ITenantResolveContext context, HttpContext httpContext)
{
var tenantId = GetTenantIdFromHttpContextOrNull(httpContext);
var tenantId = GetTenantIdFromHttpContextOrNull(context, httpContext);
if (!tenantId.IsNullOrEmpty())
{
context.Tenant = new TenantInfo(tenantId);
}
}
protected abstract string GetTenantIdFromHttpContextOrNull(HttpContext httpContext);
protected abstract string GetTenantIdFromHttpContextOrNull(ITenantResolveContext context,HttpContext httpContext);
}
}

@ -1,17 +1,18 @@
using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class QueryStringTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
protected override string GetTenantIdFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
if (!httpContext.Request.QueryString.HasValue)
{
return null;
}
return httpContext.Request.Query[Options.TenantIdKey];
return httpContext.Request.Query[context.GetAspNetCoreMultiTenancyOptions().TenantIdKey];
}
}
}

@ -1,14 +1,15 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class RouteTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
protected override string GetTenantIdFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
var tenantId = httpContext.GetRouteValue(Options.TenantIdKey);
var tenantId = httpContext.GetRouteValue(context.GetAspNetCoreMultiTenancyOptions().TenantIdKey);
if (tenantId == null)
{
return null;

@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public static class TenantResolveContextExtensions
{
public static AspNetCoreMultiTenancyOptions GetAspNetCoreMultiTenancyOptions(this ITenantResolveContext context)
{
return context.ServiceProvider.GetRequiredService<IOptionsSnapshot<AspNetCoreMultiTenancyOptions>>().Value;
}
}
}
Loading…
Cancel
Save