Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Di changes #1876

Merged
merged 9 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Source/Csla.AspNetCore.Shared/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ApplicationContextManager : IContextManager
private const string _clientContextName = "Csla.ClientContext";
private const string _globalContextName = "Csla.GlobalContext";

private readonly IServiceProvider _serviceProvider;
private IServiceProvider _serviceProvider;

/// <summary>
/// Creates an instance of the object, initializing it
Expand Down Expand Up @@ -146,7 +146,9 @@ public void SetGlobalContext(ContextDictionary globalContext)
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
return HttpContext?.RequestServices;
// on aspnet core we proactively detect request scope, before falling back to root application scope.
// this saves users from having to SetServiceProvider() at the start of every request.
return HttpContext?.RequestServices ?? _serviceProvider;
}

/// <summary>
Expand All @@ -155,23 +157,24 @@ public IServiceProvider GetDefaultServiceProvider()
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
/* ignore value - we get the one from HttpContext */
// Service provider to be used as fallback when there is no more specific scoped service provider available.
_serviceProvider = serviceProvider;
}

/// <summary>
/// Gets the service provider scope
/// Gets the service provider for current scope
/// </summary>
/// <returns></returns>
public IServiceScope GetServiceProviderScope()
public IServiceProvider GetServiceProvider()
{
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
}

/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope
/// </summary>
/// <param name="scope">IServiceScope instance</param>
public void SetServiceProviderScope(IServiceScope scope)
/// <param name="scope">IServiceProvider instance</param>
public void SetServiceProvider(IServiceProvider scope)
{
Csla.ApplicationContext.LocalContext["__sps"] = scope;
}
Expand Down
17 changes: 6 additions & 11 deletions Source/Csla.Shared/ApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Csla.Configuration;
#if !NET40 && !NET45
using Microsoft.Extensions.DependencyInjection;
using Csla.Properties;
#endif

namespace Csla
Expand Down Expand Up @@ -849,21 +850,15 @@ internal get
/// Sets the service provider scope for this application context.
/// </summary>
#pragma warning disable CS3003 // Type is not CLS-compliant
public static IServiceScope ServiceProviderScope
public static IServiceProvider ServiceProviderScope
#pragma warning restore CS3003 // Type is not CLS-compliant
{
internal get
{
var result = _contextManager.GetServiceProviderScope();
if (result == null && DefaultServiceProvider != null)
{
var scopeFactory = DefaultServiceProvider.GetRequiredService<IServiceScopeFactory>();
result = scopeFactory.CreateScope();
ServiceProviderScope = result;
}
return result;
var result = _contextManager.GetServiceProvider();
return result;
}
set => _contextManager.SetServiceProviderScope(value);
set => _contextManager.SetServiceProvider(value);
}

/// <summary>
Expand All @@ -874,7 +869,7 @@ internal static IServiceProvider ScopedServiceProvider
get
{
if (ServiceProviderScope != null)
return ServiceProviderScope.ServiceProvider;
return ServiceProviderScope;
else
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public CslaConfiguration PropertyInfoFactory(string typeName)
/// </summary>
/// <param name="scope">IServiceScope instance</param>
#pragma warning disable CS3001 // Argument type is not CLS-compliant
public CslaConfiguration ServiceProviderScope(IServiceScope scope)
public CslaConfiguration ServiceProviderScope(IServiceProvider scope)
#pragma warning restore CS3001 // Argument type is not CLS-compliant
{
ApplicationContext.ServiceProviderScope = scope;
Expand Down
12 changes: 6 additions & 6 deletions Source/Csla.Shared/Core/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,22 @@ public void SetDefaultServiceProvider(IServiceProvider serviceProvider)

#if !NET40 && !NET45
/// <summary>
/// Gets the service provider scope
/// Gets the service provider for current scope
/// </summary>
/// <returns></returns>
#pragma warning disable CS3002 // Return type is not CLS-compliant
public IServiceScope GetServiceProviderScope()
public IServiceProvider GetServiceProvider()
#pragma warning restore CS3002 // Return type is not CLS-compliant
{
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
}

/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope
/// </summary>
/// <param name="scope">IServiceScope instance</param>
/// <param name="scope">IServiceProvider instance</param>
#pragma warning disable CS3001 // Argument type is not CLS-compliant
public void SetServiceProviderScope(IServiceScope scope)
public void SetServiceProvider(IServiceProvider scope)
#pragma warning restore CS3001 // Argument type is not CLS-compliant
{
Csla.ApplicationContext.LocalContext["__sps"] = scope;
Expand Down
12 changes: 6 additions & 6 deletions Source/Csla.Shared/Core/ApplicationContextManagerTls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,22 @@ public void SetDefaultServiceProvider(IServiceProvider serviceProvider)

#if !NET40 && !NET45
/// <summary>
/// Gets the service provider scope
/// Gets the service provider for current scope
/// </summary>
/// <returns></returns>
#pragma warning disable CS3002 // Return type is not CLS-compliant
public IServiceScope GetServiceProviderScope()
public IServiceProvider GetServiceProvider()
#pragma warning restore CS3002 // Return type is not CLS-compliant
{
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
}

/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope
/// </summary>
/// <param name="scope">IServiceScope instance</param>
/// <param name="scope">IServiceProvider instance</param>
#pragma warning disable CS3001 // Argument type is not CLS-compliant
public void SetServiceProviderScope(IServiceScope scope)
public void SetServiceProvider(IServiceProvider scope)
#pragma warning restore CS3001 // Argument type is not CLS-compliant
{
Csla.ApplicationContext.LocalContext["__sps"] = scope;
Expand Down
10 changes: 5 additions & 5 deletions Source/Csla.Shared/Core/IContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public interface IContextManager
void SetDefaultServiceProvider(IServiceProvider serviceProvider);
#if !NET40 && !NET45
/// <summary>
/// Gets the service provider scope
/// Gets the service provider scope for current scope.
/// </summary>
#pragma warning disable CS3002 // Return type is not CLS-compliant
IServiceScope GetServiceProviderScope();
IServiceProvider GetServiceProvider();
#pragma warning restore CS3002 // Return type is not CLS-compliant
/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope.
/// </summary>
/// <param name="scope">IServiceScope instance</param>
/// <param name="scope">IServiceProvider instance</param>
#pragma warning disable CS3001 // Argument type is not CLS-compliant
void SetServiceProviderScope(IServiceScope scope);
void SetServiceProvider(IServiceProvider scope);
#pragma warning restore CS3001 // Argument type is not CLS-compliant
#endif
}
Expand Down
9 changes: 6 additions & 3 deletions Source/Csla.Shared/Reflection/ServiceProviderMethodCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,17 @@ public static async Task<object> CallMethodTryAsync(object obj, ServiceProviderM
int index = 0;
int criteriaIndex = 0;
#if !NET40 && !NET45
var service = ApplicationContext.ScopedServiceProvider;
var service = ApplicationContext.ScopedServiceProvider;
if(service == null)
dazinator marked this conversation as resolved.
Show resolved Hide resolved
{
throw new InvalidOperationException();
}
#endif
foreach (var item in method.Parameters)
{
if (method.IsInjected[index])
{
#if !NET40 && !NET45
if (service != null)
#if !NET40 && !NET45
plist[index] = service.GetService(item.ParameterType);
#endif
}
Expand Down
12 changes: 0 additions & 12 deletions Source/Csla.Shared/Server/DataPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,18 +744,6 @@ private void SetContext(DataPortalContext context)

private void ClearContext(DataPortalContext context)
{
#if !NET40 && !NET45
if (_oldLocation == ApplicationContext.LogicalExecutionLocations.Client)
{
var scope = ApplicationContext.ServiceProviderScope;
if (scope != null)
{
ApplicationContext.ServiceProviderScope = null;
scope.Dispose();
}
}
#endif

ApplicationContext.SetLogicalExecutionLocation(_oldLocation);
// if the dataportal is not remote then
// do nothing
Expand Down
26 changes: 12 additions & 14 deletions Source/Csla.Web.Shared/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public class ApplicationContextManager : IContextManager
private const string _clientContextName = "Csla.ClientContext";
private const string _globalContextName = "Csla.GlobalContext";

private IServiceProvider _defaultServiceProvider = null;

/// <summary>
/// Gets a value indicating whether this
/// context manager is valid for use in
/// the current environment.
/// </summary>
public bool IsValid
{
public bool IsValid
{
get { return HttpContext.Current != null; }
}

Expand Down Expand Up @@ -116,11 +118,7 @@ public void SetGlobalContext(ContextDictionary globalContext)
/// </summary>
public IServiceProvider GetDefaultServiceProvider()
{
IServiceProvider result;
result = (IServiceProvider)Csla.ApplicationContext.LocalContext["__dsp"];
if (result == null)
result = GetDefaultServiceProvider();
return result;
return _defaultServiceProvider;
}

/// <summary>
Expand All @@ -129,24 +127,24 @@ public IServiceProvider GetDefaultServiceProvider()
/// <param name="serviceProvider">IServiceProvider instance</param>
public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
{
Csla.ApplicationContext.LocalContext["__dsp"] = serviceProvider;
_defaultServiceProvider = serviceProvider;
}

#if !NET40 && !NET45
/// <summary>
/// Gets the service provider scope
/// Gets the service provider for current scope
/// </summary>
/// <returns></returns>
public IServiceScope GetServiceProviderScope()
public IServiceProvider GetServiceProvider()
{
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
}

/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope
/// </summary>
/// <param name="scope">IServiceScope instance</param>
public void SetServiceProviderScope(IServiceScope scope)
/// <param name="scope">IServiceProvider instance</param>
public void SetServiceProvider(IServiceProvider scope)
{
Csla.ApplicationContext.LocalContext["__sps"] = scope;
}
Expand Down
12 changes: 6 additions & 6 deletions Source/Csla.test/AppContext/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
}

/// <summary>
/// Gets the service provider scope
/// Gets the service provider for current scope
/// </summary>
/// <returns></returns>
public IServiceScope GetServiceProviderScope()
public IServiceProvider GetServiceProvider()
{
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
return (IServiceProvider)ApplicationContext.LocalContext["__sps"];
}

/// <summary>
/// Sets the service provider scope
/// Sets the service provider for current scope
/// </summary>
/// <param name="scope">IServiceScope instance</param>
public void SetServiceProviderScope(IServiceScope scope)
/// <param name="scope">IServiceProvider instance</param>
public void SetServiceProvider(IServiceProvider scope)
{
Csla.ApplicationContext.LocalContext["__sps"] = scope;
}
Expand Down