4 using System
.Collections
.Generic
;
6 using System
.ServiceModel
;
7 using System
.Collections
.ObjectModel
;
8 using System
.ServiceModel
.Description
;
9 using System
.ServiceModel
.Channels
;
10 using System
.ServiceModel
.Dispatcher
;
14 namespace Microsoft
.ServiceModel
.Samples
16 //This class contains the implementation of the attribute that
17 //can be used with the service class to enable
19 [AttributeUsage(AttributeTargets
.Class
)]
20 public sealed class DurableInstanceContextAttribute
: Attribute
, IServiceBehavior
22 #region Private members
24 private Type storageManagerType
;
30 public DurableInstanceContextAttribute()
37 public Type StorageManagerType
39 get { return this.storageManagerType; }
40 set { this.storageManagerType = value; }
45 #region IServiceBehavior Members
47 public void AddBindingParameters(ServiceDescription serviceDescription
, ServiceHostBase serviceHostBase
,
48 Collection
<ServiceEndpoint
> endpoints
, BindingParameterCollection bindingParameters
)
52 public void ApplyDispatchBehavior(ServiceDescription serviceDescription
, ServiceHostBase serviceHostBase
)
54 // Durable instancing could not be used with
55 // singleton instancing.
56 ServiceBehaviorAttribute serviceBehavior
=
57 serviceDescription
.Behaviors
.Find
<ServiceBehaviorAttribute
>();
59 if (serviceBehavior
!= null &&
60 serviceBehavior
.InstanceContextMode
== InstanceContextMode
.Single
)
62 throw new InvalidOperationException(
63 ResourceHelper
.GetString("ExSingeltonInstancingNotSupported"));
66 // Use the StorageManagerFactory to create an instance of a
68 IStorageManager storageManager
=
69 StorageManagerFactory
.GetStorageManager(storageManagerType
);
71 InstanceContextInitializer contextInitializer
=
72 new InstanceContextInitializer(storageManager
);
74 InstanceProvider instanceProvider
=
75 new InstanceProvider(serviceDescription
.ServiceType
);
77 foreach (ChannelDispatcherBase cdb
in serviceHostBase
.ChannelDispatchers
)
79 ChannelDispatcher cd
= cdb
as ChannelDispatcher
;
83 foreach (EndpointDispatcher ed
in cd
.Endpoints
)
85 ed
.DispatchRuntime
.InstanceContextInitializers
.Add(contextInitializer
);
86 ed
.DispatchRuntime
.InstanceProvider
= instanceProvider
;
92 public void Validate(ServiceDescription serviceDescription
, ServiceHostBase serviceHostBase
)