added SSCLI 1.0
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Extensibility / Instancing / Durable / CS / extensions / DurableInstanceContextAttribute.cs
blob5d255a20f8ef0213825691af9354be656baa9eb3
1 #region using
3 using System;
4 using System.Collections.Generic;
5 using System.Text;
6 using System.ServiceModel;
7 using System.Collections.ObjectModel;
8 using System.ServiceModel.Description;
9 using System.ServiceModel.Channels;
10 using System.ServiceModel.Dispatcher;
12 #endregion
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
18 //durable instancing.
19 [AttributeUsage(AttributeTargets.Class)]
20 public sealed class DurableInstanceContextAttribute : Attribute, IServiceBehavior
22 #region Private members
24 private Type storageManagerType;
26 #endregion
28 #region Constructors
30 public DurableInstanceContextAttribute()
34 #endregion
36 #region Properties
37 public Type StorageManagerType
39 get { return this.storageManagerType; }
40 set { this.storageManagerType = value; }
43 #endregion
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
67 // storage manager.
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;
81 if (cd != null)
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)
96 #endregion