-
Notifications
You must be signed in to change notification settings - Fork 557
/
InstanceBehavior.cs
103 lines (86 loc) · 3.29 KB
/
InstanceBehavior.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime;
using System.ServiceModel.Channels;
using System.ServiceModel.Diagnostics;
namespace System.ServiceModel.Dispatcher
{
public class InstanceBehavior
{
private IInstanceProvider _provider;
internal InstanceBehavior(DispatchRuntime dispatch, ImmutableDispatchRuntime immutableRuntime)
{
_provider = dispatch.InstanceProvider;
InstanceContextProvider = dispatch.InstanceContextProvider;
}
internal IInstanceContextProvider InstanceContextProvider { get; }
internal void AfterReply(ref MessageRpc rpc, ErrorBehavior error)
{
InstanceContext context = rpc.InstanceContext;
if (context != null)
{
try
{
context.UnbindRpc(ref rpc);
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
error.HandleError(e);
}
}
}
internal void EnsureInstanceContext(ref MessageRpc rpc)
{
if (rpc.InstanceContext == null)
{
throw new ArgumentNullException("rpc.InstanceContext");
}
rpc.OperationContext.SetInstanceContext(rpc.InstanceContext);
rpc.InstanceContext.Behavior = this;
if (rpc.InstanceContext.State == CommunicationState.Created)
{
lock (rpc.InstanceContext.ThisLock)
{
if (rpc.InstanceContext.State == CommunicationState.Created)
{
rpc.InstanceContext.Open(rpc.Channel.CloseTimeout);
}
}
}
rpc.InstanceContext.BindRpc(ref rpc);
}
internal object GetInstance(InstanceContext instanceContext)
{
if (_provider == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SRP.SFxNoDefaultConstructor));
}
return _provider.GetInstance(instanceContext);
}
internal object GetInstance(InstanceContext instanceContext, Message request)
{
if (_provider == null)
{
throw TraceUtility.ThrowHelperError(new InvalidOperationException(SRP.SFxNoDefaultConstructor), request);
}
return _provider.GetInstance(instanceContext, request);
}
internal void EnsureServiceInstance(ref MessageRpc rpc)
{
if (WcfEventSource.Instance.GetServiceInstanceStartIsEnabled())
{
WcfEventSource.Instance.GetServiceInstanceStart(rpc.EventTraceActivity);
}
rpc.Instance = rpc.InstanceContext.GetServiceInstance(rpc.Request);
if (WcfEventSource.Instance.GetServiceInstanceStopIsEnabled())
{
WcfEventSource.Instance.GetServiceInstanceStop(rpc.EventTraceActivity);
}
}
}
}