Skip to content

Commit 259b43e

Browse files
committed
update code
1 parent 7c51535 commit 259b43e

File tree

7 files changed

+71
-88
lines changed

7 files changed

+71
-88
lines changed

framework/wcf/Extensibility/Instancing/Lifetime/CS/client/Client.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
namespace Microsoft.ServiceModel.Samples
88
{
99
[ServiceContract(SessionMode = SessionMode.Required)]
10-
interface IEchoService
10+
internal interface IEchoService
1111
{
1212
[OperationContract]
1313
string Echo(string value);
1414
}
1515

1616
public static class CustomHeader
1717
{
18-
public static readonly string HeaderName = "InstanceId";
19-
public static readonly string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
18+
public const string HeaderName = "InstanceId";
19+
public const string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
2020
}
2121

22-
class Program
22+
internal class Program
2323
{
2424
static void Main(string[] args)
2525
{

framework/wcf/Extensibility/Instancing/Lifetime/CS/extensions/CustomLeaseExtension.cs

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.ServiceModel.Samples
1414
{
15-
interface ICustomLease
15+
internal interface ICustomLease
1616
{
1717
bool IsIdle { get; }
1818
InstanceContextIdleCallback Callback { get; set; }
@@ -22,41 +22,33 @@ interface ICustomLease
2222
/// This class contains the implementation of an extension to InstanceContext.
2323
/// This enables extended lifetime for the InstanceContext.
2424
/// </summary>
25-
class CustomLeaseExtension : IExtension<InstanceContext>, ICustomLease
25+
internal class CustomLeaseExtension : IExtension<InstanceContext>, ICustomLease
2626
{
2727
#region Private Fields
2828

2929
// Reference to the InstanceContext instance owns this
3030
// extension instance.
31-
InstanceContext owner;
31+
private InstanceContext _owner;
32+
private bool _isIdle;
33+
private readonly object _thisLock;
34+
private readonly Timer _idleTimer;
35+
private readonly double _idleTimeout;
36+
private InstanceContextIdleCallback _callback;
3237

33-
bool isIdle;
34-
object thisLock;
35-
Timer idleTimer;
36-
double idleTimeout;
37-
InstanceContextIdleCallback callback;
38-
string instanceId;
39-
40-
public string InstanceId
41-
{
42-
get
43-
{
44-
return this.instanceId;
45-
}
46-
}
38+
public string InstanceId { get; }
4739

4840
#endregion
4941

5042
#region Constructor
5143

5244
public CustomLeaseExtension(double idleTimeout, string instanceId)
5345
{
54-
owner = null;
55-
isIdle = false;
56-
thisLock = new object();
57-
idleTimer = new Timer();
58-
this.idleTimeout = idleTimeout;
59-
this.instanceId = instanceId;
46+
_owner = null;
47+
_isIdle = false;
48+
_thisLock = new object();
49+
_idleTimer = new Timer();
50+
_idleTimeout = idleTimeout;
51+
InstanceId = instanceId;
6052
}
6153

6254
#endregion
@@ -71,10 +63,7 @@ public CustomLeaseExtension(double idleTimeout, string instanceId)
7163
/// This method is called by WCF at the time it attaches this
7264
/// extension.
7365
/// </remarks>
74-
public void Attach(InstanceContext owner)
75-
{
76-
this.owner = owner;
77-
}
66+
public void Attach(InstanceContext owner) => _owner = owner;
7867

7968
public void Detach(InstanceContext owner)
8069
{
@@ -92,9 +81,9 @@ public bool IsIdle
9281
{
9382
get
9483
{
95-
lock (thisLock)
84+
lock (_thisLock)
9685
{
97-
if (isIdle)
86+
if (_isIdle)
9887
{
9988
return true;
10089
}
@@ -114,23 +103,23 @@ public InstanceContextIdleCallback Callback
114103
{
115104
get
116105
{
117-
lock (thisLock)
106+
lock (_thisLock)
118107
{
119-
return callback;
108+
return _callback;
120109
}
121110
}
122111
set
123112
{
124113
// Immutable state.
125-
if (idleTimer.Enabled)
114+
if (_idleTimer.Enabled)
126115
{
127116
throw new InvalidOperationException(
128117
ResourceHelper.GetString("ExCannotChangeCallback"));
129118
}
130119

131-
lock (thisLock)
120+
lock (_thisLock)
132121
{
133-
callback = value;
122+
_callback = value;
134123
}
135124
}
136125
}
@@ -142,43 +131,43 @@ public InstanceContextIdleCallback Callback
142131
/// <summary>
143132
/// Starts the timer.
144133
/// </summary>
145-
void StartTimer()
134+
private void StartTimer()
146135
{
147-
lock (thisLock)
136+
lock (_thisLock)
148137
{
149-
idleTimer.Interval = idleTimeout;
150-
idleTimer.Elapsed += new ElapsedEventHandler(idleTimer_Elapsed);
138+
_idleTimer.Interval = _idleTimeout;
139+
_idleTimer.Elapsed += new ElapsedEventHandler(idleTimer_Elapsed);
151140

152-
if (!idleTimer.Enabled)
141+
if (!_idleTimer.Enabled)
153142
{
154-
idleTimer.Start();
143+
_idleTimer.Start();
155144
}
156145
}
157146
}
158147

159148
public void StopTimer()
160149
{
161-
lock (thisLock)
150+
lock (_thisLock)
162151
{
163-
if (idleTimer.Enabled)
152+
if (_idleTimer.Enabled)
164153
{
165-
idleTimer.Stop();
154+
_idleTimer.Stop();
166155
}
167156
}
168157
}
169158

170159
/// <summary>
171160
/// Timer elapsed event handler.
172161
/// </summary>
173-
void idleTimer_Elapsed(object sender, ElapsedEventArgs args)
162+
private void idleTimer_Elapsed(object sender, ElapsedEventArgs args)
174163
{
175-
lock (thisLock)
164+
lock (_thisLock)
176165
{
177166
StopTimer();
178-
isIdle = true;
167+
_isIdle = true;
179168
Utility.WriteMessageToConsole(
180169
ResourceHelper.GetString("MsgLeaseExpired"));
181-
callback(owner);
170+
_callback(_owner);
182171
}
183172
}
184173

framework/wcf/Extensibility/Instancing/Lifetime/CS/extensions/CustomLeaseTimeAttribute.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@ public sealed class CustomLeaseTimeAttribute : Attribute, IServiceBehavior
1717
{
1818
#region Private Fields
1919

20-
double timeout;
21-
2220
#endregion
2321

2422
#region Properties
2523

2624
/// <summary>
2725
/// Gets or sets the custom lease time.
2826
/// </summary>
29-
public double Timeout
30-
{
31-
get { return this.timeout; }
32-
set { this.timeout = value; }
33-
}
27+
public double Timeout { get; set; }
3428

3529
#endregion
3630

@@ -49,7 +43,7 @@ public void AddBindingParameters(ServiceDescription description, ServiceHostBase
4943
/// </remarks>
5044
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
5145
{
52-
CustomLifetimeLease customLease = new CustomLifetimeLease(timeout);
46+
CustomLifetimeLease customLease = new CustomLifetimeLease(Timeout);
5347

5448
foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
5549
{

framework/wcf/Extensibility/Instancing/Lifetime/CS/extensions/CustomLifetimeLease.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace Microsoft.ServiceModel.Samples
99
{
1010
public static class CustomHeader
1111
{
12-
public static readonly string HeaderName = "InstanceId";
13-
public static readonly string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
12+
public const string HeaderName = "InstanceId";
13+
public const string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
1414
}
1515

1616
/// <summary>
@@ -19,13 +19,13 @@ public static class CustomHeader
1919
/// IShareableInstanceContextLifetime in order to be able
2020
/// to attach to the service model layer.
2121
/// </summary>
22-
class CustomLifetimeLease : IInstanceContextProvider
22+
internal class CustomLifetimeLease : IInstanceContextProvider
2323
{
2424
#region Private Fields
2525

26-
double timeout;
27-
bool isIdle;
28-
Dictionary<string, InstanceContext> instanceContextCache;
26+
private readonly double _timeout;
27+
private bool _isIdle;
28+
private readonly Dictionary<string, InstanceContext> _instanceContextCache;
2929

3030
// Lock must be acquired on this before
3131
// accessing the isIdle member.
@@ -38,7 +38,7 @@ class CustomLifetimeLease : IInstanceContextProvider
3838
// it will result in a considerable perf hit or
3939
// even cause dead locks depending on how
4040
// service model handles threads.
41-
object thisLock;
41+
private readonly object _thisLock;
4242

4343
#endregion
4444

@@ -49,9 +49,9 @@ class CustomLifetimeLease : IInstanceContextProvider
4949
/// </summary>
5050
public CustomLifetimeLease(double timeout)
5151
{
52-
this.timeout = timeout;
53-
thisLock = new object();
54-
this.instanceContextCache = new Dictionary<string, InstanceContext>();
52+
_timeout = timeout;
53+
_thisLock = new object();
54+
_instanceContextCache = new Dictionary<string, InstanceContext>();
5555
}
5656

5757
#endregion
@@ -60,9 +60,9 @@ public CustomLifetimeLease(double timeout)
6060

6161
public bool IsIdle(InstanceContext instanceContext)
6262
{
63-
lock (thisLock)
63+
lock (_thisLock)
6464
{
65-
if (isIdle)
65+
if (_isIdle)
6666
{
6767
Utility.WriteMessageToConsole(
6868
ResourceHelper.GetString("MsgIdle"));
@@ -73,23 +73,23 @@ public bool IsIdle(InstanceContext instanceContext)
7373
ResourceHelper.GetString("MsgNotIdle"));
7474
}
7575

76-
bool idleCopy = isIdle;
77-
isIdle = false;
76+
bool idleCopy = _isIdle;
77+
_isIdle = false;
7878
return idleCopy;
7979
}
8080
}
8181

8282
public void NotifyIdle(InstanceContextIdleCallback callback,
8383
InstanceContext instanceContext)
8484
{
85-
lock (thisLock)
85+
lock (_thisLock)
8686
{
8787
ICustomLease customLease =
8888
instanceContext.Extensions.Find<ICustomLease>();
8989

9090
customLease.Callback = callback;
91-
isIdle = customLease.IsIdle;
92-
if (isIdle)
91+
_isIdle = customLease.IsIdle;
92+
if (_isIdle)
9393
{
9494
callback(instanceContext);
9595
}
@@ -110,11 +110,11 @@ public InstanceContext GetExistingInstanceContext(System.ServiceModel.Channels.M
110110
if (message.Headers.FindHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace) != -1)
111111
{
112112
string sharingId = message.Headers.GetHeader<string>(CustomHeader.HeaderName, CustomHeader.HeaderNamespace);
113-
if (sharingId != null && instanceContextCache.ContainsKey(sharingId))
113+
if (sharingId != null && _instanceContextCache.ContainsKey(sharingId))
114114
{
115115
Utility.WriteMessageToConsole(string.Format(ResourceHelper.GetString("InstanceContextLookup"), sharingId));
116116
//Retrieve the InstanceContext from the map
117-
InstanceContext context = instanceContextCache[sharingId];
117+
InstanceContext context = _instanceContextCache[sharingId];
118118
if (context != null)
119119
{
120120
//Before returning, stop the timer on this InstanceContext
@@ -123,7 +123,7 @@ public InstanceContext GetExistingInstanceContext(System.ServiceModel.Channels.M
123123
extension.StopTimer();
124124

125125
Utility.WriteMessageToConsole(ResourceHelper.GetString("CachedInstanceContextFound"));
126-
return instanceContextCache[sharingId];
126+
return _instanceContextCache[sharingId];
127127
}
128128
}
129129
}
@@ -152,13 +152,13 @@ public void InitializeInstanceContext(InstanceContext instanceContext, System.Se
152152
Utility.WriteMessageToConsole(string.Format(ResourceHelper.GetString("InstanceContextAddedToCache"), headerId));
153153

154154
//Add this to the Cache
155-
this.instanceContextCache[headerId] = instanceContext;
155+
_instanceContextCache[headerId] = instanceContext;
156156

157157
//Register the Closing event of this InstancContext so it can be removed from the collection
158-
instanceContext.Closing += this.RemoveInstanceContext;
158+
instanceContext.Closing += RemoveInstanceContext;
159159

160160
IExtension<InstanceContext> customLeaseExtension =
161-
new CustomLeaseExtension(timeout, headerId);
161+
new CustomLeaseExtension(_timeout, headerId);
162162
instanceContext.Extensions.Add(customLeaseExtension);
163163
}
164164

@@ -167,10 +167,10 @@ public void RemoveInstanceContext(object o, EventArgs args)
167167
InstanceContext context = o as InstanceContext;
168168
CustomLeaseExtension extension = context.Extensions.Find<CustomLeaseExtension>();
169169
string id = (extension != null) ? extension.InstanceId : null;
170-
if (this.instanceContextCache[id] != null)
170+
if (_instanceContextCache[id] != null)
171171
{
172172
Utility.WriteMessageToConsole(string.Format(ResourceHelper.GetString("InstanceContextRemovedFromCache"), id));
173-
this.instanceContextCache.Remove(id);
173+
_instanceContextCache.Remove(id);
174174
}
175175
}
176176
#endregion

framework/wcf/Extensibility/Instancing/Lifetime/CS/extensions/ResourceHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.ServiceModel.Samples
1313
/// This is a helper class for accessing the
1414
/// resource file.
1515
/// </summary>
16-
class ResourceHelper
16+
internal class ResourceHelper
1717
{
1818

1919
#region Public Static Members

framework/wcf/Extensibility/Instancing/Lifetime/CS/extensions/Utility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Microsoft.ServiceModel.Samples
66
{
7-
class Utility
7+
internal class Utility
88
{
99
public static void WriteMessageToConsole(string message)
1010
{

0 commit comments

Comments
 (0)