本文整理汇总了C#中ServiceCollection.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.GetService方法的具体用法?C# ServiceCollection.GetService怎么用?C# ServiceCollection.GetService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.GetService方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Should_Be_Able_To_Nack_Message
public async void Should_Be_Able_To_Nack_Message()
{
/* Setup */
var service = new ServiceCollection()
.AddRawRabbit<AdvancedMessageContext>()
.BuildServiceProvider();
var firstResponder = service.GetService<IBusClient<AdvancedMessageContext>>();
var secondResponder = service.GetService<IBusClient<AdvancedMessageContext>>();
var requester = service.GetService<IBusClient<AdvancedMessageContext>>();
var hasBeenNacked = false;
firstResponder.RespondAsync<BasicRequest, BasicResponse>((request, context) =>
{
BasicResponse response = null;
if (!hasBeenNacked)
{
context?.Nack();
hasBeenNacked = true;
}
else
{
response = new BasicResponse();
}
return Task.FromResult(response);
}, c => c.WithNoAck(false));
secondResponder.RespondAsync<BasicRequest, BasicResponse>((request, context) =>
{
BasicResponse response = null;
if (!hasBeenNacked)
{
context?.Nack();
hasBeenNacked = true;
}
else
{
response = new BasicResponse();
}
return Task.FromResult(response);
}, c => c.WithNoAck(false));
/* Test */
var result = await requester.RequestAsync<BasicRequest, BasicResponse>(new BasicRequest(), configuration: cfg => cfg
.WithReplyQueue(
q => q.WithName("special_reply_queue")));
/* Assert */
Assert.NotNull(result);
Assert.True(hasBeenNacked);
}
示例2: GetAdvancedBusClient
public static IBusClient<AdvancedMessageContext> GetAdvancedBusClient()
{
/*
There is no "simple" and "advanced" bus client in raw rabbit.
The normal busclient is in fact just a IBusClient<MessageContext>
which is kind of the same as the one we're returning here,
with the difference that you get an "advanced context" where
you can do some cool stuff.
*/
var serviceProvider = new ServiceCollection()
.AddRawRabbit<AdvancedMessageContext>()
.AddSingleton<ILoggerFactory, VoidLoggerFactory>()
.BuildServiceProvider();
LogManager.CurrentFactory = serviceProvider.GetService<ILoggerFactory>();
return serviceProvider.GetService<IBusClient<AdvancedMessageContext>>();
}
示例3: Program
public Program()
{
_consoleWriter = new ConsoleWriter();
IServiceProvider serviceProvider = new ServiceCollection()
.AddProceesProviderServices()
.BuildServiceProvider();
_executorProvider=serviceProvider.GetService<IProcessExecutorProvider>();
}
示例4: TaskRunnerTests
public TaskRunnerTests()
{
var lf = new LoggerFactory();
lf.AddConsole();
var serviceProvider = new ServiceCollection()
.AddTransient(_ => new SampleTask(settings))
.BuildServiceProvider();
sampleTask = new TaskRunner<SampleTask>(lf, serviceProvider.GetService<IServiceScopeFactory>());
sampleTask.Interval = TimeSpan.FromSeconds(5);
}
示例5: Should_Be_Able_To_Nack_On_Subscribe
public async void Should_Be_Able_To_Nack_On_Subscribe()
{
/* Setup */
var service = new ServiceCollection()
.AddRawRabbit<AdvancedMessageContext>()
.BuildServiceProvider();
var subscriber = service.GetService<IBusClient<AdvancedMessageContext>>();
var secondSubscriber = service.GetService<IBusClient<AdvancedMessageContext>>();
var publisher = service.GetService<IBusClient<AdvancedMessageContext>>();
var callcount = 0;
var subscribeTcs = new TaskCompletionSource<bool>();
var secondSubscribeTcs = new TaskCompletionSource<bool>();
subscriber.SubscribeAsync<BasicMessage>((message, context) =>
{
Interlocked.Increment(ref callcount);
context?.Nack();
subscribeTcs.TrySetResult(true);
return Task.FromResult(true);
});
secondSubscriber.SubscribeAsync<BasicMessage>((message, context) =>
{
secondSubscribeTcs.TrySetResult(true);
return Task.FromResult(true);
});
Task.WaitAll(
publisher.PublishAsync<BasicMessage>(),
subscribeTcs.Task,
secondSubscribeTcs.Task
);
TestChannel.QueueDelete("basicmessage");
Assert.Equal(callcount, 1);
}
示例6: Non_generic_GetService_returns_null_if_service_not_registered
public void Non_generic_GetService_returns_null_if_service_not_registered()
{
var serviceProvider = new ServiceCollection().BuildServiceProvider();
Assert.Null(serviceProvider.GetService(typeof(IPilkington)));
}
示例7: GetService_returns_null_if_service_not_registered
public void GetService_returns_null_if_service_not_registered()
{
var serviceProvider = new ServiceCollection().BuildServiceProvider();
Assert.Null(serviceProvider.GetService<IPilkington>());
}