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