Merge pull request #823 from DavidKarlas/master
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Description / ServiceEndpoint.cs
1 //
2 // ServiceEndpoint.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 #if NET_4_5
31 using System.Collections.ObjectModel;
32 #endif
33 using System.Diagnostics;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Dispatcher;
37
38 namespace System.ServiceModel.Description
39 {
40         [DebuggerDisplay ("Name={name}")]
41         [DebuggerDisplay ("Address={address}")]
42         public class ServiceEndpoint
43         {
44                 ContractDescription contract;
45                 Binding binding;
46                 EndpointAddress address;
47                 KeyedByTypeCollection<IEndpointBehavior> behaviors;
48                 Uri listen_uri;
49                 ListenUriMode listen_mode;
50                 string name;
51
52                 public ServiceEndpoint (ContractDescription contract)
53                         : this (contract, null, null)
54                 {
55                 }
56
57                 public ServiceEndpoint(ContractDescription contract,
58                         Binding binding, EndpointAddress address)
59                 {
60                         if (contract == null)
61                                 throw new ArgumentNullException ("contract");
62
63                         this.contract = contract;
64                         this.binding = binding;
65                         this.address = address;
66                         behaviors = new KeyedByTypeCollection<IEndpointBehavior> ();
67                 }
68
69                 public KeyedByTypeCollection<IEndpointBehavior> Behaviors {
70                         get { return behaviors; }
71                 }
72
73 #if NET_4_5
74                 [MonoTODO]
75                 public KeyedCollection<Type,IEndpointBehavior> EndpointBehaviors {
76                         get { throw new NotImplementedException (); }
77                 }
78 #endif
79
80                 public ContractDescription Contract {
81                         get { return contract; }
82 #if NET_4_0
83                         set {
84                                 if (value == null)
85                                         throw new ArgumentNullException ("value");
86                                 contract = value;
87                         }
88 #endif
89                 }
90
91                 public EndpointAddress Address {
92                         get { return address; }
93                         set { address = value; }
94                 }
95
96                 public Binding Binding {
97                         get { return binding; }
98                         set { binding = value; }
99                 }
100
101 #if NET_4_0
102                 public
103 #else
104                 internal
105 #endif
106                 bool IsSystemEndpoint { get; set; }
107
108                 public Uri ListenUri {
109                         get { return listen_uri ?? (Address != null ? Address.Uri : null); }
110                         set { listen_uri = value; }
111                 }
112
113                 public ListenUriMode ListenUriMode {
114                         get { return listen_mode; }
115                         set { listen_mode = value; }
116                 }
117
118                 public string Name {
119                         get {
120                                 if (name == null) {
121                                         // do not create cache when either of Binding or Contract is null.
122                                         if (Binding == null)
123                                                 return Contract != null ? Contract.Name : null;
124                                         else if (Contract != null)
125                                                 name = Binding.Name + "_" + Contract.Name;
126                                 }
127                                 return name;
128                         }
129                         set { name = value; }
130                 }
131
132                 internal void Validate ()
133                 {
134                         if (Contract.Operations.Count == 0)
135                                 throw new InvalidOperationException (String.Format ("ContractDescription '{0}' has zero operations; a contract must have at least one operation.", Contract.ContractType.Name));
136
137                         foreach (IContractBehavior b in Contract.Behaviors)
138                                 b.Validate (Contract, this);
139                         foreach (IEndpointBehavior b in Behaviors)
140                                 b.Validate (this);
141                         foreach (OperationDescription operation in Contract.Operations) {
142                                 foreach (IOperationBehavior b in operation.Behaviors)
143                                         b.Validate (operation);
144                         }
145                 }
146
147
148                 internal ClientRuntime CreateClientRuntime (object callbackDispatchRuntime)
149                 {
150                         ServiceEndpoint se = this;
151
152                         var proxy = se.Contract.CreateClientRuntime (callbackDispatchRuntime);
153
154                         foreach (IEndpointBehavior b in se.Behaviors)
155                                 b.ApplyClientBehavior (se, proxy);
156                         foreach (IContractBehavior b in se.Contract.Behaviors)
157                                 b.ApplyClientBehavior (se.Contract, se, proxy);
158
159                         return proxy;
160                 }
161         }
162 }