Merge pull request #409 from Alkarex/patch-1
[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 using System.Diagnostics;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Dispatcher;
34
35 namespace System.ServiceModel.Description
36 {
37         [DebuggerDisplay ("Name={name}")]
38         [DebuggerDisplay ("Address={address}")]
39         public class ServiceEndpoint
40         {
41                 ContractDescription contract;
42                 Binding binding;
43                 EndpointAddress address;
44                 KeyedByTypeCollection<IEndpointBehavior> behaviors;
45                 Uri listen_uri;
46                 ListenUriMode listen_mode;
47                 string name;
48
49                 public ServiceEndpoint (ContractDescription contract)
50                         : this (contract, null, null)
51                 {
52                 }
53
54                 public ServiceEndpoint(ContractDescription contract,
55                         Binding binding, EndpointAddress address)
56                 {
57                         if (contract == null)
58                                 throw new ArgumentNullException ("contract");
59
60                         this.contract = contract;
61                         this.binding = binding;
62                         this.address = address;
63                         behaviors = new KeyedByTypeCollection<IEndpointBehavior> ();
64                 }
65
66                 public KeyedByTypeCollection<IEndpointBehavior> Behaviors {
67                         get { return behaviors; }
68                 }
69
70                 public ContractDescription Contract {
71                         get { return contract; }
72 #if NET_4_0
73                         set {
74                                 if (value == null)
75                                         throw new ArgumentNullException ("value");
76                                 contract = value;
77                         }
78 #endif
79                 }
80
81                 public EndpointAddress Address {
82                         get { return address; }
83                         set { address = value; }
84                 }
85
86                 public Binding Binding {
87                         get { return binding; }
88                         set { binding = value; }
89                 }
90
91 #if NET_4_0
92                 public
93 #else
94                 internal
95 #endif
96                 bool IsSystemEndpoint { get; set; }
97
98                 public Uri ListenUri {
99                         get { return listen_uri ?? (Address != null ? Address.Uri : null); }
100                         set { listen_uri = value; }
101                 }
102
103                 public ListenUriMode ListenUriMode {
104                         get { return listen_mode; }
105                         set { listen_mode = value; }
106                 }
107
108                 public string Name {
109                         get {
110                                 if (name == null) {
111                                         // do not create cache when either of Binding or Contract is null.
112                                         if (Binding == null)
113                                                 return Contract != null ? Contract.Name : null;
114                                         else if (Contract != null)
115                                                 name = Binding.Name + "_" + Contract.Name;
116                                 }
117                                 return name;
118                         }
119                         set { name = value; }
120                 }
121
122                 internal void Validate ()
123                 {
124                         if (Contract.Operations.Count == 0)
125                                 throw new InvalidOperationException (String.Format ("ContractDescription '{0}' has zero operations; a contract must have at least one operation.", Contract.ContractType.Name));
126
127                         foreach (IContractBehavior b in Contract.Behaviors)
128                                 b.Validate (Contract, this);
129                         foreach (IEndpointBehavior b in Behaviors)
130                                 b.Validate (this);
131                         foreach (OperationDescription operation in Contract.Operations) {
132                                 foreach (IOperationBehavior b in operation.Behaviors)
133                                         b.Validate (operation);
134                         }
135                 }
136
137
138                 internal ClientRuntime CreateClientRuntime (object callbackDispatchRuntime)
139                 {
140                         ServiceEndpoint se = this;
141
142                         var proxy = se.Contract.CreateClientRuntime (callbackDispatchRuntime);
143
144                         foreach (IEndpointBehavior b in se.Behaviors)
145                                 b.ApplyClientBehavior (se, proxy);
146                         foreach (IContractBehavior b in se.Contract.Behaviors)
147                                 b.ApplyClientBehavior (se.Contract, se, proxy);
148
149                         return proxy;
150                 }
151         }
152 }