2 // ServiceBehaviorAttribute.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Collections
.Generic
;
30 using System
.Collections
.ObjectModel
;
31 using System
.Runtime
.Serialization
;
32 using System
.Transactions
;
33 using System
.ServiceModel
.Channels
;
34 using System
.ServiceModel
.Description
;
35 using System
.ServiceModel
.Dispatcher
;
37 namespace System
.ServiceModel
39 [AttributeUsage (AttributeTargets
.Class
)]
40 public sealed class ServiceBehaviorAttribute
41 : Attribute
, IServiceBehavior
43 public ServiceBehaviorAttribute ()
45 AutomaticSessionShutdown
= true;
46 ConcurrencyMode
= ConcurrencyMode
.Single
;
47 InstanceContextMode
= InstanceContextMode
.PerSession
;
48 MaxItemsInObjectGraph
= 0x10000;
49 SessionMode
= SessionMode
.Allowed
;
50 ReleaseServiceInstanceOnTransactionComplete
= true;
51 TransactionIsolationLevel
= IsolationLevel
.Unspecified
;
52 UseSynchronizationContext
= true;
53 ValidateMustUnderstand
= true;
60 public string Name { get; set; }
62 public string Namespace { get; set; }
64 public string ConfigurationName { get; set; }
67 public AddressFilterMode AddressFilterMode { get; set; }
70 public bool AutomaticSessionShutdown { get; set; }
73 public ConcurrencyMode ConcurrencyMode { get; set; }
76 public bool IgnoreExtensionDataObject { get; set; }
78 public InstanceContextMode InstanceContextMode { get; set; }
80 public bool IncludeExceptionDetailInFaults { get; set; }
83 public int MaxItemsInObjectGraph { get; set; }
86 public bool ReleaseServiceInstanceOnTransactionComplete { get; set; }
89 public SessionMode SessionMode { get; set; }
91 public bool UseSynchronizationContext { get; set; }
94 public IsolationLevel TransactionIsolationLevel { get; set; }
97 public bool TransactionAutoCompleteOnSessionClose { get; set; }
100 public string TransactionTimeout
{
101 get { return tx_timeout; }
104 TimeSpan
.Parse (value);
110 public bool ValidateMustUnderstand { get; set; }
112 public object GetWellKnownSingleton ()
117 public void SetWellKnownSingleton (object value)
120 throw new ArgumentNullException ("value");
125 void IServiceBehavior
.AddBindingParameters (
126 ServiceDescription description
,
127 ServiceHostBase serviceHostBase
,
128 Collection
<ServiceEndpoint
> endpoints
,
129 BindingParameterCollection parameters
)
134 void IServiceBehavior
.ApplyDispatchBehavior (
135 ServiceDescription description
,
136 ServiceHostBase serviceHostBase
)
138 if (singleton
!= null && InstanceContextMode
!= InstanceContextMode
.Single
)
139 throw new InvalidOperationException ("When creating a Service host with a service instance, use InstanceContextMode.Single in the ServiceBehaviorAttribute.");
141 foreach (ChannelDispatcherBase cdb
in serviceHostBase
.ChannelDispatchers
) {
142 ChannelDispatcher cd
= cdb
as ChannelDispatcher
;
145 if (IncludeExceptionDetailInFaults
) // may be set also in ServiceDebugBehaviorAttribute
146 cd
.IncludeExceptionDetailInFaults
= true;
147 foreach (EndpointDispatcher ed
in cd
.Endpoints
) {
148 if (InstanceContextMode
== InstanceContextMode
.Single
)
149 ed
.DispatchRuntime
.SingletonInstanceContext
= CreateSingletonInstanceContext (serviceHostBase
);
150 ed
.DispatchRuntime
.InstanceContextProvider
= CreateInstanceContextProvider (serviceHostBase
, ed
.DispatchRuntime
);
155 InstanceContext
CreateSingletonInstanceContext (ServiceHostBase host
)
157 return new InstanceContext (host
, GetWellKnownSingleton ());
160 IInstanceContextProvider
CreateInstanceContextProvider (ServiceHostBase host
, DispatchRuntime runtime
)
162 switch (InstanceContextMode
) {
163 case InstanceContextMode
.Single
:
164 return new SingletonInstanceContextProvider (runtime
.SingletonInstanceContext
);
165 case InstanceContextMode
.PerSession
:
166 return new SessionInstanceContextProvider (host
);
167 //case InstanceContextMode.PerCall:
169 return null; // default
174 void IServiceBehavior
.Validate (
175 ServiceDescription description
,
176 ServiceHostBase serviceHostBase
)