Support ability to scope how global behaviors are applied to clients and services...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Internal / WcfUtils.cs
blobcdec13c214f7d870c646233b74777bdeb6580e7a
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.Internal
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.ServiceModel;
21 using Castle.Core;
22 using Castle.MicroKernel;
24 internal static class WcfUtils
26 public static bool FindDependency<T>(IDictionary dependencies,
27 out T match)
29 return FindDependency<T>(dependencies, null, out match);
32 public static bool FindDependency<T>(IDictionary dependencies,
33 Predicate<T> test, out T match)
35 match = default(T);
37 foreach (object dependency in dependencies.Values)
39 if (dependency is T)
41 T candidate = (T)dependency;
43 if (test == null || test(candidate))
45 match = candidate;
46 return true;
50 return false;
53 public static bool FindDependency<T>(ICollection<T> dependencies,
54 out T match)
56 return FindDependency<T>(dependencies, null, out match);
59 public static bool FindDependency<T>(ICollection<T> dependencies,
60 Predicate<T> test, out T match)
62 match = default(T);
64 foreach (object dependency in dependencies)
66 if (dependency is T)
68 T candidate = (T)dependency;
70 if (test == null || test(candidate))
72 match = candidate;
73 return true;
77 return false;
80 public static ICollection<IHandler> FindBehaviors<T>(IKernel kernel, WcfBehaviorScope scope)
82 List<IHandler> handlers = new List<IHandler>();
83 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(T)))
85 ComponentModel model = handler.ComponentModel;
86 if (model.Configuration != null)
88 string scopeAttrib = model.Configuration.Attributes[WcfConstants.BehaviorScopeKey];
89 if (string.IsNullOrEmpty(scopeAttrib) ||
90 scopeAttrib.Equals(scope.ToString(), StringComparison.InvariantCultureIgnoreCase))
92 handlers.Add(handler);
96 return handlers;
99 public static bool IsCommunicationObjectReady(ICommunicationObject comm)
101 switch (comm.State)
103 case CommunicationState.Closed:
104 case CommunicationState.Closing:
105 case CommunicationState.Faulted:
106 return false;
108 return true;
111 public static void ReleaseCommunicationObject(ICommunicationObject comm)
113 if (comm != null)
115 if (comm.State != CommunicationState.Faulted)
119 comm.Close();
121 catch (CommunicationException)
123 comm.Abort();
125 catch (TimeoutException)
127 comm.Abort();
129 catch
131 comm.Abort();
132 throw;
135 else
137 comm.Abort();