1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Reflection
;
19 using System
.ServiceModel
;
20 using System
.ServiceModel
.Channels
;
21 using System
.ServiceModel
.Description
;
22 using Castle
.MicroKernel
;
25 /// The default implementation of <see cref="IClientChannelBuilder{M}"/>.
27 public class DefaultChannelBuilder
: AbstractChannelBuilder
<WcfClientModel
>
29 private readonly IKernel kernel
;
31 public DefaultChannelBuilder(IKernel kernel
)
36 protected override ChannelCreator
GetChannelCreator(WcfClientModel clientModel
, Type contract
,
37 ServiceEndpoint endpoint
)
39 return CreateChannelCreator(contract
, endpoint
);
42 protected override ChannelCreator
GetChannelCreator(WcfClientModel clientMode
, Type contract
,
43 string configurationName
)
45 return CreateChannelCreator(contract
, configurationName
);
48 protected override ChannelCreator
GetChannelCreator(WcfClientModel clientModel
, Type contract
,
49 Binding binding
, string address
)
51 return CreateChannelCreator(contract
, binding
, address
);
54 protected override ChannelCreator
GetChannelCreator(WcfClientModel clientModel
, Type contract
,
55 Binding binding
, EndpointAddress address
)
57 return CreateChannelCreator(contract
, binding
, address
);
60 private ChannelCreator
CreateChannelCreator(Type contract
, params object[] channelFactoryArgs
)
62 Type type
= typeof(ChannelFactory
<>).MakeGenericType(new Type
[] { contract }
);
64 ChannelFactory channelFactory
= (ChannelFactory
)
65 Activator
.CreateInstance(type
, channelFactoryArgs
);
66 channelFactory
.Opening
+= ChannelFactory_Opening
;
68 MethodInfo methodInfo
= type
.GetMethod("CreateChannel", new Type
[0]);
69 return (ChannelCreator
)Delegate
.CreateDelegate(
70 typeof(ChannelCreator
), channelFactory
, methodInfo
);
73 private void ChannelFactory_Opening(object sender
, EventArgs e
)
75 ChannelFactory channelFactory
= (ChannelFactory
)sender
;
76 ServiceEndpoint endpoint
= channelFactory
.Endpoint
;
78 IHandler
[] endPointBehaviors
= kernel
.GetAssignableHandlers(typeof(IEndpointBehavior
));
79 foreach (IHandler handler
in endPointBehaviors
)
81 endpoint
.Behaviors
.Add((IEndpointBehavior
)handler
.Resolve(CreationContext
.Empty
));
84 IHandler
[] operationBehaviors
= kernel
.GetAssignableHandlers(typeof(IOperationBehavior
));
85 foreach (OperationDescription operation
in endpoint
.Contract
.Operations
)
87 foreach (IHandler operationHandler
in operationBehaviors
)
89 operation
.Behaviors
.Add((IOperationBehavior
)
90 operationHandler
.Resolve(CreationContext
.Empty
));