Merge pull request #2400 from esdrubal/extrahead
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ServiceBehaviorAttribute.cs
1 //
2 // ServiceBehaviorAttribute.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
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;
36
37 namespace System.ServiceModel
38 {
39         [AttributeUsage (AttributeTargets.Class)]
40         public sealed class ServiceBehaviorAttribute
41                 : Attribute, IServiceBehavior
42         {
43                 public ServiceBehaviorAttribute ()
44                 {
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;
53                 }
54
55                 string tx_timeout;
56                 object singleton;
57
58                 [MonoTODO]
59                 public string Name { get; set; }
60                 [MonoTODO]
61                 public string Namespace { get; set; }
62                 [MonoTODO]
63                 public string ConfigurationName { get; set; }
64
65                 [MonoTODO]
66                 public AddressFilterMode AddressFilterMode { get; set; }
67
68                 [MonoTODO]
69                 public bool AutomaticSessionShutdown { get; set; }
70
71                 [MonoTODO]
72                 public ConcurrencyMode ConcurrencyMode { get; set; }
73
74                 [MonoTODO]
75                 public bool IgnoreExtensionDataObject { get; set; }
76
77                 public InstanceContextMode InstanceContextMode { get; set; }
78
79                 public bool IncludeExceptionDetailInFaults { get; set; }
80
81                 [MonoTODO]
82                 public int MaxItemsInObjectGraph { get; set; }
83
84                 [MonoTODO]
85                 public bool ReleaseServiceInstanceOnTransactionComplete { get; set; }
86
87                 public bool UseSynchronizationContext { get; set; }
88
89                 [MonoTODO]
90                 public IsolationLevel TransactionIsolationLevel { get; set; }
91
92                 [MonoTODO]
93                 public bool TransactionAutoCompleteOnSessionClose { get; set; }
94
95                 [MonoTODO]
96                 public string TransactionTimeout {
97                         get { return tx_timeout; }
98                         set {
99                                 if (value != null)
100                                         TimeSpan.Parse (value);
101                                 tx_timeout = value;
102                         }
103                 }
104
105                 [MonoTODO]
106                 public bool ValidateMustUnderstand { get; set; }
107
108                 public object GetWellKnownSingleton ()
109                 {
110                         return singleton;
111                 }
112
113                 public void SetWellKnownSingleton (object value)
114                 {
115                         if (value == null)
116                                 throw new ArgumentNullException ("value");
117                         singleton = value;
118                 }
119
120                 [MonoTODO]
121                 void IServiceBehavior.AddBindingParameters (
122                         ServiceDescription description,
123                         ServiceHostBase serviceHostBase,
124                         Collection<ServiceEndpoint> endpoints,
125                         BindingParameterCollection parameters)
126                 {
127                 }
128
129                 [MonoTODO]
130                 void IServiceBehavior.ApplyDispatchBehavior (
131                         ServiceDescription description,
132                         ServiceHostBase serviceHostBase)
133                 {
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.");
136
137                         foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) {
138                                 ChannelDispatcher cd = cdb as ChannelDispatcher;
139                                 if (cd == null)
140                                         continue;
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);
149                                 }
150                         }
151                 }
152
153                 InstanceContext CreateSingletonInstanceContext (ServiceHostBase host)
154                 {
155                         return new InstanceContext (host, GetWellKnownSingleton ());
156                 }
157
158                 IInstanceContextProvider CreateInstanceContextProvider (ServiceHostBase host, DispatchRuntime runtime)
159                 {
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:
166                         default:
167                                 return null; // default
168                         }
169                 }
170
171                 [MonoTODO]
172                 void IServiceBehavior.Validate (
173                         ServiceDescription description,
174                         ServiceHostBase serviceHostBase)
175                 {                       
176                 }
177         }
178 }