This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to IServiceProvidersFeature for RequestServices
- Loading branch information
Showing
3 changed files
with
182 additions
and
18 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/Microsoft.AspNet.Hosting/Internal/RequestServicesContainerFeature.cs
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
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(value)); | ||
} | ||
_appServices = value; | ||
} | ||
} | ||
|
||
public IServiceProvider RequestServices | ||
{ | ||
get | ||
{ | ||
if (!_requestServicesSet) | ||
{ | ||
_scope = ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
_requestServices = _scope.ServiceProvider; | ||
_requestServicesSet = true; | ||
} | ||
return _requestServices; | ||
} | ||
|
||
set | ||
{ | ||
_requestServicesSet = true; | ||
RequestServices = value; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_scope?.Dispose(); | ||
_scope = null; | ||
_requestServices = null; | ||
} | ||
} | ||
} |
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
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