[System.ServiceModel] Prevent crash in Dispatcher.ListenerLoopManager… (#7136)
[mono-project.git] / mono / tests / bug-415577.cs
blob6e8e92d76ddd079ca224dc97e6e774ee43aacaa7
1 using System;
2 using System.Threading;
3 using System.Runtime.Remoting;
4 using System.Runtime.Remoting.Activation;
5 using System.Runtime.Remoting.Contexts;
6 using System.Runtime.Remoting.Messaging;
8 public class MyContextAttribute: Attribute, IContextAttribute {
9 public void GetPropertiesForNewContext (IConstructionCallMessage msg)
13 public bool IsContextOK (Context ctx, IConstructionCallMessage msg)
15 return false;
19 // CBO class whose objects are always in the correct context
20 class UnlockedCbo : ContextBoundObject {
21 public int Counter;
24 void Inc (ref int a)
26 a++;
29 public void Inc ()
31 Inc (ref Counter);
35 // CBO class whose objects are always out of context
36 [MyContext]
37 class LockedCbo : UnlockedCbo {
40 class Mbr : MarshalByRefObject {
41 public int Counter;
43 void Inc (ref int a)
45 a++;
48 public void Inc ()
50 Inc (ref Counter);
54 class Test {
55 static int Main ()
57 // warning CS0197 is expected several times
59 UnlockedCbo uc = new UnlockedCbo ();
60 Interlocked.Increment (ref uc.Counter);
61 uc.Inc ();
63 LockedCbo lc = new LockedCbo ();
64 try {
65 Interlocked.Increment (ref lc.Counter);
66 return 1;
67 } catch (InvalidOperationException) {
70 lc.Inc ();
72 if (lc.Counter != 1)
73 return 2;
75 Mbr m = new Mbr ();
76 Interlocked.Increment (ref m.Counter);
77 m.Inc ();
79 if (m.Counter != 2)
80 return 3;
82 Mbr rm = (Mbr) CreateRemote (typeof (Mbr));
83 try {
84 Interlocked.Increment (ref rm.Counter);
85 return 4;
86 } catch (InvalidOperationException) {
89 rm.Inc ();
91 if (rm.Counter != 1)
92 return 5;
94 return 0;
97 static object CreateRemote (Type t)
99 AppDomain d = AppDomain.CreateDomain ("foo");
100 return d.CreateInstanceAndUnwrap (t.Assembly.FullName, t.FullName);