public void SendWithFailureOnRetry() { var count = 0; var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var endpointId = new EndpointId("id"); var msg = new EndpointDisconnectMessage(endpointId); Func <IStoreV1CommunicationData, MessageReceptionConfirmation> handler = s => { count++; throw new Exception("Lets bail the first one"); }; var receiver = new MockMessageReceivingEndpoint(handler); var uri = new Uri("net.pipe://localhost/test/pipe"); var host = new ServiceHost(receiver, uri); var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); var address = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id); var remoteEndpoint = host.AddServiceEndpoint(typeof(IMessageReceivingEndpoint), binding, address); foreach (var operation in remoteEndpoint.Contract.Operations) { var behavior = operation.Behaviors.Find <DataContractSerializerOperationBehavior>(); if (behavior == null) { behavior = new DataContractSerializerOperationBehavior(operation); operation.Behaviors.Add(behavior); } behavior.DataContractResolver = new ProtocolDataContractResolver(); } host.Open(); try { var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var localAddress = string.Format("{0}/{1}", uri.OriginalString, address); var template = new NamedPipeProtocolChannelTemplate(configuration.Object, new ProtocolDataContractResolver()); var sender = new RestoringMessageSendingEndpoint( new Uri(localAddress), template, new ProtocolDataContractResolver(), new IConvertCommunicationMessages[] { new EndpointDisconnectConverter(), }, systemDiagnostics); // This message should fault the channel Assert.Throws <FailedToSendMessageException>(() => sender.Send(msg, 2)); Assert.AreEqual(2, count); } finally { host.Close(); } }
public void SendWithNoChannel() { var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var endpointId = new EndpointId("id"); var msg = new EndpointDisconnectMessage(endpointId); var autoReset = new AutoResetEvent(false); bool hasBeenInvoked = false; Func <IStoreV1CommunicationData, MessageReceptionConfirmation> handler = s => { hasBeenInvoked = true; autoReset.Set(); Assert.AreEqual(endpointId, s.Sender); return(new MessageReceptionConfirmation { WasDataReceived = true, }); }; var receiver = new MockMessageReceivingEndpoint(handler); var uri = new Uri("net.pipe://localhost/test/pipe"); var host = new ServiceHost(receiver, uri); host.Faulted += (s, e) => Assert.Fail(); var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { TransferMode = TransferMode.Buffered, }; var address = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id); var remoteEndpoint = host.AddServiceEndpoint(typeof(IMessageReceivingEndpoint), binding, address); foreach (var operation in remoteEndpoint.Contract.Operations) { var behavior = operation.Behaviors.Find <DataContractSerializerOperationBehavior>(); if (behavior == null) { behavior = new DataContractSerializerOperationBehavior(operation); operation.Behaviors.Add(behavior); } behavior.DataContractResolver = new ProtocolDataContractResolver(); } host.Open(); try { var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var localAddress = string.Format("{0}/{1}", uri.OriginalString, address); var template = new NamedPipeProtocolChannelTemplate(configuration.Object, new ProtocolDataContractResolver()); var sender = new RestoringMessageSendingEndpoint( new Uri(localAddress), template, new ProtocolDataContractResolver(), new IConvertCommunicationMessages[] { new EndpointDisconnectConverter(), }, systemDiagnostics); sender.Send(msg, 1); autoReset.WaitOne(500); Assert.IsTrue(hasBeenInvoked); } finally { host.Close(); } }
public void SendWithNoChannel() { var text = "Hello world."; var data = new MemoryStream(); var writer = new StreamWriter(data); writer.Write(text); writer.Flush(); data.Position = 0; var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var sendingEndpoint = new EndpointId("a"); var msg = new DataTransferMessage { SendingEndpoint = sendingEndpoint, Data = data, }; bool hasBeenInvoked = false; Func <StreamData, StreamReceptionConfirmation> handler = s => { hasBeenInvoked = true; Assert.AreEqual(sendingEndpoint, s.SendingEndpoint); var storedText = new StreamReader(s.Data).ReadToEnd(); Assert.AreEqual(text, storedText); return(new StreamReceptionConfirmation { WasDataReceived = true, }); }; var receiver = new MockDataReceivingEndpoint(handler); var uri = new Uri("net.pipe://localhost/test/pipe"); var host = new ServiceHost(receiver, uri); var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { MaxReceivedMessageSize = CommunicationConstants.DefaultBindingMaxReceivedSizeForMessagesInBytes, TransferMode = TransferMode.Streamed, }; var address = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id); host.AddServiceEndpoint(typeof(IDataReceivingEndpoint), binding, address); host.Faulted += (s, e) => Assert.Fail(); host.Open(); try { var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var localAddress = string.Format("{0}/{1}", uri.OriginalString, address); var template = new NamedPipeProtocolChannelTemplate(configuration.Object, new ProtocolDataContractResolver()); var sender = new RestoringDataTransferingEndpoint( new Uri(localAddress), template, systemDiagnostics); sender.Send(msg, 1); Assert.IsTrue(hasBeenInvoked); } finally { host.Close(); } }