本文整理汇总了C#中BuiltinHandlerActivator.Handle方法的典型用法代码示例。如果您正苦于以下问题:C# BuiltinHandlerActivator.Handle方法的具体用法?C# BuiltinHandlerActivator.Handle怎么用?C# BuiltinHandlerActivator.Handle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BuiltinHandlerActivator
的用法示例。
在下文中一共展示了BuiltinHandlerActivator.Handle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoIt
public async Task DoIt()
{
var builtinHandlerActivator = new BuiltinHandlerActivator();
var allDone = false;
var gotMessage = new ManualResetEvent(false);
builtinHandlerActivator.Handle<string>(async _ =>
{
gotMessage.Set();
await Task.Delay(2000);
allDone = true;
});
var bus = Configure.With(builtinHandlerActivator)
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "shutdown with pending tasks"))
.Start();
using (bus)
{
await bus.SendLocal("hej");
gotMessage.WaitOrDie(TimeSpan.FromSeconds(2));
// make bus shut down here
}
Assert.That(allDone, Is.True, "The message was apparently not handled all the way to the end!!!");
}
示例2: DoesNotDispatchMessageWithNullBus
public void DoesNotDispatchMessageWithNullBus()
{
var busInstances = new ConcurrentQueue<IBus>();
using (var activator = new BuiltinHandlerActivator())
{
activator.Handle<string>(async (bus, message) =>
{
busInstances.Enqueue(bus);
});
Configure.With(activator)
.Logging(l => l.ColoredConsole(LogLevel.Warn))
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "race-condition"))
.Options(o =>
{
o.SetNumberOfWorkers(1);
o.SetMaxParallelism(1);
})
.Start();
Task.WaitAll(Enumerable.Range(0, 1000)
.Select(i => activator.Bus.SendLocal($"message-{i}"))
.ToArray());
}
Thread.Sleep(1000);
var numberOfNulls = busInstances.Count(i => i == null);
Assert.That(numberOfNulls, Is.EqualTo(0),
"Did not expect any messages to be dispatched with a NULL bus instance");
}
示例3: CanMoveMessageToErrorQueueEvenThoughExceptionIsNotSerializable
public async Task CanMoveMessageToErrorQueueEvenThoughExceptionIsNotSerializable()
{
using (var activator = new BuiltinHandlerActivator())
{
activator.Handle<string>(async str =>
{
throw new ThisOneCannotBeSerialized("BAM!!!!!!!!!!!11111111111111111");
});
var network = new InMemNetwork();
var bus = Configure.With(activator)
.Transport(t => t.UseInMemoryTransport(network, "unserializable exceptions"))
.Options(o => o.SimpleRetryStrategy(maxDeliveryAttempts: 1))
.Start();
const string knownString = "JUST SOME LISP!!!!11((((((((((((((((((()))))))))))))))))))))))";
await bus.SendLocal(knownString);
var failedMessage = await network.WaitForNextMessageFrom("error");
Assert.That(Encoding.UTF8.GetString(failedMessage.Body), Is.EqualTo(JsonConvert.SerializeObject(knownString)));
}
}
示例4: ItHasBeenFixed
public async Task ItHasBeenFixed()
{
var activator = new BuiltinHandlerActivator();
Using(activator);
var receivedMessageIds = new ConcurrentBag<string>();
activator.Handle<string>(async (_, context, message) =>
{
receivedMessageIds.Add(context.TransportMessage.Headers[Headers.MessageId]);
});
var bus = Configure.With(activator)
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "buggerino"))
.Start();
var customHeaders = new Dictionary<string, string>
{
{"custom-header", "woohoo"}
};
const string repeatedMessage = "hej med dig";
await bus.SendLocal(repeatedMessage, customHeaders);
await bus.SendLocal("hej igen med", customHeaders);
await bus.SendLocal(repeatedMessage, customHeaders);
await Task.Delay(TimeSpan.FromSeconds(1));
Assert.That(receivedMessageIds.Distinct().Count(), Is.EqualTo(3), "Expected three unique message IDs - got: {0}", string.Join(", ", receivedMessageIds));
}
示例5: Main
static void Main()
{
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<Job>(async job =>
{
Console.WriteLine("Processing job {0}", job.JobNumber);
await Task.Delay(TimeSpan.FromMilliseconds(300));
});
Configure.With(adapter)
.Logging(l => l.ColoredConsole(LogLevel.Warn))
.Transport(t => t.UseSqlServer("server=.; initial catalog=rebus; integrated security=true", "Messages", "consumer"))
.Options(o =>
{
o.SetNumberOfWorkers(1);
o.SetMaxParallelism(20);
})
.Start();
Console.WriteLine("Consumer listening - press ENTER to quit");
Console.ReadLine();
}
}
示例6: NizzleName
public async Task NizzleName()
{
var gotMessage = new ManualResetEvent(false);
using (var activator = new BuiltinHandlerActivator())
{
activator.Handle<string>(async s =>
{
Console.WriteLine("Got message: {0}", s);
gotMessage.Set();
});
Configure.With(activator)
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "bimse"))
.Options(o =>
{
o.Register<IWorkerFactory>(c =>
{
var transport = c.Get<ITransport>();
var pipeline = c.Get<IPipeline>();
var pipelineInvoker = c.Get<IPipelineInvoker>();
var rebusLoggerFactory = c.Get<IRebusLoggerFactory>();
return new AsyncTaskWorkerFactory(transport, pipeline, pipelineInvoker, rebusLoggerFactory);
});
})
.Start();
await activator.Bus.SendLocal("hej med dig min ven");
gotMessage.WaitOrDie(TimeSpan.FromSeconds(3));
}
}
示例7: CanReceiveBunchOfMessages
public async Task CanReceiveBunchOfMessages()
{
var events = new ConcurrentQueue<string>();
using (var activator = new BuiltinHandlerActivator())
{
activator.Handle<string>(async s => events.Enqueue(s));
Configure.With(activator)
.Logging(l => l.Console(minLevel:LogLevel.Info))
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "bimse"))
.Options(o =>
{
o.Register<IWorkerFactory>(c =>
{
var transport = c.Get<ITransport>();
var pipeline = c.Get<IPipeline>();
var pipelineInvoker = c.Get<IPipelineInvoker>();
var rebusLoggerFactory = c.Get<IRebusLoggerFactory>();
return new AsyncTaskWorkerFactory(transport, pipeline, pipelineInvoker, rebusLoggerFactory);
});
o.SetNumberOfWorkers(100);
})
.Start();
var bus = activator.Bus;
await Task.WhenAll(Enumerable.Range(0, 100)
.Select(i => bus.SendLocal(string.Format("msg-{0}", i))));
await Task.Delay(1000);
Assert.That(events.Count, Is.EqualTo(100));
}
}
示例8: Main
static void Main()
{
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<Reply>(async reply =>
{
Console.WriteLine("Got reply '{0}' (from OS process {1})", reply.KeyChar, reply.OsProcessId);
});
Configure.With(adapter)
.Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
.Transport(t => t.UseSqlServer("server=.; database=rebus; trusted_connection=true", "Messages", "producer.input"))
.Routing(r => r.TypeBased().MapAssemblyOf<Job>("consumer.input"))
.Start();
Console.WriteLine("Press Q to quit or any other key to produce a job");
while (true)
{
var keyChar = char.ToLower(Console.ReadKey(true).KeyChar);
switch (keyChar)
{
case 'q':
goto quit;
default:
adapter.Bus.Send(new Job(keyChar)).Wait();
break;
}
}
quit:
Console.WriteLine("Quitting...");
}
}
示例9: ItWorksWithComplexMessage
public void ItWorksWithComplexMessage(bool withCompressionEnabled)
{
var activator = new BuiltinHandlerActivator();
var gotIt = new ManualResetEvent(false);
activator.Handle<TextMessage>(async str =>
{
if (string.Equals(str.Text, LongText))
{
gotIt.Set();
}
else
{
throw new Exception(string.Format("Received text with {0} chars did not match expected text with {1} chars!",
str.Text.Length, LongText.Length));
}
});
Using(activator);
var bus = CreateBus(withCompressionEnabled, activator);
bus.SendLocal(new TextMessage {Text = LongText}).Wait();
gotIt.WaitOrDie(TimeSpan.FromSeconds(10));
}
示例10: StartBus
static IBus StartBus(InMemNetwork network, string queueName, ConcurrentQueue<string> recordedEvents)
{
var activator = new BuiltinHandlerActivator();
activator.Handle(async (string message) =>
{
recordedEvents.Enqueue($"GOT MESSAGE: {message}");
});
return Configure.With(activator)
.Transport(t => t.UseInMemoryTransport(network, queueName))
.Options(o =>
{
o.Decorate(c =>
{
var events = c.Get<BusLifetimeEvents>();
events.BusStarting += () => recordedEvents.Enqueue("Bus starting");
events.BusStarted += () => recordedEvents.Enqueue("Bus started");
events.BusDisposing += () => recordedEvents.Enqueue("Bus disposing");
events.BusDisposed += () => recordedEvents.Enqueue("Bus disposed");
return events;
});
})
.Start();
}
示例11: Main
static void Main()
{
using (var adapter = new BuiltinHandlerActivator())
{
adapter.Handle<Job>(async job =>
{
Console.WriteLine("Processing job {0}", job.JobNumber);
await Task.Delay(TimeSpan.FromMilliseconds(300));
});
Configure.With(adapter)
.Logging(l => l.ColoredConsole(LogLevel.Warn))
.Transport(t => t.UsePostgreSql("server=localhost;port=5433;database=rebus2_test; user id=test; password=test; maximum pool size=30", "messages", "consumer"))
.Options(o =>
{
o.SetNumberOfWorkers(10);
o.SetMaxParallelism(20);
})
.Start();
Console.WriteLine("Consumer listening - press ENTER to quit");
Console.ReadLine();
}
}
示例12: IncludesCorrelationIdInTheThreeLoggedLines
public void IncludesCorrelationIdInTheThreeLoggedLines()
{
// ${basedir}/logs/logfile.log
var logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "logfile.log");
if (File.Exists(logFilePath))
{
File.Delete(logFilePath);
}
var activator = new BuiltinHandlerActivator();
Configure.With(Using(activator))
.Logging(l => l.NLog())
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "test"))
.Start();
var counter = new SharedCounter(1);
Using(counter);
var logger = LogManager.GetLogger("test");
activator.Handle<string>(async str =>
{
logger.Info("1");
await Task.Delay(100);
logger.Info("2");
await Task.Delay(100);
logger.Info("3");
counter.Decrement();
});
var headers = new Dictionary<string,string>
{
{Headers.CorrelationId, "known-correlation-id" }
};
activator.Bus.SendLocal("hej med dig min ven!!!", headers).Wait();
counter.WaitForResetEvent();
WaitForFile(logFilePath);
var loggedLines = File.ReadAllLines(logFilePath);
AssertLineIsThere(loggedLines, "1|known-correlation-id");
AssertLineIsThere(loggedLines, "2|known-correlation-id");
AssertLineIsThere(loggedLines, "3|known-correlation-id");
}
示例13: GetBus
IBus GetBus(ConcurrentQueue<string> receivedMessages, int numberOfWorkers)
{
var activator = new BuiltinHandlerActivator();
activator.Handle<string>(async str => receivedMessages.Enqueue(str));
var bus = Configure.With(activator)
.Transport(t => t.UseMsmq(_inputQueueName))
.Options(o => o.SetNumberOfWorkers(numberOfWorkers))
.Start();
return bus;
}
示例14: ConfigureSubscriber
static void ConfigureSubscriber(BuiltinHandlerActivator activator, string inputQueueName)
{
activator.Handle<string>(async str =>
{
Console.WriteLine("{0} => '{1}'", str, inputQueueName);
});
Configure.With(activator)
.Logging(l => l.ColoredConsole(MinimumLogLevel))
.Transport(t => t.UseRabbitMq(ConnectionString, inputQueueName))
.Start();
}
示例15: CreateBus
IBus CreateBus(string inputQueueName, Func<string, Task> stringHandler)
{
var activator = new BuiltinHandlerActivator();
activator.Handle(stringHandler);
var bus = Configure.With(activator)
.Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))
.Transport(t => t.UseSqlServer(SqlTestHelper.ConnectionString, _messagesTableName, inputQueueName))
.Subscriptions(s => s.StoreInSqlServer(SqlTestHelper.ConnectionString, _subscriptionsTableName, isCentralized: true))
.Start();
return Using(bus);
}