-
Notifications
You must be signed in to change notification settings - Fork 311
Switch to using feature for RequestServices #369
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNet.Http.Features.Internal; | ||
using Microsoft.Framework.DependencyInjection; | ||
|
||
namespace Microsoft.AspNet.Hosting.Internal | ||
{ | ||
public class RequestServicesFeature : IServiceProvidersFeature, IDisposable | ||
{ | ||
private IServiceProvider _appServices; | ||
private IServiceProvider _requestServices; | ||
private IServiceScope _scope; | ||
private bool _requestServicesSet; | ||
|
||
public RequestServicesFeature(IServiceProvider applicationServices) | ||
{ | ||
if (applicationServices == null) | ||
{ | ||
throw new ArgumentNullException(nameof(applicationServices)); | ||
} | ||
|
||
ApplicationServices = applicationServices; | ||
} | ||
|
||
public IServiceProvider ApplicationServices | ||
{ | ||
get | ||
{ | ||
return _appServices; | ||
} | ||
set | ||
{ | ||
if (value == null) | ||
{ | ||
throw new ArgumentNullException(nameof(ApplicationServices)); | ||
} | ||
_appServices = value; | ||
} | ||
} | ||
|
||
public IServiceProvider RequestServices | ||
{ | ||
get | ||
{ | ||
if (!_requestServicesSet) | ||
{ | ||
_scope = ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be defensive since ApplicationServices is settable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah looks like it. Do we want to throw an exception if you try to set it to null, or just treat that as no-op and basically do nothing and leave RequestServices null if you set ApplicationServices to null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This behavior is totally undefined. To repro this you would have to set ApplicationServices to null then before ever touching RequestServices (I just know somebody is going to do something stupid and report a bug 😄). This works today because nothing is lazy. We could store the original application services and use that as a fallback but that might be perplexing. Throwing might be the most reasonable thing. Null isn't bad either so lets flip a coin. If you did this as a customer an exception might be the best since you'd want to know why instead of getting a null ref. On the other hand if you get RequestServices and then set ApplicationServices to null, should that throw? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any real scenario where we want to allow them to set ApplicationServices to null? If not, I can just un auto the property and throw for null sets... Won't we fall over all over the place if ApplicationServices is null? I don't think we are checking anywhere in our code today There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why anybody would set it to null unless they were trying to clear it out. Maybe if the old value was null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok well this is a subtle breaking change then, setting to null will throw an exception now... updated and added a test |
||
_requestServices = _scope.ServiceProvider; | ||
_requestServicesSet = true; | ||
} | ||
return _requestServices; | ||
} | ||
|
||
set | ||
{ | ||
_requestServicesSet = true; | ||
RequestServices = value; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_scope?.Dispose(); | ||
_scope = null; | ||
_requestServices = null; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be value not ApplicationServices afaik