Repository navigation
Expand file tree
/
Copy pathBaseServiceClient.cs
More file actions
144 lines (121 loc) · 3.99 KB
/
Copy pathBaseServiceClient.cs
File metadata and controls
144 lines (121 loc) · 3.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Community.Community
File: BaseServiceClient.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Community
{
using System;
using System.ServiceModel;
using Ecng.Common;
using Ecng.Net;
/// <summary>
/// The base client for access to the WCF services.
/// </summary>
/// <typeparam name="TService">WCF service type.</typeparam>
public abstract class BaseServiceClient<TService> : Disposable
where TService : class
{
private readonly string _endpointName;
private readonly bool _hasCallbacks;
private TService _service;
private ChannelFactory<TService> _factory;
/// <summary>
/// Initialize <see cref="BaseCommunityClient{TService}"/>.
/// </summary>
/// <param name="address">Server address.</param>
/// <param name="endpointName">The access point name in the configuration file.</param>
/// <param name="hasCallbacks">Whether the <typeparamref name="TService" /> has events.</param>
protected BaseServiceClient(Uri address, string endpointName, bool hasCallbacks = false)
{
if (address == null)
throw new ArgumentNullException(nameof(address));
Address = address;
_endpointName = endpointName;
_hasCallbacks = hasCallbacks;
}
/// <summary>
/// Server address.
/// </summary>
public Uri Address { get; }
/// <summary>
/// Whether the connection has been established.
/// </summary>
public bool IsConnected { get; private set; }
/// <summary>
/// Create WCF channel.
/// </summary>
/// <returns>WCF channel.</returns>
protected virtual ChannelFactory<TService> CreateChannel()
{
return new ChannelFactory<TService>(new WSHttpBinding { Security = { Mode = SecurityMode.None }, UseDefaultWebProxy = true }, new EndpointAddress(Address));
}
/// <summary>
/// To connect. The connection is established automatically when the method <see cref="Invoke"/> or <see cref="Invoke{TService}"/> is called.
/// </summary>
public virtual void Connect()
{
_factory = ChannelHelper.Create(_endpointName, CreateChannel);
if (_hasCallbacks)
_service = _factory.CreateChannel();
OnConnect();
IsConnected = true;
}
/// <summary>
/// Connect.
/// </summary>
protected virtual void OnConnect()
{
}
/// <summary>
/// To call the service <typeparamref name="TService" /> method.
/// </summary>
/// <typeparam name="TResult">The result type returning the service method.</typeparam>
/// <param name="handler">The handler in which the method is called.</param>
/// <returns>The result returning the service method.</returns>
protected virtual TResult Invoke<TResult>(Func<TService, TResult> handler)
{
if (handler == null)
throw new ArgumentNullException(nameof(handler));
if (!IsConnected)
Connect();
return _hasCallbacks ? handler(_service) : _factory.Invoke(handler);
}
/// <summary>
/// To call the service <typeparamref name="TService" /> method.
/// </summary>
/// <param name="handler">The handler in which the method is called.</param>
protected void Invoke(Action<TService> handler)
{
if (handler == null)
throw new ArgumentNullException(nameof(handler));
Invoke<object>(srv =>
{
handler(srv);
return null;
});
}
/// <summary>
/// Release resources.
/// </summary>
protected override void DisposeManaged()
{
if (IsConnected)
{
((IDisposable)_factory).Dispose();
_factory = null;
_service = null;
IsConnected = false;
}
base.DisposeManaged();
}
}
}