Skip to content

Commit 8fc0f04

Browse files
Merge pull request MarimerLLC#1876 from dazinator/di-changes
Di changes
2 parents a2c2494 + 89b83f3 commit 8fc0f04

File tree

15 files changed

+95
-99
lines changed

15 files changed

+95
-99
lines changed

Source/Csla.AspNetCore.Shared/ApplicationContextManager.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ApplicationContextManager : IContextManager
2525
private const string _clientContextName = "Csla.ClientContext";
2626
private const string _globalContextName = "Csla.GlobalContext";
2727

28-
private readonly IServiceProvider _serviceProvider;
28+
private IServiceProvider _serviceProvider;
2929

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

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

161164
/// <summary>
162-
/// Gets the service provider scope
165+
/// Gets the service provider for current scope
163166
/// </summary>
164167
/// <returns></returns>
165-
public IServiceScope GetServiceProviderScope()
168+
public IServiceProvider GetServiceProvider()
166169
{
167-
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
170+
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
168171
}
169172

170173
/// <summary>
171-
/// Sets the service provider scope
174+
/// Sets the service provider for current scope
172175
/// </summary>
173-
/// <param name="scope">IServiceScope instance</param>
174-
public void SetServiceProviderScope(IServiceScope scope)
176+
/// <param name="scope">IServiceProvider instance</param>
177+
public void SetServiceProvider(IServiceProvider scope)
175178
{
176179
Csla.ApplicationContext.LocalContext["__sps"] = scope;
177180
}

Source/Csla.NetStandard2.0/Csla.NetStandard2.0.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<Import Project="..\Csla.Shared\Csla.Shared.projitems" Label="Shared" />
3838

39+
3940
<ItemGroup>
4041
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.9" />
4142
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.9" />
@@ -54,4 +55,5 @@
5455
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
5556
</ItemGroup>
5657

58+
5759
</Project>

Source/Csla.NetStandard2.0/Properties/Resources.Designer.cs

+15-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Csla.Shared/ApplicationContext.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Csla.Configuration;
1515
#if !NET40 && !NET45
1616
using Microsoft.Extensions.DependencyInjection;
17+
using Csla.Properties;
1718
#endif
1819

1920
namespace Csla
@@ -849,21 +850,15 @@ internal get
849850
/// Sets the service provider scope for this application context.
850851
/// </summary>
851852
#pragma warning disable CS3003 // Type is not CLS-compliant
852-
public static IServiceScope ServiceProviderScope
853+
public static IServiceProvider ServiceProviderScope
853854
#pragma warning restore CS3003 // Type is not CLS-compliant
854855
{
855856
internal get
856857
{
857-
var result = _contextManager.GetServiceProviderScope();
858-
if (result == null && DefaultServiceProvider != null)
859-
{
860-
var scopeFactory = DefaultServiceProvider.GetRequiredService<IServiceScopeFactory>();
861-
result = scopeFactory.CreateScope();
862-
ServiceProviderScope = result;
863-
}
864-
return result;
858+
var result = _contextManager.GetServiceProvider();
859+
return result;
865860
}
866-
set => _contextManager.SetServiceProviderScope(value);
861+
set => _contextManager.SetServiceProvider(value);
867862
}
868863

869864
/// <summary>
@@ -874,7 +869,7 @@ internal static IServiceProvider ScopedServiceProvider
874869
get
875870
{
876871
if (ServiceProviderScope != null)
877-
return ServiceProviderScope.ServiceProvider;
872+
return ServiceProviderScope;
878873
else
879874
return null;
880875
}

Source/Csla.Shared/Configuration/Fluent/CslaConfiguration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public CslaConfiguration PropertyInfoFactory(string typeName)
160160
/// </summary>
161161
/// <param name="scope">IServiceScope instance</param>
162162
#pragma warning disable CS3001 // Argument type is not CLS-compliant
163-
public CslaConfiguration ServiceProviderScope(IServiceScope scope)
163+
public CslaConfiguration ServiceProviderScope(IServiceProvider scope)
164164
#pragma warning restore CS3001 // Argument type is not CLS-compliant
165165
{
166166
ApplicationContext.ServiceProviderScope = scope;

Source/Csla.Shared/Core/ApplicationContextManager.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,22 @@ public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
170170

171171
#if !NET40 && !NET45
172172
/// <summary>
173-
/// Gets the service provider scope
173+
/// Gets the service provider for current scope
174174
/// </summary>
175175
/// <returns></returns>
176176
#pragma warning disable CS3002 // Return type is not CLS-compliant
177-
public IServiceScope GetServiceProviderScope()
177+
public IServiceProvider GetServiceProvider()
178178
#pragma warning restore CS3002 // Return type is not CLS-compliant
179179
{
180-
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
180+
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
181181
}
182182

183183
/// <summary>
184-
/// Sets the service provider scope
184+
/// Sets the service provider for current scope
185185
/// </summary>
186-
/// <param name="scope">IServiceScope instance</param>
186+
/// <param name="scope">IServiceProvider instance</param>
187187
#pragma warning disable CS3001 // Argument type is not CLS-compliant
188-
public void SetServiceProviderScope(IServiceScope scope)
188+
public void SetServiceProvider(IServiceProvider scope)
189189
#pragma warning restore CS3001 // Argument type is not CLS-compliant
190190
{
191191
Csla.ApplicationContext.LocalContext["__sps"] = scope;

Source/Csla.Shared/Core/ApplicationContextManagerTls.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@ public void SetDefaultServiceProvider(IServiceProvider serviceProvider)
151151

152152
#if !NET40 && !NET45
153153
/// <summary>
154-
/// Gets the service provider scope
154+
/// Gets the service provider for current scope
155155
/// </summary>
156156
/// <returns></returns>
157157
#pragma warning disable CS3002 // Return type is not CLS-compliant
158-
public IServiceScope GetServiceProviderScope()
158+
public IServiceProvider GetServiceProvider()
159159
#pragma warning restore CS3002 // Return type is not CLS-compliant
160160
{
161-
return (IServiceScope)ApplicationContext.LocalContext["__sps"];
161+
return (IServiceProvider)ApplicationContext.LocalContext["__sps"] ?? GetDefaultServiceProvider();
162162
}
163163

164164
/// <summary>
165-
/// Sets the service provider scope
165+
/// Sets the service provider for current scope
166166
/// </summary>
167-
/// <param name="scope">IServiceScope instance</param>
167+
/// <param name="scope">IServiceProvider instance</param>
168168
#pragma warning disable CS3001 // Argument type is not CLS-compliant
169-
public void SetServiceProviderScope(IServiceScope scope)
169+
public void SetServiceProvider(IServiceProvider scope)
170170
#pragma warning restore CS3001 // Argument type is not CLS-compliant
171171
{
172172
Csla.ApplicationContext.LocalContext["__sps"] = scope;

Source/Csla.Shared/Core/IContextManager.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ public interface IContextManager
7272
void SetDefaultServiceProvider(IServiceProvider serviceProvider);
7373
#if !NET40 && !NET45
7474
/// <summary>
75-
/// Gets the service provider scope
75+
/// Gets the service provider scope for current scope.
7676
/// </summary>
7777
#pragma warning disable CS3002 // Return type is not CLS-compliant
78-
IServiceScope GetServiceProviderScope();
78+
IServiceProvider GetServiceProvider();
7979
#pragma warning restore CS3002 // Return type is not CLS-compliant
8080
/// <summary>
81-
/// Sets the service provider scope
81+
/// Sets the service provider for current scope.
8282
/// </summary>
83-
/// <param name="scope">IServiceScope instance</param>
83+
/// <param name="scope">IServiceProvider instance</param>
8484
#pragma warning disable CS3001 // Argument type is not CLS-compliant
85-
void SetServiceProviderScope(IServiceScope scope);
85+
void SetServiceProvider(IServiceProvider scope);
8686
#pragma warning restore CS3001 // Argument type is not CLS-compliant
8787
#endif
8888
}

Source/Csla.Shared/Reflection/ServiceProviderMethodCaller.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ public static ServiceProviderMethodInfo FindDataPortalMethod<T>(Type targetType,
244244
{
245245
resultingMethod = FindDataPortalMethod<T>(baseType, criteria, throwOnError);
246246
}
247-
catch(TargetParameterCountException ex)
247+
catch (TargetParameterCountException ex)
248248
{
249249
throw new TargetParameterCountException(cacheKey, ex);
250250
}
251-
catch(AmbiguousMatchException ex)
251+
catch (AmbiguousMatchException ex)
252252
{
253253
throw new AmbiguousMatchException($"{targetType.FullName}.[{typeOfOperation.Name.Replace("Attribute", "")}]{GetCriteriaTypeNames(criteria)}.", ex);
254254
}
@@ -403,7 +403,7 @@ public static async Task<object> CallMethodTryAsync(object obj, ServiceProviderM
403403
int index = 0;
404404
int criteriaIndex = 0;
405405
#if !NET40 && !NET45
406-
var service = ApplicationContext.ScopedServiceProvider;
406+
var service = ApplicationContext.ScopedServiceProvider;
407407
#endif
408408
foreach (var item in method.Parameters)
409409
{
@@ -412,6 +412,8 @@ public static async Task<object> CallMethodTryAsync(object obj, ServiceProviderM
412412
#if !NET40 && !NET45
413413
if (service != null)
414414
plist[index] = service.GetService(item.ParameterType);
415+
//throw new InjectException(obj.GetType().Name + "." + info.Name + " " + Resources.MethodInjectFailed);
416+
415417
#endif
416418
}
417419
else

Source/Csla.Shared/Server/DataPortal.cs

-12
Original file line numberDiff line numberDiff line change
@@ -744,18 +744,6 @@ private void SetContext(DataPortalContext context)
744744

745745
private void ClearContext(DataPortalContext context)
746746
{
747-
#if !NET40 && !NET45
748-
if (_oldLocation == ApplicationContext.LogicalExecutionLocations.Client)
749-
{
750-
var scope = ApplicationContext.ServiceProviderScope;
751-
if (scope != null)
752-
{
753-
ApplicationContext.ServiceProviderScope = null;
754-
scope.Dispose();
755-
}
756-
}
757-
#endif
758-
759747
ApplicationContext.SetLogicalExecutionLocation(_oldLocation);
760748
// if the dataportal is not remote then
761749
// do nothing

0 commit comments

Comments
 (0)