[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Dispatcher / Bug652331Test.cs
1 //
2 // Authors:
3 //      David Straw
4 //      Atsushi Enomoto <atsushi@ximian.com>
5 //
6 // Copyright (C) 2011 Novell, Inc.  http://www.novell.com
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Runtime.Serialization;
31 using System.ServiceModel;
32 using System.ServiceModel.Description;
33 using System.Threading;
34 using NUnit.Framework;
35
36 using WebServiceMoonlightTest.ServiceReference1;
37
38 using MonoTests.Helpers;
39
40 namespace MonoTests.System.ServiceModel.Dispatcher
41 {
42         [TestFixture]
43         public class Bug652331Test
44         {
45                 [Test]
46                 public void Bug652331_2 () // test in one of the comment
47                 {
48                         // Init service
49                         int port = NetworkHelpers.FindFreePort ();
50                         ServiceHost serviceHost = new ServiceHost (typeof (Service1), new Uri ("http://localhost:" + port + "/Service1"));
51                         serviceHost.AddServiceEndpoint (typeof (IService1), new BasicHttpBinding (), string.Empty);
52
53                         // Enable metadata exchange (WSDL publishing)
54                         var mexBehavior = new ServiceMetadataBehavior ();
55                         mexBehavior.HttpGetEnabled = true;
56                         serviceHost.Description.Behaviors.Add (mexBehavior);
57                         serviceHost.AddServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
58
59                         serviceHost.Open ();
60
61                         try {
62                                 // client
63                                 var binding = new BasicHttpBinding ();
64                                 var remoteAddress = new EndpointAddress ("http://localhost:" + port + "/Service1");
65                                 var client = new Service1Client (binding, remoteAddress);
66
67                                 var wait = new ManualResetEvent (false);
68                                 client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
69                                         if (e.Error != null)
70                                                 throw e.Error;
71                                         Assert.AreEqual ("A", ((DataType1) e.Result).Id, "#1");
72                                         wait.Set ();
73                                 };
74
75                                 client.GetDataAsync ();
76                                 if (!wait.WaitOne (TimeSpan.FromSeconds (20)))
77                                         Assert.Fail ("timeout");
78                         } finally {
79                                 serviceHost.Close ();
80                         }
81                 }
82
83                 public class Service1 : IService1
84                 {
85                         public object GetData ()
86                         {
87                                 return new DataType1 { Id = "A" };
88                         }
89
90                         Func<object> d;
91                         public IAsyncResult BeginGetData (AsyncCallback callback, object state)
92                         {
93                                 if (d == null)
94                                 d = new Func<object> (GetData);
95                                 return d.BeginInvoke (callback, state);
96                         }
97
98                         public object EndGetData (IAsyncResult result)
99                         {
100                                 return d.EndInvoke (result);
101                         }
102                 }
103         }
104 }
105
106 // below are part of autogenerated code in comment #1 on bug #652331.
107
108 namespace WebServiceMoonlightTest.ServiceReference1 {
109
110     [System.Diagnostics.DebuggerStepThroughAttribute()]
111     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
112     [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
113     public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
114         
115         private string IdField;
116         
117         [System.Runtime.Serialization.DataMemberAttribute()]
118         public string Id {
119             get {
120                 return this.IdField;
121             }
122             set {
123                 if ((object.ReferenceEquals(this.IdField, value) != true)) {
124                     this.IdField = value;
125                     this.RaisePropertyChanged("Id");
126                 }
127             }
128         }
129         
130         public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
131         
132         protected void RaisePropertyChanged(string propertyName) {
133             System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
134             if ((propertyChanged != null)) {
135                 propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
136             }
137         }
138     }
139     
140     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
141     [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
142 [ServiceKnownType (typeof (DataType1))]
143     public interface IService1 {
144         
145         [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
146         [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
147         System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
148         
149         object EndGetData(System.IAsyncResult result);
150     }
151     
152     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
153     public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
154     }
155     
156     [System.Diagnostics.DebuggerStepThroughAttribute()]
157     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
158     public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
159         
160         private object[] results;
161         
162         public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
163                 base(exception, cancelled, userState) {
164             this.results = results;
165         }
166         
167         public object Result {
168             get {
169                 base.RaiseExceptionIfNecessary();
170                 return ((object)(this.results[0]));
171             }
172         }
173     }
174     
175     [System.Diagnostics.DebuggerStepThroughAttribute()]
176     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
177     public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
178         
179         private BeginOperationDelegate onBeginGetDataDelegate;
180         
181         private EndOperationDelegate onEndGetDataDelegate;
182         
183         private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
184         
185         private BeginOperationDelegate onBeginOpenDelegate;
186         
187         private EndOperationDelegate onEndOpenDelegate;
188         
189         private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
190         
191         private BeginOperationDelegate onBeginCloseDelegate;
192         
193         private EndOperationDelegate onEndCloseDelegate;
194         
195         private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
196         
197         public Service1Client() {
198         }
199         
200         public Service1Client(string endpointConfigurationName) : 
201                 base(endpointConfigurationName) {
202         }
203         
204         public Service1Client(string endpointConfigurationName, string remoteAddress) : 
205                 base(endpointConfigurationName, remoteAddress) {
206         }
207         
208         public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
209                 base(endpointConfigurationName, remoteAddress) {
210         }
211         
212         public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
213                 base(binding, remoteAddress) {
214         }
215         
216 /*
217         public System.Net.CookieContainer CookieContainer {
218             get {
219                 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
220                 if ((httpCookieContainerManager != null)) {
221                     return httpCookieContainerManager.CookieContainer;
222                 }
223                 else {
224                     return null;
225                 }
226             }
227             set {
228                 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
229                 if ((httpCookieContainerManager != null)) {
230                     httpCookieContainerManager.CookieContainer = value;
231                 }
232                 else {
233                     throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
234                             "ookieContainerBindingElement.");
235                 }
236             }
237         }
238 */
239         
240         public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
241         
242         public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
243         
244         public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
245         
246         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
247         System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
248             return base.Channel.BeginGetData(callback, asyncState);
249         }
250         
251         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
252         object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
253             return base.Channel.EndGetData(result);
254         }
255         
256         private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
257             return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
258         }
259         
260         private object[] OnEndGetData(System.IAsyncResult result) {
261             object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
262             return new object[] {
263                     retVal};
264         }
265         
266         private void OnGetDataCompleted(object state) {
267             if ((this.GetDataCompleted != null)) {
268                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
269                 this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
270             }
271         }
272         
273         public void GetDataAsync() {
274             this.GetDataAsync(null);
275         }
276         
277         public void GetDataAsync(object userState) {
278             if ((this.onBeginGetDataDelegate == null)) {
279                 this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
280             }
281             if ((this.onEndGetDataDelegate == null)) {
282                 this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
283             }
284             if ((this.onGetDataCompletedDelegate == null)) {
285                 this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
286             }
287             base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
288         }
289         
290         private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
291             return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
292         }
293         
294         private object[] OnEndOpen(System.IAsyncResult result) {
295             ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
296             return null;
297         }
298         
299         private void OnOpenCompleted(object state) {
300             if ((this.OpenCompleted != null)) {
301                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
302                 this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
303             }
304         }
305         
306         public void OpenAsync() {
307             this.OpenAsync(null);
308         }
309         
310         public void OpenAsync(object userState) {
311             if ((this.onBeginOpenDelegate == null)) {
312                 this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
313             }
314             if ((this.onEndOpenDelegate == null)) {
315                 this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
316             }
317             if ((this.onOpenCompletedDelegate == null)) {
318                 this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
319             }
320             base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
321         }
322         
323         private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
324             return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
325         }
326         
327         private object[] OnEndClose(System.IAsyncResult result) {
328             ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
329             return null;
330         }
331         
332         private void OnCloseCompleted(object state) {
333             if ((this.CloseCompleted != null)) {
334                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
335                 this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
336             }
337         }
338         
339         public void CloseAsync() {
340             this.CloseAsync(null);
341         }
342         
343         public void CloseAsync(object userState) {
344             if ((this.onBeginCloseDelegate == null)) {
345                 this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
346             }
347             if ((this.onEndCloseDelegate == null)) {
348                 this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
349             }
350             if ((this.onCloseCompletedDelegate == null)) {
351                 this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
352             }
353             base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
354         }
355         
356 /*
357         protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
358             return new Service1ClientChannel(this);
359         }
360         
361         private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
362             
363             public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) : 
364                     base(client) {
365             }
366             
367             public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
368                 object[] _args = new object[0];
369                 System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
370                 return _result;
371             }
372             
373             public object EndGetData(System.IAsyncResult result) {
374                 object[] _args = new object[0];
375                 object _result = ((object)(base.EndInvoke("GetData", _args, result)));
376                 return _result;
377             }
378         }
379 */
380     }
381 }