Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / WcfEndpoint.cs
blobc85b15ec9d2f4df5816093ca5d678fdbbed0f4a9
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.Collections.Generic;
19 using System.ServiceModel;
20 using System.ServiceModel.Channels;
21 using System.ServiceModel.Description;
23 public static class WcfEndpoint
25 public static ServiceEndpointModel FromEndpoint(ServiceEndpoint endpoint)
27 return new ContractEndpointModel().FromEndpoint(endpoint);
30 public static ConfigurationEndpointModel FromConfiguration(string endpointName)
32 return new ContractEndpointModel().FromConfiguration(endpointName);
35 public static BindingEndpointModel BoundTo(Binding binding)
37 return new ContractEndpointModel().BoundTo(binding);
40 public static BindingAddressEndpointModel At(string address)
42 return new ContractEndpointModel().At(address);
45 public static BindingAddressEndpointModel At(Uri address)
47 return new ContractEndpointModel().At(address);
50 public static ContractEndpointModel ForContract(Type contract)
52 return new ContractEndpointModel(contract);
55 public static ContractEndpointModel ForContract<Contract>()
56 where Contract : class
58 return ForContract(typeof(Contract));
62 #region Nested Class: WcfEndpointBase
64 public abstract class WcfEndpointBase : IWcfEndpoint
66 private Type contract;
67 private ICollection<IWcfBehavior> behaviors;
69 protected WcfEndpointBase(Type contract)
71 this.contract = contract;
74 #region IWcfEndpoint Members
76 public Type Contract
78 get { return contract; }
81 Type IWcfEndpoint.Contract
83 get { return contract; }
84 set { contract = value; }
87 public ICollection<IWcfBehavior> Behaviors
89 get
91 if (behaviors == null)
93 behaviors = new List<IWcfBehavior>();
95 return behaviors;
99 void IWcfEndpoint.Accept(IWcfEndpointVisitor visitor)
101 Accept(visitor);
104 protected abstract void Accept(IWcfEndpointVisitor visitor);
106 #endregion
109 public abstract class WcfEndpointBase<T> : WcfEndpointBase
110 where T : WcfEndpointBase<T>
112 protected WcfEndpointBase(Type contract)
113 : base(contract)
117 public T AddBehaviors(params object[] behaviors)
119 foreach (object behavior in behaviors)
121 Behaviors.Add(WcfExplcitBehavior.CreateFrom(behavior));
123 return (T)this;
127 #endregion
129 #region Nested Class: ContractModel
131 public class ContractEndpointModel : WcfEndpointBase<ContractEndpointModel>
133 internal ContractEndpointModel()
134 : this(null)
138 internal ContractEndpointModel(Type contract)
139 : base(contract)
143 public ServiceEndpointModel FromEndpoint(ServiceEndpoint endpoint)
145 if (endpoint == null)
147 throw new ArgumentNullException("endpoint");
150 return new ServiceEndpointModel(Contract, endpoint);
153 public ConfigurationEndpointModel FromConfiguration(string endpointName)
155 if (string.IsNullOrEmpty(endpointName))
157 throw new ArgumentException("endpointName cannot be nul or empty");
159 return new ConfigurationEndpointModel(Contract, endpointName);
162 public BindingEndpointModel BoundTo(Binding binding)
164 if (binding == null)
166 throw new ArgumentNullException("binding");
168 return new BindingEndpointModel(Contract, binding);
171 public BindingAddressEndpointModel At(string address)
173 return new BindingEndpointModel(Contract, null).At(address);
176 public BindingAddressEndpointModel At(Uri address)
178 return new BindingEndpointModel(Contract, null).At(address);
181 protected override void Accept(IWcfEndpointVisitor visitor)
183 visitor.VisitContractEndpoint(this);
187 #endregion
189 #region Nested Class: ServiceEndpointModel
191 public class ServiceEndpointModel : WcfEndpointBase<ServiceEndpointModel>
193 private readonly ServiceEndpoint endpoint;
195 internal ServiceEndpointModel(Type contract, ServiceEndpoint endpoint)
196 : base(contract)
198 this.endpoint = endpoint;
201 public ServiceEndpoint ServiceEndpoint
203 get { return endpoint; }
206 protected override void Accept(IWcfEndpointVisitor visitor)
208 visitor.VisitServiceEndpoint(this);
212 #endregion
214 #region Nested Class: ConfigurationEndpointModel
216 public class ConfigurationEndpointModel : WcfEndpointBase<ConfigurationEndpointModel>
218 private readonly string endpointName;
220 internal ConfigurationEndpointModel(Type contract, string endpointName)
221 : base(contract)
223 this.endpointName = endpointName;
226 public string EndpointName
228 get { return endpointName; }
231 protected override void Accept(IWcfEndpointVisitor visitor)
233 visitor.VisitConfigurationEndpoint(this);
237 #endregion
239 #region Nested Class: BindingEndpointModel
241 public class BindingEndpointModel : WcfEndpointBase<BindingEndpointModel>
243 private readonly Binding binding;
245 internal BindingEndpointModel(Type contract, Binding binding)
246 : base(contract)
248 this.binding = binding;
251 public Binding Binding
253 get { return binding; }
256 public BindingAddressEndpointModel At(string address)
258 if (string.IsNullOrEmpty(address))
260 throw new ArgumentException("address cannot be null or empty");
262 return new BindingAddressEndpointModel(Contract, Binding, address);
265 public BindingAddressEndpointModel At(Uri address)
267 if (address == null)
269 throw new ArgumentNullException("address");
271 return new BindingAddressEndpointModel(Contract, Binding, address.AbsoluteUri);
274 public BindingAddressEndpointModel At(EndpointAddress address)
276 if (address == null)
278 throw new ArgumentNullException("address");
280 return new BindingAddressEndpointModel(Contract, Binding, address);
283 protected override void Accept(IWcfEndpointVisitor visitor)
285 visitor.VisitBindingEndpoint(this);
289 #endregion
291 #region Nested Class: BindingAddressEndpointModel
293 public class BindingAddressEndpointModel : WcfEndpointBase<BindingAddressEndpointModel>
295 private readonly Binding binding;
296 private readonly string address;
297 private readonly EndpointAddress endpointAddress;
298 private string via;
300 internal BindingAddressEndpointModel(Type contract, Binding binding,
301 string address)
302 : base(contract)
304 this.binding = binding;
305 this.address = address;
308 internal BindingAddressEndpointModel(Type contract, Binding binding,
309 EndpointAddress address)
310 : base(contract)
312 this.binding = binding;
313 this.endpointAddress = address;
316 public Binding Binding
318 get { return binding; }
321 public string Address
323 get { return address ?? endpointAddress.Uri.AbsoluteUri; }
326 public EndpointAddress EndpointAddress
328 get { return endpointAddress; }
331 public Uri ViaAddress
333 get { return new Uri(via, UriKind.Absolute); }
336 public bool HasViaAddress
338 get { return !string.IsNullOrEmpty(via); }
341 public BindingAddressEndpointModel Via(string physicalAddress)
343 via = physicalAddress;
344 return this;
347 protected override void Accept(IWcfEndpointVisitor visitor)
349 visitor.VisitBindingAddressEndpoint(this);
353 #endregion