1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 
5 namespace System.ServiceModel
6 {
7     using System.Reflection;
8     using System.ServiceModel.Channels;
9     using System.ServiceModel.Dispatcher;
10     using System.ServiceModel.Description;
11     using System.Transactions;
12     using System.ServiceModel.Security;
13     using System.Security.Principal;
14     using System.Collections.Generic;
15 
16     [AttributeUsage(ServiceModelAttributeTargets.OperationBehavior)]
17     public sealed class OperationBehaviorAttribute : Attribute, IOperationBehavior
18     {
19         internal const ImpersonationOption DefaultImpersonationOption = ImpersonationOption.NotAllowed;
20         bool autoCompleteTransaction = true;
21         bool autoEnlistTransaction = false;
22         bool autoDisposeParameters = true;
23         bool preferAsyncInvocation = false;
24         ImpersonationOption impersonation = ImpersonationOption.NotAllowed;
25         ReleaseInstanceMode releaseInstance = ReleaseInstanceMode.None;
26 
27 
28         public bool TransactionAutoComplete
29         {
30             get { return this.autoCompleteTransaction; }
31             set { this.autoCompleteTransaction = value; }
32         }
33 
34         public bool TransactionScopeRequired
35         {
36             get { return this.autoEnlistTransaction; }
37             set { this.autoEnlistTransaction = value; }
38         }
39 
40         public bool AutoDisposeParameters
41         {
42             get { return this.autoDisposeParameters; }
43             set { this.autoDisposeParameters = value; }
44         }
45 
46         public ImpersonationOption Impersonation
47         {
48             get
49             {
50                 return this.impersonation;
51             }
52             set
53             {
54                 if (!ImpersonationOptionHelper.IsDefined(value))
55                 {
56                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
57                 }
58                 this.impersonation = value;
59             }
60         }
61 
62         public ReleaseInstanceMode ReleaseInstanceMode
63         {
64             get { return this.releaseInstance; }
65             set
66             {
67                 if (!ReleaseInstanceModeHelper.IsDefined(value))
68                 {
69                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
70                 }
71 
72                 this.releaseInstance = value;
73             }
74         }
75 
76         internal bool PreferAsyncInvocation
77         {
78             get { return this.preferAsyncInvocation; }
79             set { this.preferAsyncInvocation = value; }
80         }
81 
IOperationBehavior.Validate(OperationDescription description)82         void IOperationBehavior.Validate(OperationDescription description)
83         {
84         }
85 
IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)86         void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
87         {
88         }
89 
IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)90         void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
91         {
92             if (description == null)
93             {
94                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
95             }
96             if (dispatch == null)
97             {
98                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatch");
99             }
100             if (description.IsServerInitiated() && this.releaseInstance != ReleaseInstanceMode.None)
101             {
102                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
103                     SR.GetString(SR.SFxOperationBehaviorAttributeReleaseInstanceModeDoesNotApplyToCallback,
104                     description.Name)));
105             }
106             dispatch.TransactionRequired = this.autoEnlistTransaction;
107             dispatch.TransactionAutoComplete = this.autoCompleteTransaction;
108             dispatch.AutoDisposeParameters = this.autoDisposeParameters;
109             dispatch.ReleaseInstanceBeforeCall = (this.releaseInstance & ReleaseInstanceMode.BeforeCall) != 0;
110             dispatch.ReleaseInstanceAfterCall = (this.releaseInstance & ReleaseInstanceMode.AfterCall) != 0;
111             dispatch.Impersonation = this.Impersonation;
112         }
113 
IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)114         void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
115         {
116         }
117     }
118 }
119