Updates to the WCF Integrations facility to make client and service support consistent.
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Client / WcfClientActivator.cs
blobda30c1bd3b2db5dff435afc6e427d3e7e6fad643
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 namespace Castle.Facilities.WcfIntegration
17 using System;
18 using Castle.Core;
19 using Castle.MicroKernel;
20 using Castle.MicroKernel.ComponentActivator;
22 public class WcfClientActivator : DefaultComponentActivator
24 private readonly ChannelCreator createChannel;
26 public WcfClientActivator(ComponentModel model, IKernel kernel,
27 ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
28 : base(model, kernel, onCreation, onDestruction)
30 createChannel = CreateChannelCreator(kernel, model);
33 protected override object InternalCreate(CreationContext context)
35 object instance = Instantiate(context);
37 ApplyCommissionConcerns(instance);
39 return instance;
42 protected override object Instantiate(CreationContext context)
44 object instance = createChannel();
46 try
48 instance = Kernel.ProxyFactory.Create(Kernel, instance, Model);
50 catch (Exception ex)
52 throw new ComponentActivatorException("WcfClientActivator: could not proxy service " +
53 Model.Service.FullName, ex);
56 return instance;
59 private ChannelCreator CreateChannelCreator(IKernel kernel, ComponentModel model)
61 WcfClientModel clientModel = (WcfClientModel)
62 model.ExtendedProperties[WcfConstants.ClientModelKey];
64 IClientChannelBuilder channelBuilder = kernel.Resolve<IClientChannelBuilder>();
65 ChannelCreator creator = channelBuilder.GetChannelCreator(clientModel.Endpoint, model.Service);
66 model.ExtendedProperties[WcfConstants.ChannelCreatorKey] = creator;
67 return creator;