-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedRealProxy.cs
More file actions
75 lines (70 loc) · 2.28 KB
/
ExtendedRealProxy.cs
File metadata and controls
75 lines (70 loc) · 2.28 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
using System;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.ServiceModel;
using System.Threading.Tasks;
namespace NetCoreStack.Wcf.Client
{
internal class ExtendedRealProxy<T> : RealProxy
{
private ExtendedChannelFactory<T> _factory;
private Type _proxyType;
private T _channel;
public ExtendedRealProxy(ExtendedChannelFactory<T> factory, Type proxyType, T channel)
: base(proxyType)
{
_factory = factory;
_proxyType = proxyType;
_channel = channel;
}
private async Task Handle(Task taskMethod, object channel)
{
await taskMethod;
}
/// <summary>
/// See <see cref="RealProxy.Invoke"/>
/// </summary>
public override IMessage Invoke(IMessage message)
{
var methodCall = message as IMethodCallMessage;
var methodInfo = methodCall.MethodBase as MethodInfo;
var success = false;
try
{
_channel = _factory.CreateProxy();
var result = methodInfo.Invoke(_channel, methodCall.InArgs);
success = true;
return new ReturnMessage(
result, // Operation result
null, // Out arguments
0, // Out arguments count
methodCall.LogicalCallContext, // Call context
methodCall); // Original message
}
catch (Exception e)
{
throw e;
}
finally
{
if (!success)
{
if (_channel is IClientChannel)
{
var clientChannel = (IClientChannel)_channel;
if (clientChannel != null) clientChannel.Abort();
}
}
else
{
if (_channel is IClientChannel)
{
var clientChannel = (IClientChannel)_channel;
if (clientChannel != null) clientChannel.Close();
}
}
}
}
}
}