-
Notifications
You must be signed in to change notification settings - Fork 277
/
IDisposableChannel.cs
executable file
·73 lines (61 loc) · 1.98 KB
/
IDisposableChannel.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace MTApiService
{
sealed class DisposableChannel<T> : IDisposable
{
T proxy;
bool disposed;
public DisposableChannel(T proxy)
{
if (!(proxy is ICommunicationObject)) throw new ArgumentException("object of type ICommunicationObject expected", "proxy");
this.proxy = proxy;
}
public T Service
{
get
{
if (disposed) throw new ObjectDisposedException("DisposableProxy");
return proxy;
}
}
public void Dispose()
{
if (!disposed)
{
Dispose(true);
}
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposing)
{
if (proxy != null)
{
ICommunicationObject ico = null;
if (proxy is ICommunicationObject)
ico = (ICommunicationObject)proxy;
// This state may change after the test and there's no known way to synchronize
// so that's why we just give it our best shot
if (ico.State == CommunicationState.Faulted)
ico.Abort(); // Known to be faulted
else
try
{
ico.Close(); // Attempt to close, this is the nice way and we ought to be nice
}
catch
{
ico.Abort(); // Sometimes being nice isn't an option
}
proxy = default(T);
}
}
disposed = true;
}
}
}