Applied patch from Jan Limpens 'ReflectionBasedDictionaryAdapter needs to check if...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Client / WcfClientExtension.cs
blobe665229d1754d2b2f4438e4cfaaf4e2ba2abeccf
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.Proxy;
21 using Castle.Facilities.WcfIntegration.Internal;
22 using Castle.Facilities.WcfIntegration.Rest;
24 public class WcfClientExtension : IDisposable
26 private readonly IKernel kernel;
28 public WcfClientExtension(IKernel kernel)
30 this.kernel = kernel;
32 AddDefaultChannelBuilders();
34 kernel.AddComponent<WcfManagedChannelInterceptor>();
35 kernel.ComponentModelCreated += Kernel_ComponentModelCreated;
38 public WcfClientExtension AddChannelBuilder<T, M>()
39 where T : IClientChannelBuilder<M>
40 where M : IWcfClientModel
42 AddChannelBuilder<T, M>(true);
43 return this;
46 private void Kernel_ComponentModelCreated(ComponentModel model)
48 IWcfClientModel clientModel = ResolveClientModel(model);
50 if (clientModel != null)
52 model.CustomComponentActivator = typeof(WcfClientActivator);
53 model.ExtendedProperties[WcfConstants.ClientModelKey] = clientModel;
54 model.LifecycleSteps.Add(LifecycleStepType.Decommission,
55 WcfCommunicationDecomissionConcern.Instance);
56 InstallManagedChannelInterceptor(model);
60 private void AddDefaultChannelBuilders()
62 AddChannelBuilder<DefaultChannelBuilder, DefaultClientModel>(false);
63 #if DOTNET35
64 AddChannelBuilder<RestChannelBuilder, RestClientModel>(false);
65 #endif
68 internal void AddChannelBuilder<T, M>(bool force)
69 where T : IClientChannelBuilder<M>
70 where M : IWcfClientModel
72 if (force || !kernel.HasComponent(typeof(IClientChannelBuilder<M>)))
74 kernel.AddComponent<T>(typeof(IClientChannelBuilder<M>));
78 private void InstallManagedChannelInterceptor(ComponentModel model)
80 model.Dependencies.Add(new DependencyModel(DependencyType.Service, null,
81 typeof(WcfManagedChannelInterceptor), false));
82 model.Interceptors.Add(new InterceptorReference(typeof(WcfManagedChannelInterceptor)));
83 ProxyOptions options = ProxyUtil.ObtainProxyOptions(model, true);
84 options.AllowChangeTarget = true;
87 private IWcfClientModel ResolveClientModel(ComponentModel model)
90 if (model.Service.IsInterface)
92 foreach (IWcfClientModel clientModel in WcfUtils
93 .FindDependencies<IWcfClientModel>(model.CustomDependencies))
95 return clientModel;
99 if (model.Configuration != null)
101 string endpointConfiguration =
102 model.Configuration.Attributes[WcfConstants.EndpointConfiguration];
104 if (!string.IsNullOrEmpty(endpointConfiguration))
106 return new DefaultClientModel(WcfEndpoint.FromConfiguration(endpointConfiguration));
110 return null;
113 #region IDisposable Members
115 public void Dispose()
119 #endregion