Merge pull request #1952 from esdrubal/proc_name
[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                         Thread.Sleep (2000);  // let WCF spin up
61
62                         try {
63                                 // client
64                                 var binding = new BasicHttpBinding ();
65                                 var remoteAddress = new EndpointAddress ("http://localhost:" + port + "/Service1");
66                                 var client = new Service1Client (binding, remoteAddress);
67
68                                 var wait = new ManualResetEvent (false);
69                                 client.GetDataCompleted += delegate (object o, GetDataCompletedEventArgs e) {
70                                         if (e.Error != null)
71                                                 throw e.Error;
72                                         Assert.AreEqual ("A", ((DataType1) e.Result).Id, "#1");
73                                         wait.Set ();
74                                 };
75
76                                 client.GetDataAsync ();
77                                 if (!wait.WaitOne (TimeSpan.FromSeconds (20)))
78                                         Assert.Fail ("timeout");
79                         } finally {
80                                 serviceHost.Close ();
81                         }
82                 }
83
84                 public class Service1 : IService1
85                 {
86                         public object GetData ()
87                         {
88                                 return new DataType1 { Id = "A" };
89                         }
90
91                         Func<object> d;
92                         public IAsyncResult BeginGetData (AsyncCallback callback, object state)
93                         {
94                                 if (d == null)
95                                 d = new Func<object> (GetData);
96                                 return d.BeginInvoke (callback, state);
97                         }
98
99                         public object EndGetData (IAsyncResult result)
100                         {
101                                 return d.EndInvoke (result);
102                         }
103                 }
104         }
105 }
106
107 // below are part of autogenerated code in comment #1 on bug #652331.
108
109 namespace WebServiceMoonlightTest.ServiceReference1 {
110
111     [System.Diagnostics.DebuggerStepThroughAttribute()]
112     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
113     [System.Runtime.Serialization.DataContractAttribute(Name="DataType1", Namespace="http://mynamespace")]
114     public partial class DataType1 : object, System.ComponentModel.INotifyPropertyChanged {
115         
116         private string IdField;
117         
118         [System.Runtime.Serialization.DataMemberAttribute()]
119         public string Id {
120             get {
121                 return this.IdField;
122             }
123             set {
124                 if ((object.ReferenceEquals(this.IdField, value) != true)) {
125                     this.IdField = value;
126                     this.RaisePropertyChanged("Id");
127                 }
128             }
129         }
130         
131         public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
132         
133         protected void RaisePropertyChanged(string propertyName) {
134             System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
135             if ((propertyChanged != null)) {
136                 propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
137             }
138         }
139     }
140     
141     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
142     [System.ServiceModel.ServiceContractAttribute(Namespace="http://mynamespace", ConfigurationName="ServiceReference1.IService1")]
143 [ServiceKnownType (typeof (DataType1))]
144     public interface IService1 {
145         
146         [System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://mynamespace/IService1/GetData", ReplyAction="http://mynamespace/IService1/GetDataResponse")]
147         [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WebServiceMoonlightTest.ServiceReference1.DataType1))]
148         System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState);
149         
150         object EndGetData(System.IAsyncResult result);
151     }
152     
153     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
154     public interface IService1Channel : WebServiceMoonlightTest.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
155     }
156     
157     [System.Diagnostics.DebuggerStepThroughAttribute()]
158     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
159     public partial class GetDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
160         
161         private object[] results;
162         
163         public GetDataCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
164                 base(exception, cancelled, userState) {
165             this.results = results;
166         }
167         
168         public object Result {
169             get {
170                 base.RaiseExceptionIfNecessary();
171                 return ((object)(this.results[0]));
172             }
173         }
174     }
175     
176     [System.Diagnostics.DebuggerStepThroughAttribute()]
177     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
178     public partial class Service1Client : System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
179         
180         private BeginOperationDelegate onBeginGetDataDelegate;
181         
182         private EndOperationDelegate onEndGetDataDelegate;
183         
184         private System.Threading.SendOrPostCallback onGetDataCompletedDelegate;
185         
186         private BeginOperationDelegate onBeginOpenDelegate;
187         
188         private EndOperationDelegate onEndOpenDelegate;
189         
190         private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
191         
192         private BeginOperationDelegate onBeginCloseDelegate;
193         
194         private EndOperationDelegate onEndCloseDelegate;
195         
196         private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
197         
198         public Service1Client() {
199         }
200         
201         public Service1Client(string endpointConfigurationName) : 
202                 base(endpointConfigurationName) {
203         }
204         
205         public Service1Client(string endpointConfigurationName, string remoteAddress) : 
206                 base(endpointConfigurationName, remoteAddress) {
207         }
208         
209         public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
210                 base(endpointConfigurationName, remoteAddress) {
211         }
212         
213         public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
214                 base(binding, remoteAddress) {
215         }
216         
217 /*
218         public System.Net.CookieContainer CookieContainer {
219             get {
220                 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
221                 if ((httpCookieContainerManager != null)) {
222                     return httpCookieContainerManager.CookieContainer;
223                 }
224                 else {
225                     return null;
226                 }
227             }
228             set {
229                 System.ServiceModel.Channels.IHttpCookieContainerManager httpCookieContainerManager = this.InnerChannel.GetProperty<System.ServiceModel.Channels.IHttpCookieContainerManager>();
230                 if ((httpCookieContainerManager != null)) {
231                     httpCookieContainerManager.CookieContainer = value;
232                 }
233                 else {
234                     throw new System.InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
235                             "ookieContainerBindingElement.");
236                 }
237             }
238         }
239 */
240         
241         public event System.EventHandler<GetDataCompletedEventArgs> GetDataCompleted;
242         
243         public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
244         
245         public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
246         
247         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
248         System.IAsyncResult WebServiceMoonlightTest.ServiceReference1.IService1.BeginGetData(System.AsyncCallback callback, object asyncState) {
249             return base.Channel.BeginGetData(callback, asyncState);
250         }
251         
252         [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
253         object WebServiceMoonlightTest.ServiceReference1.IService1.EndGetData(System.IAsyncResult result) {
254             return base.Channel.EndGetData(result);
255         }
256         
257         private System.IAsyncResult OnBeginGetData(object[] inValues, System.AsyncCallback callback, object asyncState) {
258             return ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).BeginGetData(callback, asyncState);
259         }
260         
261         private object[] OnEndGetData(System.IAsyncResult result) {
262             object retVal = ((WebServiceMoonlightTest.ServiceReference1.IService1)(this)).EndGetData(result);
263             return new object[] {
264                     retVal};
265         }
266         
267         private void OnGetDataCompleted(object state) {
268             if ((this.GetDataCompleted != null)) {
269                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
270                 this.GetDataCompleted(this, new GetDataCompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
271             }
272         }
273         
274         public void GetDataAsync() {
275             this.GetDataAsync(null);
276         }
277         
278         public void GetDataAsync(object userState) {
279             if ((this.onBeginGetDataDelegate == null)) {
280                 this.onBeginGetDataDelegate = new BeginOperationDelegate(this.OnBeginGetData);
281             }
282             if ((this.onEndGetDataDelegate == null)) {
283                 this.onEndGetDataDelegate = new EndOperationDelegate(this.OnEndGetData);
284             }
285             if ((this.onGetDataCompletedDelegate == null)) {
286                 this.onGetDataCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnGetDataCompleted);
287             }
288             base.InvokeAsync(this.onBeginGetDataDelegate, null, this.onEndGetDataDelegate, this.onGetDataCompletedDelegate, userState);
289         }
290         
291         private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) {
292             return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
293         }
294         
295         private object[] OnEndOpen(System.IAsyncResult result) {
296             ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);
297             return null;
298         }
299         
300         private void OnOpenCompleted(object state) {
301             if ((this.OpenCompleted != null)) {
302                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
303                 this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
304             }
305         }
306         
307         public void OpenAsync() {
308             this.OpenAsync(null);
309         }
310         
311         public void OpenAsync(object userState) {
312             if ((this.onBeginOpenDelegate == null)) {
313                 this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
314             }
315             if ((this.onEndOpenDelegate == null)) {
316                 this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
317             }
318             if ((this.onOpenCompletedDelegate == null)) {
319                 this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
320             }
321             base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
322         }
323         
324         private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) {
325             return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
326         }
327         
328         private object[] OnEndClose(System.IAsyncResult result) {
329             ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);
330             return null;
331         }
332         
333         private void OnCloseCompleted(object state) {
334             if ((this.CloseCompleted != null)) {
335                 InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
336                 this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
337             }
338         }
339         
340         public void CloseAsync() {
341             this.CloseAsync(null);
342         }
343         
344         public void CloseAsync(object userState) {
345             if ((this.onBeginCloseDelegate == null)) {
346                 this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
347             }
348             if ((this.onEndCloseDelegate == null)) {
349                 this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
350             }
351             if ((this.onCloseCompletedDelegate == null)) {
352                 this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
353             }
354             base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
355         }
356         
357 /*
358         protected override WebServiceMoonlightTest.ServiceReference1.IService1 CreateChannel() {
359             return new Service1ClientChannel(this);
360         }
361         
362         private class Service1ClientChannel : ChannelBase<WebServiceMoonlightTest.ServiceReference1.IService1>, WebServiceMoonlightTest.ServiceReference1.IService1 {
363             
364             public Service1ClientChannel(System.ServiceModel.ClientBase<WebServiceMoonlightTest.ServiceReference1.IService1> client) : 
365                     base(client) {
366             }
367             
368             public System.IAsyncResult BeginGetData(System.AsyncCallback callback, object asyncState) {
369                 object[] _args = new object[0];
370                 System.IAsyncResult _result = base.BeginInvoke("GetData", _args, callback, asyncState);
371                 return _result;
372             }
373             
374             public object EndGetData(System.IAsyncResult result) {
375                 object[] _args = new object[0];
376                 object _result = ((object)(base.EndInvoke("GetData", _args, result)));
377                 return _result;
378             }
379         }
380 */
381     }
382 }