forked from NetCoreStack/Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyHelper.cs
More file actions
34 lines (29 loc) · 1.2 KB
/
Copy pathProxyHelper.cs
File metadata and controls
34 lines (29 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.Extensions.DependencyInjection;
using NetCoreStack.Contracts;
using System;
using System.Reflection;
namespace NetCoreStack.Proxy.Internal
{
internal static class ProxyHelper
{
internal static TProxy CreateProxy<TProxy>(IServiceProvider container) where TProxy : IApiContract
{
object proxy = DispatchProxyAsync.Create<TProxy, HttpDispatchProxy>();
var proxyContextFilter = container.GetRequiredService<IProxyContextFilter>();
var proxyContext = new ProxyContext(typeof(TProxy));
var cultureFactory = container.GetService<ICultureFactory>();
if (cultureFactory != null)
{
proxyContext.CultureFactory = cultureFactory.Invoke;
}
proxyContextFilter.Invoke(proxyContext);
var initializer = proxy.GetType().GetMethod(nameof(HttpDispatchProxy.Initialize));
if (initializer == null)
{
throw new ArgumentNullException(nameof(initializer));
}
initializer.Invoke(proxy, new[] { proxyContext, (object)container.GetService<IProxyManager>() });
return (TProxy)proxy;
}
}
}