Support ability to scope how global behaviors are applied to clients and services...
[castle.git] / Facilities / Wcf / Castle.Facilities.WcfIntegration / WcfEndpoint.cs
blob5d575148b5e5ada26a388c826395464ef1669810
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.ServiceModel;
19 using System.ServiceModel.Channels;
20 using System.ServiceModel.Description;
22 public static class WcfEndpoint
24 public static ServiceEndpointModel FromEndpoint(ServiceEndpoint endpoint)
26 return new ContractEndpointModel().FromEndpoint(endpoint);
29 public static ConfigurationEndpointModel FromConfiguration(string endpointName)
31 return new ContractEndpointModel().FromConfiguration(endpointName);
34 public static BindingEndpointModel BoundTo(Binding binding)
36 return new ContractEndpointModel().BoundTo(binding);
39 public static BindingAddressEndpointModel At(string address)
41 return new ContractEndpointModel().At(address);
44 public static BindingAddressEndpointModel At(Uri address)
46 return new ContractEndpointModel().At(address);
49 public static ContractEndpointModel ForContract(Type contract)
51 return new ContractEndpointModel(contract);
54 public static ContractEndpointModel ForContract<Contract>()
55 where Contract : class
57 return ForContract(typeof(Contract));
61 #region Nested Class: WcfEndpointBase
63 public abstract class WcfEndpointBase : IWcfEndpoint
65 private Type contract;
67 protected WcfEndpointBase(Type contract)
69 this.contract = contract;
72 #region IWcfEndpoint Members
74 public Type Contract
76 get { return contract; }
79 Type IWcfEndpoint.Contract
81 get { return contract; }
82 set { contract = value; }
85 void IWcfEndpoint.Accept(IWcfEndpointVisitor visitor)
87 Accept(visitor);
90 protected abstract void Accept(IWcfEndpointVisitor visitor);
92 #endregion
95 #endregion
97 #region Nested Class: ContractModel
99 public class ContractEndpointModel
101 private readonly Type contract;
103 internal ContractEndpointModel()
107 internal ContractEndpointModel(Type contract)
109 this.contract = contract;
112 public ServiceEndpointModel FromEndpoint(ServiceEndpoint endpoint)
114 if (endpoint == null)
116 throw new ArgumentNullException("endpoint");
119 return new ServiceEndpointModel(contract, endpoint);
122 public ConfigurationEndpointModel FromConfiguration(string endpointName)
124 if (string.IsNullOrEmpty(endpointName))
126 throw new ArgumentException("endpointName cannot be nul or empty");
128 return new ConfigurationEndpointModel(contract, endpointName);
131 public BindingEndpointModel BoundTo(Binding binding)
133 if (binding == null)
135 throw new ArgumentNullException("binding");
137 return new BindingEndpointModel(contract, binding);
140 public BindingAddressEndpointModel At(string address)
142 return new BindingEndpointModel(contract, null).At(address);
145 public BindingAddressEndpointModel At(Uri address)
147 return new BindingEndpointModel(contract, null).At(address);
151 #endregion
153 #region Nested Class: ServiceEndpointModel
155 public class ServiceEndpointModel : WcfEndpointBase
157 private readonly ServiceEndpoint endpoint;
159 internal ServiceEndpointModel(Type contract, ServiceEndpoint endpoint)
160 : base(contract)
162 this.endpoint = endpoint;
165 public ServiceEndpoint ServiceEndpoint
167 get { return endpoint; }
170 protected override void Accept(IWcfEndpointVisitor visitor)
172 visitor.VisitServiceEndpoint(this);
176 #endregion
178 #region Nested Class: ConfigurationEndpointModel
180 public class ConfigurationEndpointModel : WcfEndpointBase
182 private readonly string endpointName;
184 internal ConfigurationEndpointModel(Type contract, string endpointName)
185 : base(contract)
187 this.endpointName = endpointName;
190 public string EndpointName
192 get { return endpointName; }
195 protected override void Accept(IWcfEndpointVisitor visitor)
197 visitor.VisitConfigurationEndpoint(this);
201 #endregion
203 #region Nested Class: BindingEndpointModel
205 public class BindingEndpointModel : WcfEndpointBase
207 private readonly Binding binding;
209 internal BindingEndpointModel(Type contract, Binding binding)
210 : base(contract)
212 this.binding = binding;
215 public Binding Binding
217 get { return binding; }
220 public BindingAddressEndpointModel At(string address)
222 if (string.IsNullOrEmpty(address))
224 throw new ArgumentException("address cannot be null or empty");
226 return new BindingAddressEndpointModel(Contract, Binding, address);
229 public BindingAddressEndpointModel At(Uri address)
231 if (address == null)
233 throw new ArgumentNullException("address");
235 return new BindingAddressEndpointModel(Contract, Binding, address.AbsoluteUri);
238 public BindingAddressEndpointModel At(EndpointAddress address)
240 if (address == null)
242 throw new ArgumentNullException("address");
244 return new BindingAddressEndpointModel(Contract, Binding, address);
247 protected override void Accept(IWcfEndpointVisitor visitor)
249 visitor.VisitBindingEndpoint(this);
253 #endregion
255 #region Nested Class: BindingAddressEndpointModel
257 public class BindingAddressEndpointModel : WcfEndpointBase
259 private readonly Binding binding;
260 private readonly string address;
261 private readonly EndpointAddress endpointAddress;
262 private string via;
264 internal BindingAddressEndpointModel(Type contract, Binding binding,
265 string address)
266 : base(contract)
268 this.binding = binding;
269 this.address = address;
272 internal BindingAddressEndpointModel(Type contract, Binding binding,
273 EndpointAddress address)
274 : base(contract)
276 this.binding = binding;
277 this.endpointAddress = address;
280 public Binding Binding
282 get { return binding; }
285 public string Address
287 get { return address ?? endpointAddress.Uri.AbsoluteUri; }
290 public EndpointAddress EndpointAddress
292 get { return endpointAddress; }
295 public Uri ViaAddress
297 get { return new Uri(via, UriKind.Absolute); }
300 public bool HasViaAddress
302 get { return !string.IsNullOrEmpty(via); }
305 public BindingAddressEndpointModel Via(string physicalAddress)
307 via = physicalAddress;
308 return this;
311 protected override void Accept(IWcfEndpointVisitor visitor)
313 visitor.VisitBindingAddressEndpoint(this);
317 #endregion