Updated WCF Facility to support automatic server hosting, automatic client proxy...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / WindsorInstanceProvider.cs
blobb4f130533c9a508fd99b156222f471db6d1ea44a
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 using Castle.MicroKernel;
17 namespace Castle.Facilities.WcfIntegration
19 using System;
20 using System.ServiceModel;
21 using System.ServiceModel.Channels;
22 using System.ServiceModel.Dispatcher;
23 using Castle.Windsor;
25 /// <summary>
26 /// Initialize a service using Windsor
27 /// </summary>
28 public class WindsorInstanceProvider : IInstanceProvider
30 private readonly Type contractType;
31 private readonly IKernel kernel;
32 private readonly Type serviceType;
34 /// <summary>
35 /// Initializes a new instance of the <see cref="T:Castle.Facilities.WcfIntegration.WindsorInstanceProvider" /> class.
36 /// </summary>
37 public WindsorInstanceProvider(IKernel kernel, Type contractType, Type serviceType)
39 this.kernel = kernel;
40 this.contractType = contractType;
41 this.serviceType = serviceType;
44 /// <summary>
45 /// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
46 /// </summary>
47 ///
48 /// <returns>
49 /// A user-defined service object.
50 /// </returns>
51 ///
52 /// <param name="instanceContext">The current <see cref="T:System.ServiceModel.InstanceContext"></see> object.</param>
53 public object GetInstance(InstanceContext instanceContext)
55 return GetInstance(instanceContext, null);
58 /// <summary>
59 /// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
60 /// </summary>
61 ///
62 /// <returns>
63 /// The service object.
64 /// </returns>
65 ///
66 /// <param name="message">The message that triggered the creation of a service object.</param>
67 /// <param name="instanceContext">The current <see cref="T:System.ServiceModel.InstanceContext"></see> object.</param>
68 public object GetInstance(InstanceContext instanceContext, Message message)
70 if (kernel.HasComponent(contractType) || !kernel.HasComponent(serviceType))
72 return kernel.Resolve(contractType);
74 return kernel.Resolve(serviceType);
77 /// <summary>
78 /// Called when an <see cref="T:System.ServiceModel.InstanceContext"></see> object recycles a service object.
79 /// </summary>
80 ///
81 /// <param name="instanceContext">The service's instance context.</param>
82 /// <param name="instance">The service object to be recycled.</param>
83 public void ReleaseInstance(InstanceContext instanceContext, object instance)
85 kernel.ReleaseComponent(instance);