1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 using System
.ServiceModel
;
21 using System
.ServiceModel
.Channels
;
22 using System
.ServiceModel
.Dispatcher
;
26 /// Initialize a service using Windsor
28 public class WindsorInstanceProvider
: IInstanceProvider
30 private readonly Type contractType
;
31 private readonly IKernel kernel
;
32 private readonly Type serviceType
;
35 /// Initializes a new instance of the <see cref="T:Castle.Facilities.WcfIntegration.WindsorInstanceProvider" /> class.
37 public WindsorInstanceProvider(IKernel kernel
, Type contractType
, Type serviceType
)
40 this.contractType
= contractType
;
41 this.serviceType
= serviceType
;
45 /// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
49 /// A user-defined service object.
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);
59 /// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext"></see> object.
63 /// The service object.
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
);
78 /// Called when an <see cref="T:System.ServiceModel.InstanceContext"></see> object recycles a service object.
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
);