本文整理汇总了C#中Bus.SendRequest方法的典型用法代码示例。如果您正苦于以下问题:C# Bus.SendRequest方法的具体用法?C# Bus.SendRequest怎么用?C# Bus.SendRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bus
的用法示例。
在下文中一共展示了Bus.SendRequest方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendingRequestWithEndpointSynchronouslyShouldReturnResponse
public void SendingRequestWithEndpointSynchronouslyShouldReturnResponse()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
Action<FakeMessage2> action = null;
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Returns(task)
.Callback<Action<FakeMessage2>>(r => action = r);
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send("test", It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>())).Callback(() =>
{
action(new FakeMessage2(message.CorrelationId)
{
DisplayName = "Tim Watson",
Email = "[email protected]"
});
task.Start();
});
// Act
var bus = new Bus(mockConfiguration.Object);
FakeMessage2 response = bus.SendRequest<FakeMessage1, FakeMessage2>("test", message, null, 1000);
// Assert
Assert.Equal("Tim Watson", response.DisplayName);
Assert.Equal("[email protected]", response.Email);
Assert.Equal(message.CorrelationId, response.CorrelationId);
}
示例2: SendingRequestWithEndpointSynchronouslyShouldSendMessageToTheSpecifiedEndPoint
public void SendingRequestWithEndpointSynchronouslyShouldSendMessageToTheSpecifiedEndPoint()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Returns(task);
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send("test", typeof(FakeMessage1), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>())).Callback(task.Start);
// Act
var bus = new Bus(mockConfiguration.Object);
FakeMessage2 response = bus.SendRequest<FakeMessage1, FakeMessage2>("test", message, null, 1000);
// Assert
mockProducer.Verify(x => x.Send("test", typeof(FakeMessage1), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()), Times.Once);
}
示例3: SendingRequestWithEndpointAndCallbackShouldPassCallbackToHandler
public void SendingRequestWithEndpointAndCallbackShouldPassCallbackToHandler()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
bool actionCalled = false;
Action<FakeMessage2> action = message2 => { actionCalled = true; };
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Callback<Action<object>>(a => a(new FakeMessage2(Guid.NewGuid()))).Returns(task);
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send(It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()));
// Act
var bus = new Bus(mockConfiguration.Object);
bus.SendRequest("test", message, action, null);
// Assert
mockRequestConfiguration.Verify(x => x.SetHandler(It.IsAny<Action<object>>()), Times.Once());
Assert.True(actionCalled);
}
示例4: SendingRequestWithCallbackShouldSendCommand
public void SendingRequestWithCallbackShouldSendCommand()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Returns(task);
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send(typeof(FakeMessage1), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()));
// Act
var bus = new Bus(mockConfiguration.Object);
bus.SendRequest<FakeMessage1, FakeMessage2>(message, x => { }, null);
// Assert
mockProducer.Verify(x => x.Send(typeof(FakeMessage1), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()), Times.Once);
}
示例5: SendingRequestToMultipleEndpointsSynchronouslyShouldReturnResponses
public void SendingRequestToMultipleEndpointsSynchronouslyShouldReturnResponses()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
Action<FakeMessage2> action = null;
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Returns(task).Callback<Action<FakeMessage2>>(r =>
{
action = r;
});
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
var r1 = new FakeMessage2(Guid.NewGuid());
var r2 = new FakeMessage2(Guid.NewGuid());
mockProducer.Setup(x => x.Send("test1", It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>())).Callback(() =>
{
action(r1);
});
mockProducer.Setup(x => x.Send("test2", It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>())).Callback(() =>
{
action(r2);
task.Start();
});
// Act
var bus = new Bus(mockConfiguration.Object);
IList<FakeMessage2> responses = bus.SendRequest<FakeMessage1, FakeMessage2>(new List<string>{ "test1", "test2" }, message, null, 1000);
// Assert
Assert.Equal(2, responses.Count);
Assert.True(responses.Contains(r1));
Assert.True(responses.Contains(r2));
}
示例6: SendingRequestToMultipleEndpointsShouldPassResponsesToCallbackHandler
public void SendingRequestToMultipleEndpointsShouldPassResponsesToCallbackHandler()
{
// Arrange
var mockConfiguration = new Mock<IConfiguration>();
var mockProducer = new Mock<IProducer>();
var mockContainer = new Mock<IBusContainer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny<Guid>())).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
int count = 0;
var r1 = new FakeMessage2(Guid.NewGuid());
var r2 = new FakeMessage2(Guid.NewGuid());
var responses = new List<FakeMessage2>();
Action<FakeMessage2> action = message2 =>
{
count++;
responses.Add(message2);
};
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Callback<Action<object>>(a =>
{
a(r1);
a(r2);
}).Returns(task);
var message = new FakeMessage1(Guid.NewGuid())
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send(It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()));
// Act
var bus = new Bus(mockConfiguration.Object);
bus.SendRequest(message, action, null);
// Assert
mockRequestConfiguration.Verify(x => x.SetHandler(It.IsAny<Action<object>>()), Times.Exactly(1));
Assert.Equal(2, count);
Assert.True(responses.Contains(r1));
Assert.True(responses.Contains(r2));
}
示例7: ConsumeMessageEventShouldProcessResponseMessage
public void ConsumeMessageEventShouldProcessResponseMessage()
{
// Arrange
var mockProducer = new Mock<IProducer>();
var mockRequestConfiguration = new Mock<IRequestConfiguration>();
_mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
var bus = new Bus(_mockConfiguration.Object);
var handlerReferences = new List<HandlerReference>
{
new HandlerReference
{
HandlerType = typeof (FakeHandler1),
MessageType = typeof (FakeMessage1)
},
new HandlerReference
{
HandlerType = typeof (FakeHandler2),
MessageType = typeof (FakeMessage2)
}
};
var headers = new Dictionary<string, object>
{
{ "MessageType", Encoding.ASCII.GetBytes("Send") }
};
_mockContainer.Setup(x => x.GetHandlerTypes()).Returns(handlerReferences);
_mockConsumerPool.Setup(x => x.AddConsumer(It.IsAny<string>(), It.IsAny<IList<string>>(), It.Is<ConsumerEventHandler>(y => AssignEventHandler(y)), It.IsAny<IConfiguration>()));
var mockMessageHandlerProcessor = new Mock<IMessageHandlerProcessor>();
_mockContainer.Setup(x => x.GetInstance<IMessageHandlerProcessor>(It.Is<Dictionary<string, object>>(y => y["container"] == _mockContainer.Object))).Returns(mockMessageHandlerProcessor.Object);
mockMessageHandlerProcessor.Setup(x => x.ProcessMessage<FakeMessage2>(It.IsAny<string>(), It.Is<IConsumeContext>(y => y.Headers == headers)));
var mockProcessManagerProcessor = new Mock<IProcessManagerProcessor>();
_mockContainer.Setup(x => x.GetInstance<IProcessManagerProcessor>(It.IsAny<Dictionary<string, object>>())).Returns(mockProcessManagerProcessor.Object);
mockProcessManagerProcessor.Setup(x => x.ProcessMessage<FakeMessage2>(It.IsAny<string>(), It.Is<IConsumeContext>(y => y.Headers == headers)));
_mockConfiguration.Setup(x => x.GetRequestConfiguration(It.Is<Guid>(y => SetCorrelationId(y)))).Returns(mockRequestConfiguration.Object);
var task = new Task(() => { });
mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny<Action<object>>())).Returns(task);
var id = Guid.NewGuid();
var message = new FakeMessage1(id)
{
Username = "Tim Watson"
};
mockProducer.Setup(x => x.Send(It.IsAny<Type>(), It.IsAny<byte[]>(), It.IsAny<Dictionary<string, string>>()));
bus.SendRequest<FakeMessage1, FakeMessage2>(message, x => { }, null);
bus.StartConsuming();
headers["SourceAddress"] = Encoding.ASCII.GetBytes(_correlationId.ToString());
var message2 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new FakeMessage2(id)
{
}));
// Act
_fakeEventHandler(message2, typeof(FakeMessage2).AssemblyQualifiedName, headers);
// Assert
mockMessageHandlerProcessor.Verify(x => x.ProcessMessage<FakeMessage2>(It.Is<string>(y => ((FakeMessage2)JsonConvert.DeserializeObject(y, typeof(FakeMessage2))).CorrelationId == id), It.Is<IConsumeContext>(y => y.Headers == headers)), Times.Once);
}