Support ability to scope how global behaviors are applied to clients and services...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Behaviors / WcfExplicitBehavior.cs
blob2f26a8bdd5ebe56491642f86e5662f0a0d0c2aae
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 System.ServiceModel.Description;
19 using Castle.MicroKernel;
21 internal abstract class WcfExplcitBehavior : IWcfServiceBehavior, IWcfEndpointBehavior
24 #region IWcfServiceBehavior
26 public void Install(ServiceDescription description, IKernel kernel)
28 object behavior = GetBehaviorInstance(kernel);
30 if (behavior is IServiceBehavior)
32 description.Behaviors.Add((IServiceBehavior)behavior);
36 #endregion
38 #region IWcfEndpointBehavior
40 public void Install(ServiceEndpoint endpoint, IKernel kernel)
42 object behavior = GetBehaviorInstance(kernel);
44 if (behavior is IEndpointBehavior)
46 endpoint.Behaviors.Add((IEndpointBehavior)behavior);
48 else if (behavior is IOperationBehavior)
50 foreach (OperationDescription operation in endpoint.Contract.Operations)
52 operation.Behaviors.Add((IOperationBehavior)behavior);
57 #endregion
59 protected abstract object GetBehaviorInstance(IKernel kernel);
61 internal static WcfExplcitBehavior CreateFrom(object behavior)
63 if (behavior is Type)
65 return new WcfServiceTypeBehavior((Type)behavior);
67 else if (behavior is string)
69 return new WcfServiceKeyBehavior((string)behavior);
71 else
73 return new WcfInstanceBehavior(behavior);
77 public void Accept(IWcfBehaviorVisitor visitor)
79 visitor.VisitServiceBehavior(this);
80 visitor.VisitEndpointBehavior(this);
84 #region Class: WcfServiceKeyBehavior
86 internal class WcfServiceKeyBehavior : WcfExplcitBehavior
88 private readonly string key;
90 internal WcfServiceKeyBehavior(string key)
92 this.key = key;
95 protected override object GetBehaviorInstance(IKernel kernel)
97 return kernel[key];
101 #endregion
103 #region Class: WcfServiceTypeBehavior
105 internal class WcfServiceTypeBehavior : WcfExplcitBehavior
107 private readonly Type service;
109 internal WcfServiceTypeBehavior(Type service)
111 this.service = service;
114 protected override object GetBehaviorInstance(IKernel kernel)
116 return kernel.Resolve(service);
120 #endregion
122 #region Class: WcfInstanceBehavior
124 internal class WcfInstanceBehavior : WcfExplcitBehavior
126 private readonly object instance;
128 internal WcfInstanceBehavior(object instance)
130 this.instance = instance;
133 protected override object GetBehaviorInstance(IKernel kernel)
135 return instance;
139 #endregion