-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from Cysharp/hotfix/DI
Add support for IServiceLocator / IServiceProvider Scope.
- Loading branch information
Showing
18 changed files
with
504 additions
and
105 deletions.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
src/MagicOnion.Hosting/MicrosoftExtensionsMagicOnionServiceActivator.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,15 @@ | ||
using System; | ||
using MagicOnion.Server; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace MagicOnion.Hosting | ||
{ | ||
public class MicrosoftExtensionsMagicOnionServiceActivator : IMagicOnionServiceActivator | ||
{ | ||
public T Create<T>(IServiceLocator serviceLocator) | ||
{ | ||
return ActivatorUtilities.GetServiceOrCreateInstance<T>((IServiceProvider)serviceLocator); | ||
} | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/MagicOnion.Hosting/MicrosoftExtensionsServiceLocator.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,63 @@ | ||
using MagicOnion.Server; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace MagicOnion.Hosting | ||
{ | ||
public class MicrosoftExtensionsServiceLocator : IServiceLocator, IServiceProvider | ||
{ | ||
readonly IServiceProvider serviceProvider; | ||
|
||
public MicrosoftExtensionsServiceLocator(IServiceProvider serviceProvider, MagicOnionOptions options) | ||
{ | ||
this.serviceProvider = serviceProvider; | ||
} | ||
|
||
public IServiceLocatorScope CreateScope() | ||
{ | ||
return new ServiceLocatorScope(serviceProvider.CreateScope()); | ||
} | ||
|
||
public T GetService<T>() | ||
{ | ||
return serviceProvider.GetService<T>(); | ||
} | ||
|
||
object IServiceProvider.GetService(Type serviceType) | ||
{ | ||
return serviceProvider.GetService(serviceType); | ||
} | ||
|
||
class ServiceLocatorScope : IServiceLocatorScope, IServiceLocator, IServiceProvider | ||
{ | ||
readonly IServiceScope scope; | ||
|
||
public IServiceLocator ServiceLocator => this; | ||
|
||
public ServiceLocatorScope(IServiceScope scope) | ||
{ | ||
this.scope = scope; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
scope.Dispose(); | ||
} | ||
|
||
object IServiceProvider.GetService(Type serviceType) | ||
{ | ||
return scope.ServiceProvider.GetService(serviceType); | ||
} | ||
|
||
public T GetService<T>() | ||
{ | ||
return scope.ServiceProvider.GetService<T>(); | ||
} | ||
|
||
public IServiceLocatorScope CreateScope() | ||
{ | ||
return new ServiceLocatorScope(scope.ServiceProvider.CreateScope()); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.