public
Description:
Homepage: http://www.iserviceoriented.com
Clone URL: git://github.com/jezell/iserviceoriented.git
100644 240 lines (194 sloc) 10.038 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using IServiceOriented.ServiceBus.Delivery;
using IServiceOriented.ServiceBus.Dispatchers;
using IServiceOriented.ServiceBus.Delivery.Formatters;
using System.ServiceModel.Description;
using System.ServiceModel;
using System.Transactions;
 
namespace IServiceOriented.ServiceBus.UnitTests
{
[TestFixture]
    public class TestRequestResponse
    {
        public TestRequestResponse()
        {
        }
 
        public void TEst()
        {
            ContractDescription description = ContractDescription.GetContract(typeof(ISendMessageContract));
            Console.WriteLine(description);
        }
 
 
[Test]
        public void MethodDispatcher_Publishes_Response_Messages()
        {
            System.Messaging.IMessageFormatter binaryFormatter = new System.Messaging.BinaryMessageFormatter();
            using (ServiceBusRuntime runtime = Create.BinaryMsmqRuntime())
            {
                CEcho echo = new CEcho();
 
                SubscriptionEndpoint replyEndpoint = new SubscriptionEndpoint(Guid.NewGuid(), "test", null, null, typeof(void), new MethodDispatcher(echo, false), new PredicateMessageFilter( m => m.Action == "Echo"));
                runtime.Subscribe(replyEndpoint);
                runtime.Start();
                try
                {
                    string message = "echo this";
 
                    MessageDelivery[] output = runtime.PublishTwoWay(new PublishRequest(typeof(void), "Echo", message), TimeSpan.FromSeconds(100));
 
                    Assert.IsNotNull(output);
                    Assert.AreEqual(1, output.Length);
                    Assert.AreEqual(message, (string)output[0].Message);
                }
                finally
                {
                    runtime.Stop();
                }
            }
        }
 
[Test]
        public void MethodDispatcher_Publishes_Fault_Messages()
        {
            System.Messaging.IMessageFormatter binaryFormatter = new System.Messaging.BinaryMessageFormatter();
            using (ServiceBusRuntime runtime = Create.BinaryMsmqRuntime())
            {
                CEcho echo = new CEcho();
 
                SubscriptionEndpoint replyEndpoint = new SubscriptionEndpoint(Guid.NewGuid(), "test", null, null, typeof(void), new MethodDispatcher(echo, false), new PredicateMessageFilter(m =>
                    {
                        bool result = m.Action == "ThrowInvalidOperationException";
                        return result;
                    }));
                runtime.Subscribe(replyEndpoint);
                runtime.Start();
                try
                {
                    string message = null;
 
                    MessageDelivery[] output = runtime.PublishTwoWay(new PublishRequest(typeof(void), "ThrowInvalidOperationException", message), TimeSpan.FromSeconds(100));
 
                    Assert.IsNotNull(output);
                    Assert.AreEqual(1, output.Length);
                    Assert.IsInstanceOfType(typeof(InvalidOperationException), output[0].Message);
                }
                finally
                {
                    runtime.Stop();
                }
            }
        }
 
[Test]
        public void WcfDispatcher_Publishes_Fault_Messages()
        {
            //Assert.Ignore("Bug in .NET framework prevents this from working properly");
 
            // Code in
            /*
private void ValidateScopeRequiredAndAutoComplete(OperationDescription operation, bool singleThreaded, string contractName)
{
OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
if (attribute != null)
{
if (!attribute.TransactionScopeRequired && !attribute.TransactionAutoComplete)
{
string name = "SFxTransactionAutoEnlistOrAutoComplete2";
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(name, new object[] { contractName, operation.Name })));
}
if (!singleThreaded && !attribute.TransactionAutoComplete)
{
string str2 = "SFxTransactionNonConcurrentOrAutoComplete2";
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(str2, new object[] { contractName, operation.Name })));
}
}
}
throws:
System.InvalidOperationException : The operation 'ThrowFaultException' on contract 'IEcho' is configured with TransactionAutoComplete set to true and with TransactionScopeRequired set to false. TransactionAutoComplete requires that TransactionScopeRequired is set to true.
at System.ServiceModel.Dispatcher.TransactionValidationBehavior.ValidateScopeRequiredAndAutoComplete(OperationDescription operation, Boolean singleThreaded, String contractName)
at System.ServiceModel.Dispatcher.TransactionValidationBehavior.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription service, ServiceHostBase serviceHostBase)
at System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnBeginOpen()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
Check that throws this exception appears to be coded incorrectly, since it checks !attribute.TransactionAutoComplete instead of attribute.TransactionAutoComplete before throwing the message, so we
can't tell WCF not to cancel the transaction.
*/
 
 
            using (ServiceBusRuntime runtime = Create.MsmqRuntime<IEcho>())
            {
                CEcho echo = new CEcho();
 
                ServiceHost echoHost = new ServiceHost(typeof(CEcho));
                 
                try
                {
                    echoHost.Open();
 
                    SubscriptionEndpoint replyEndpoint = new SubscriptionEndpoint(Guid.NewGuid(), "EchoClient", "EchoHostClient", "net.pipe://localhost/echo", typeof(IEcho), new WcfProxyDispatcher(), new PredicateMessageFilter( m => m.Action == "ThrowFaultException"));
                    runtime.Subscribe(replyEndpoint);
                    runtime.Start();
 
                    string message = "fault reason";
 
                    MessageDelivery[] output = runtime.PublishTwoWay(new PublishRequest(typeof(IEcho), "ThrowFaultException", message), TimeSpan.FromSeconds(10));
 
                    Assert.IsNotNull(output);
                    Assert.IsInstanceOfType(typeof(FaultException<SendFault>), output[0].Message);
                    Assert.AreEqual(message, ((FaultException<SendFault>)output[0].Message).Reason.ToString());
                    echoHost.Close();
                }
                finally
                {
                    echoHost.Abort();
                }
 
            }
        }
 
[Test]
        public void WcfDispatcher_Publishes_Response_Messages()
        {
 
            using (ServiceBusRuntime runtime = Create.MsmqRuntime<IEcho>())
            {
                CEcho echo = new CEcho();
 
                ServiceHost echoHost = new ServiceHost(typeof(CEcho));
 
                try
                {
                    echoHost.Open();
 
                    SubscriptionEndpoint replyEndpoint = new SubscriptionEndpoint(Guid.NewGuid(), "EchoClient", "EchoHostClient", "net.pipe://localhost/echo", typeof(IEcho), new WcfProxyDispatcher(), new PredicateMessageFilter( m => m.Action == "Echo"));
                    runtime.Subscribe(replyEndpoint);
                    runtime.Start();
 
                    string message = "this is a message";
 
                    MessageDelivery[] output = runtime.PublishTwoWay(new PublishRequest(typeof(IEcho), "Echo", message), TimeSpan.FromSeconds(10));
 
                    Assert.IsNotNull(output);
                    Assert.IsInstanceOfType(typeof(string), output[0].Message);
                    Assert.AreEqual(message, output[0].Message);
                    echoHost.Close();
                }
                finally
                {
                    echoHost.Abort();
                }
 
            }
        }
       
 
        
    }
[ServiceContract]
    public interface IEcho
    {
[OperationContract(Action="Echo")]
        string Echo(string echo);
 
[FaultContract(typeof(SendFault))]
[OperationContract(Action="ThrowFaultException")]
        string ThrowFaultException(string echo);
    }
 
[ServiceBehavior(ConfigurationName = "EchoHost")]
    public class CEcho : IEcho
    {
        public string Echo(string echo)
        {
            if (echo == null)
            {
                throw new InvalidOperationException();
            }
            return echo;
        }
 
        public string ThrowInvalidOperationException(string echo)
        {
            throw new InvalidOperationException();
        }
 
[TransactionFlow(TransactionFlowOption.NotAllowed)]
        public string ThrowFaultException(string echo)
        {
            throw new FaultException<SendFault>(new SendFault(), echo);
        }
 
    }
        
}