-
Notifications
You must be signed in to change notification settings - Fork 921
Expand file tree
/
Copy pathServiceProxyProvider.cs
More file actions
79 lines (75 loc) · 3.29 KB
/
ServiceProxyProvider.cs
File metadata and controls
79 lines (75 loc) · 3.29 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.DependencyResolution;
using Surging.Core.CPlatform.Routing;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Surging.Core.ProxyGenerator.Implementation
{
/// <summary>
/// 服务代理提供者,通过RoutePath调用服务时使用
/// </summary>
public class ServiceProxyProvider : IServiceProxyProvider
{
private readonly IServiceRouteProvider _serviceRouteProvider;
private readonly CPlatformContainer _serviceProvider;
public ServiceProxyProvider( IServiceRouteProvider serviceRouteProvider
, CPlatformContainer serviceProvider)
{
_serviceRouteProvider = serviceRouteProvider;
_serviceProvider = serviceProvider;
}
public async Task<T> Invoke<T>(IDictionary<string, object> parameters, string routePath)
{
var serviceRoute= await _serviceRouteProvider.GetRouteByPath(routePath.ToLower());
T result = default(T);
if (parameters.ContainsKey("serviceKey"))
{
var serviceKey = parameters["serviceKey"].ToString();
var proxy = ServiceResolver.Current.GetService<RemoteServiceProxy>(serviceKey);
if (proxy == null)
{
proxy = new RemoteServiceProxy(serviceKey.ToString(), _serviceProvider);
ServiceResolver.Current.Register(serviceKey.ToString(), proxy);
}
result = await proxy.Invoke<T>(parameters, serviceRoute.ServiceDescriptor.Id);
}
else
{
var proxy = ServiceResolver.Current.GetService<RemoteServiceProxy>();
if (proxy == null)
{
proxy = new RemoteServiceProxy(null, _serviceProvider);
ServiceResolver.Current.Register(null, proxy);
}
result = await proxy.Invoke<T>(parameters, serviceRoute.ServiceDescriptor.Id);
}
return result;
}
public async Task<T> Invoke<T>(IDictionary<string, object> parameters, string routePath, string serviceKey)
{
var serviceRoute = await _serviceRouteProvider.GetRouteByPath(routePath.ToLower());
T result = default(T);
if (!string.IsNullOrEmpty(serviceKey))
{
var proxy = ServiceResolver.Current.GetService<RemoteServiceProxy>(serviceKey);
if (proxy == null)
{
proxy = new RemoteServiceProxy(serviceKey, _serviceProvider);
ServiceResolver.Current.Register(serviceKey, proxy);
}
result = await proxy.Invoke<T>(parameters, serviceRoute.ServiceDescriptor.Id);
}
else
{
var proxy = ServiceResolver.Current.GetService<RemoteServiceProxy>();
if (proxy == null)
{
proxy = new RemoteServiceProxy(null, _serviceProvider);
ServiceResolver.Current.Register(null, proxy);
}
result = await proxy.Invoke<T>(parameters, serviceRoute.ServiceDescriptor.Id);
}
return result;
}
}
}