added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Extensibility / Instancing / Durable / CS / extensions / InstanceProvider.cs
blob2b07d07d5e629de511d968c91724af4cec12295d
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel;
5 using System.ServiceModel.Dispatcher;
6 using System.ServiceModel.Channels;
8 namespace Microsoft.ServiceModel.Samples
10 //This class contains the implementation of an IInstanceProvider
11 //which provides the instances created by the storage manager.
12 class InstanceProvider : IInstanceProvider
14 Type serviceType;
16 public InstanceProvider(Type serviceType)
18 this.serviceType = serviceType;
21 public object GetInstance(InstanceContext instanceContext)
23 return this.GetInstance(instanceContext, null);
26 public void ReleaseInstance(InstanceContext instanceContext, object instance)
30 public object GetInstance(InstanceContext instanceContext, Message message)
32 object instance = null;
34 InstanceContextExtension extension =
35 instanceContext.Extensions.Find<InstanceContextExtension>();
37 string contextId = extension.ContextId;
38 IStorageManager storageManager = extension.StorageManager;
40 //Get the instance from the storage manager.
41 instance = storageManager.GetInstance(contextId, serviceType);
43 // If the storage manager returns null (which means that there is
44 // no instance available for the given context id) create a new
45 // instance and return it.
46 if (instance == null)
48 instance = Activator.CreateInstance(serviceType);
51 return instance;