Add unit tests to demonstrate resolving client dependencies automatically and supplyi...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / WcfClientModel.cs
blobd4f8e5dc3a8dbc755a723c84ffc82853f6aadfeb
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.Reflection;
19 using System.ServiceModel;
21 public class WcfClientModel : WcfEndpoint
23 public WcfClientModel()
27 public WcfClientModel(Type contract)
28 : base(contract)
32 public WcfClientModel(string endpointName)
34 EndpointName = endpointName;
37 public WcfClientModel(Type contract, string endpointName)
38 : this(contract)
40 EndpointName = endpointName;
44 internal CreateChannel GetChannelBuilder()
46 return GetChannelBuilder(Contract);
49 internal CreateChannel GetChannelBuilder(Type contract)
51 object target;
52 contract = contract ?? Contract;
53 Type type = typeof(ChannelFactory<>).MakeGenericType(new Type[] { contract });
55 if (!string.IsNullOrEmpty(EndpointName))
57 target = Activator.CreateInstance(type, new object[] { EndpointName });
59 else
61 EndpointAddress address = EndpointAddress;
62 if (address == null)
64 address = new EndpointAddress(Address);
66 target = Activator.CreateInstance(type, new object[] { Binding, address });
69 MethodInfo methodInfo = type.GetMethod("CreateChannel", new Type[0]);
70 return (CreateChannel) Delegate.CreateDelegate(typeof(CreateChannel), target, methodInfo);
74 public class WcfClientModel<Contract> : WcfClientModel
76 public WcfClientModel()
77 : base(typeof(Contract))
81 public WcfClientModel(string endpointName) : this()
83 EndpointName = endpointName;