Support multiple WCF Service Models in addition to multiple endpoints per Service...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / Internal / WcfUtils.cs
blobecec47a750ba9ba22fed97d097cbacf4800f0a0f
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 IsHosted(IWcfServiceModel serviceModel)
28 return serviceModel.IsHosted;
31 public static IEnumerable<T> FindDependencies<T>(IDictionary dependencies)
33 return FindDependencies<T>(dependencies, null);
36 public static IEnumerable<T> FindDependencies<T>(IDictionary dependencies,
37 Predicate<T> test)
39 foreach (object dependency in dependencies.Values)
41 if (dependency is T)
43 T candidate = (T)dependency;
45 if (test == null || test(candidate))
47 yield return candidate;
53 public static IEnumerable<T> FindDependencies<T>(ICollection<T> dependencies)
55 return FindDependencies<T>(dependencies, null);
58 public static IEnumerable<T> FindDependencies<T>(ICollection<T> dependencies,
59 Predicate<T> test)
61 foreach (object dependency in dependencies)
63 if (dependency is T)
65 T candidate = (T)dependency;
67 if (test == null || test(candidate))
69 yield return candidate;
75 public static ICollection<IHandler> FindBehaviors<T>(IKernel kernel, WcfBehaviorScope scope)
77 List<IHandler> handlers = new List<IHandler>();
78 foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(T)))
80 ComponentModel model = handler.ComponentModel;
81 if (model.Configuration != null)
83 string scopeAttrib = model.Configuration.Attributes[WcfConstants.BehaviorScopeKey];
84 if (string.IsNullOrEmpty(scopeAttrib) ||
85 scopeAttrib.Equals(scope.ToString(), StringComparison.InvariantCultureIgnoreCase))
87 handlers.Add(handler);
91 return handlers;
94 public static bool IsCommunicationObjectReady(ICommunicationObject comm)
96 switch (comm.State)
98 case CommunicationState.Closed:
99 case CommunicationState.Closing:
100 case CommunicationState.Faulted:
101 return false;
103 return true;
106 public static void ReleaseCommunicationObject(ICommunicationObject comm)
108 if (comm != null)
110 if (comm.State != CommunicationState.Faulted)
114 comm.Close();
116 catch (CommunicationException)
118 comm.Abort();
120 catch (TimeoutException)
122 comm.Abort();
124 catch
126 comm.Abort();
127 throw;
130 else
132 comm.Abort();