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 ReleaseServiceInstanceOnTransactionComplete = true;
50 TransactionIsolationLevel = IsolationLevel.Unspecified;
51 UseSynchronizationContext = true;
52 ValidateMustUnderstand = true;
59 public string Name { get; set; }
61 public string Namespace { get; set; }
63 public string ConfigurationName { get; set; }
66 public AddressFilterMode AddressFilterMode { get; set; }
69 public bool AutomaticSessionShutdown { get; set; }
72 public ConcurrencyMode ConcurrencyMode { get; set; }
75 public bool IgnoreExtensionDataObject { get; set; }
77 public InstanceContextMode InstanceContextMode { get; set; }
79 public bool IncludeExceptionDetailInFaults { get; set; }
82 public int MaxItemsInObjectGraph { get; set; }
85 public bool ReleaseServiceInstanceOnTransactionComplete { get; set; }
87 public bool UseSynchronizationContext { get; set; }
90 public IsolationLevel TransactionIsolationLevel { get; set; }
93 public bool TransactionAutoCompleteOnSessionClose { get; set; }
96 public string TransactionTimeout {
97 get { return tx_timeout; }
100 TimeSpan.Parse (value);
106 public bool ValidateMustUnderstand { get; set; }
108 public object GetWellKnownSingleton ()
113 public void SetWellKnownSingleton (object value)
116 throw new ArgumentNullException ("value");
121 void IServiceBehavior.AddBindingParameters (
122 ServiceDescription description,
123 ServiceHostBase serviceHostBase,
124 Collection<ServiceEndpoint> endpoints,
125 BindingParameterCollection parameters)
130 void IServiceBehavior.ApplyDispatchBehavior (
131 ServiceDescription description,
132 ServiceHostBase serviceHostBase)
134 if (singleton != null && InstanceContextMode != InstanceContextMode.Single)
135 throw new InvalidOperationException ("When creating a Service host with a service instance, use InstanceContextMode.Single in the ServiceBehaviorAttribute.");
137 foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) {
138 ChannelDispatcher cd = cdb as ChannelDispatcher;
141 if (IncludeExceptionDetailInFaults) // may be set also in ServiceDebugBehaviorAttribute
142 cd.IncludeExceptionDetailInFaults = true;
143 foreach (EndpointDispatcher ed in cd.Endpoints) {
144 var dr = ed.DispatchRuntime;
145 if (dr.SingletonInstanceContext == null && InstanceContextMode == InstanceContextMode.Single)
146 dr.SingletonInstanceContext = CreateSingletonInstanceContext (serviceHostBase);
147 if (dr.InstanceContextProvider == null)
148 dr.InstanceContextProvider = CreateInstanceContextProvider (serviceHostBase, dr);
153 InstanceContext CreateSingletonInstanceContext (ServiceHostBase host)
155 return new InstanceContext (host, GetWellKnownSingleton ());
158 IInstanceContextProvider CreateInstanceContextProvider (ServiceHostBase host, DispatchRuntime runtime)
160 switch (InstanceContextMode) {
161 case InstanceContextMode.Single:
162 return new SingletonInstanceContextProvider (runtime.SingletonInstanceContext);
163 case InstanceContextMode.PerSession:
164 return new SessionInstanceContextProvider (host);
165 //case InstanceContextMode.PerCall:
167 return null; // default
172 void IServiceBehavior.Validate (
173 ServiceDescription description,
174 ServiceHostBase serviceHostBase)