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 ()
47 bool auto_session_shutdown, ignore_ext_data,
48 release, inc_fault_details,
49 use_sync_ctx, tx_close, validate_must_understand;
50 ConcurrencyMode concurrency;
51 IsolationLevel tx_level;
53 InstanceContextMode context_mode = InstanceContextMode.PerCall;
57 public bool AutomaticSessionShutdown {
58 get { return auto_session_shutdown; }
59 set { auto_session_shutdown = value; }
63 public ConcurrencyMode ConcurrencyMode {
64 get { return concurrency; }
65 set { concurrency = value; }
69 public bool IgnoreExtensionDataObject {
70 get { return ignore_ext_data; }
71 set { ignore_ext_data = value; }
74 public InstanceContextMode InstanceContextMode {
75 get { return context_mode; }
76 set { context_mode = value; }
79 public bool ReleaseServiceInstanceOnTransactionComplete {
80 get { return release; }
81 set { release = value; }
85 public bool IncludeExceptionDetailInFaults {
86 get { return inc_fault_details; }
87 set { inc_fault_details = value; }
91 public bool UseSynchronizationContext {
92 get { return use_sync_ctx; }
93 set { use_sync_ctx = value; }
97 public bool TransactionAutoCompleteOnSessionClose {
98 get { return tx_close; }
99 set { tx_close = value; }
103 public IsolationLevel TransactionIsolationLevel {
104 get { return tx_level; }
105 set { tx_level = value; }
109 public string TransactionTimeout {
110 get { return tx_timeout; }
113 TimeSpan.Parse (value);
119 public bool ValidateMustUnderstand {
120 get { return validate_must_understand; }
121 set { validate_must_understand = value; }
124 public object GetWellKnownSingleton ()
129 public void SetWellKnownSingleton (object value)
132 throw new ArgumentNullException ("value");
137 void IServiceBehavior.AddBindingParameters (
138 ServiceDescription description,
139 ServiceHostBase serviceHostBase,
140 Collection<ServiceEndpoint> endpoints,
141 BindingParameterCollection parameters)
146 void IServiceBehavior.ApplyDispatchBehavior (
147 ServiceDescription description,
148 ServiceHostBase serviceHostBase)
150 if (singleton != null && context_mode != InstanceContextMode.Single)
151 throw new InvalidOperationException ("When creating a Service host with a service instance, use InstanceContext.Mode.Single in the ServiceBehaviorAttribute.");
153 foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) {
154 ChannelDispatcher cd = cdb as ChannelDispatcher;
157 if (IncludeExceptionDetailInFaults) // may be set also in ServiceDebugBehaviorAttribute
158 cd.IncludeExceptionDetailInFaults = true;
159 foreach (EndpointDispatcher ed in cd.Endpoints)
160 ed.DispatchRuntime.InstanceContextProvider = CreateInstanceContextProvider (serviceHostBase);
164 IInstanceContextProvider CreateInstanceContextProvider (ServiceHostBase host)
166 switch (InstanceContextMode) {
167 case InstanceContextMode.Single:
168 return new SingletonInstanceContextProvider (new InstanceContext (host, GetWellKnownSingleton ()));
169 case InstanceContextMode.PerSession:
171 throw new NotImplementedException ();
172 //case InstanceContextMode.PerCall:
174 return null; // default
179 void IServiceBehavior.Validate (
180 ServiceDescription description,
181 ServiceHostBase serviceHostBase)