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 Castle
.MicroKernel
;
20 using System
.ServiceModel
;
21 using System
.ServiceModel
.Description
;
22 using System
.ServiceModel
.Channels
;
24 public abstract class AbstractChannelBuilder
<M
> : AbstractChannelBuilder
, IClientChannelBuilder
<M
>
25 where M
: IWcfClientModel
27 private M clientModel
;
29 public AbstractChannelBuilder(IKernel kernel
)
35 /// Get a delegate capable of creating channels.
37 /// <param name="clientModel">The client model.</param>
38 /// <returns>The <see cref="ChannelCreator"/></returns>
39 public ChannelCreator
GetChannelCreator(M clientModel
)
41 this.clientModel
= clientModel
;
42 return GetEndpointChannelCreator(clientModel
.Endpoint
);
46 /// Get a delegate capable of creating channels.
48 /// <param name="clientModel">The client model.</param>
49 /// <param name="contract">The contract override.</param>
50 /// <returns>The <see cref="ChannelCreator"/></returns>
51 public ChannelCreator
GetChannelCreator(M clientModel
, Type contract
)
53 this.clientModel
= clientModel
;
54 return GetEndpointChannelCreator(clientModel
.Endpoint
, contract
);
57 #region AbstractChannelBuilder Members
59 protected override ChannelCreator
GetChannelCreator(Type contract
, ServiceEndpoint endpoint
)
61 return GetChannelCreator(clientModel
, contract
, endpoint
);
64 protected override ChannelCreator
GetChannelCreator(Type contract
, string configurationName
)
66 return GetChannelCreator(clientModel
, contract
, configurationName
);
69 protected override ChannelCreator
GetChannelCreator(Type contract
, Binding binding
, string address
)
71 return GetChannelCreator(clientModel
, contract
, binding
, address
);
74 protected override ChannelCreator
GetChannelCreator(Type contract
, Binding binding
, EndpointAddress address
)
76 return GetChannelCreator(contract
, binding
, address
);
81 #region GetChannelCreator Members
83 protected virtual ChannelCreator
GetChannelCreator(M clientModel
, Type contract
,
84 ServiceEndpoint endpoint
)
86 return CreateChannelCreator(contract
, clientModel
, endpoint
);
89 protected virtual ChannelCreator
GetChannelCreator(M clientModel
, Type contract
,
90 string configurationName
)
92 return CreateChannelCreator(contract
, clientModel
, configurationName
);
95 protected virtual ChannelCreator
GetChannelCreator(M clientModel
, Type contract
,
96 Binding binding
, string address
)
98 return CreateChannelCreator(contract
, clientModel
, binding
, address
);
101 protected virtual ChannelCreator
GetChannelCreator(M clientModel
, Type contract
,
102 Binding binding
, EndpointAddress address
)
104 return CreateChannelCreator(contract
, clientModel
, binding
, address
);
107 protected virtual ChannelCreator
CreateChannelCreator(Type contract
, M clientModel
,
108 params object[] channelFactoryArgs
)
110 Type type
= typeof(ChannelFactory
<>).MakeGenericType(new Type
[] { contract }
);
112 ChannelFactory channelFactory
= (ChannelFactory
)
113 Activator
.CreateInstance(type
, channelFactoryArgs
);
114 channelFactory
.Opening
+= delegate { OnOpening(channelFactory, clientModel); }
;
116 MethodInfo methodInfo
= type
.GetMethod("CreateChannel", new Type
[0]);
117 return (ChannelCreator
)Delegate
.CreateDelegate(
118 typeof(ChannelCreator
), channelFactory
, methodInfo
);
121 protected virtual void OnOpening(ChannelFactory channelFactory
, M clientModel
)
123 ServiceEndpointBehaviors behaviors
=
124 new ServiceEndpointBehaviors(channelFactory
.Endpoint
, Kernel
)
125 .Install(new WcfEndpointBehaviors(WcfBehaviorScope
.Clients
));
127 if (clientModel
!= null)
129 foreach (IWcfBehavior behavior
in clientModel
.Behaviors
)
131 behaviors
.Install(behavior
);