added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Extensibility / Instancing / Durable / CS / extensions / DurableInstanceContextBindingElement.cs
blob89d379ba0ca65e083f2a96795374f33b11f85b85
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
6 using System.IO;
7 using System.ServiceModel.Security;
9 namespace Microsoft.ServiceModel.Samples
11 public class DurableInstanceContextBindingElement : BindingElement
13 string contextStoreLocation;
14 ContextType contextType;
17 public DurableInstanceContextBindingElement()
19 InitializeContextStoreLocation();
22 public DurableInstanceContextBindingElement(BindingElement other)
23 : base(other)
25 DurableInstanceContextBindingElement otherBindingElement = other as DurableInstanceContextBindingElement;
27 if (otherBindingElement != null)
29 this.contextStoreLocation = otherBindingElement.contextStoreLocation;
30 this.contextType = otherBindingElement.contextType;
32 if ((contextStoreLocation == null) ||
33 (contextStoreLocation.Trim().Length == 0))
35 InitializeContextStoreLocation();
37 else
39 // Throw if the specified contextStoreLocation is
40 // invalid.
41 if (!Directory.Exists(contextStoreLocation))
43 throw new InvalidOperationException(
44 ResourceHelper.GetString(@"ExInvalidContextStorePath"));
49 else
51 this.contextType = ContextType.MessageHeader;
52 InitializeContextStoreLocation();
56 public override bool CanBuildChannelFactory<TChannel>(
57 BindingContext context)
59 ValidateTransport(context.Binding);
60 return context.CanBuildInnerChannelFactory<TChannel>();
63 public override bool CanBuildChannelListener<TChannel>(
64 BindingContext context)
66 ValidateTransport(context.Binding);
67 return context.CanBuildInnerChannelListener<TChannel>();
70 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
72 ValidateTransport(context.Binding);
73 IChannelFactory<TChannel> innerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
75 IChannelFactory<TChannel> channelFactory =
76 new DurableInstanceChannelFactory<TChannel>(contextStoreLocation,
77 contextType, innerChannelFactory);
79 return channelFactory;
82 public override IChannelListener<TChannel>
83 BuildChannelListener<TChannel>(BindingContext context)
85 ValidateTransport(context.Binding);
87 return new DurableInstanceChannelListener<TChannel>(contextType, contextStoreLocation, context);
90 public override BindingElement Clone()
92 return new DurableInstanceContextBindingElement(this);
95 public override T GetProperty<T>(BindingContext context)
97 if (typeof(T) == typeof(ContextType))
99 return (T)(object)this.ContextType;
102 return context.GetInnerProperty<T>();
105 public ContextType ContextType
107 get { return this.contextType; }
108 set { this.contextType = value; }
111 public string ContextStoreLocation
113 get { return this.contextStoreLocation; }
114 set { this.contextStoreLocation = value; }
117 //If the context type is set to HttpCookie this
118 //function verifies that the current binding
119 //is using the HTTP transport.
120 void ValidateTransport(CustomBinding binding)
122 if (this.contextType == ContextType.HttpCookie)
124 HttpTransportBindingElement httpTransport =
125 binding.Elements.Find<HttpTransportBindingElement>();
127 if (httpTransport == null)
129 throw new InvalidOperationException(
130 ResourceHelper.GetString("ExInvalidTransport"));
135 //Initializes contextStoreLocation.
136 //This method checks whether the directory that is used as the
137 //context store location exists and creates it if it is not
138 //available.
139 void InitializeContextStoreLocation()
141 // Get the path of the temp directory of the current user.
142 string temp = Path.GetTempPath();
144 // Build the required directory path in the Local Settings
145 // directory.
146 temp += DurableInstanceContextUtility.ContextStoreDirectory;
148 // Create the directory if it does not exist.
149 if (!Directory.Exists(temp))
151 Directory.CreateDirectory(temp);
154 this.contextStoreLocation = temp;