本文整理汇总了C#中ServiceCollection.BuildApiConfiguration方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.BuildApiConfiguration方法的具体用法?C# ServiceCollection.BuildApiConfiguration怎么用?C# ServiceCollection.BuildApiConfiguration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.BuildApiConfiguration方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigurationRegistersHookPointsCorrectly
public void ConfigurationRegistersHookPointsCorrectly()
{
IServiceCollection services = new ServiceCollection();
var configuration = services.BuildApiConfiguration();
Assert.Null(configuration.GetApiService<IHookA>());
Assert.Null(configuration.GetApiService<IHookB>());
var singletonHookPoint = new HookA();
services.CutoffPrevious<IHookA>(singletonHookPoint);
configuration = services.BuildApiConfiguration();
Assert.Same(singletonHookPoint, configuration.GetApiService<IHookA>());
Assert.Null(configuration.GetApiService<IHookB>());
var multiCastHookPoint1 = new HookB();
services.CutoffPrevious<IHookB>(multiCastHookPoint1);
configuration = services.BuildApiConfiguration();
Assert.Same(singletonHookPoint, configuration.GetApiService<IHookA>());
Assert.Equal(multiCastHookPoint1, configuration.GetApiService<IHookB>());
services = new ServiceCollection()
.CutoffPrevious<IHookB>(multiCastHookPoint1)
.ChainPrevious<IHookB, HookB>()
.AddInstance(new HookB());
configuration = services.BuildApiConfiguration();
var multiCastHookPoint2 = configuration.GetApiService<HookB>();
var handler = configuration.GetApiService<IHookB>();
Assert.Equal(multiCastHookPoint2, handler);
var delegateHandler = handler as HookB;
Assert.NotNull(delegateHandler);
Assert.Equal(multiCastHookPoint1, delegateHandler.InnerHandler);
}
示例2: ContributorsAreCalledCorrectly
public void ContributorsAreCalledCorrectly()
{
int i = 0;
var services = new ServiceCollection()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Next = next(),
Value = i++,
})
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Next = next(),
Value = i++,
})
.ChainPrevious<ISomeService>(next => new SomeService()
{
Next = next,
Value = i++,
})
.ChainPrevious<ISomeService>((sp, next) => new SomeService()
{
Next = next,
Value = i++,
})
.ChainPrevious<ISomeService, SomeService>();
var configuration = services.BuildApiConfiguration();
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("03210", value);
}
示例3: GetModelUsingDefaultModelHandler
public async Task GetModelUsingDefaultModelHandler()
{
var services = new ServiceCollection();
services.CutoffPrevious<IModelBuilder>(new TestModelProducer());
services.ChainPrevious<IModelBuilder>(next => new TestModelExtender(2)
{
InnerHandler = next,
});
services.ChainPrevious<IModelBuilder>(next => new TestModelExtender(3)
{
InnerHandler = next,
});
var configuration = services.BuildApiConfiguration();
var context = new ApiContext(configuration);
var model = await context.GetModelAsync();
Assert.Equal(4, model.SchemaElements.Count());
Assert.NotNull(model.SchemaElements
.SingleOrDefault(e => e.Name == "TestName"));
Assert.NotNull(model.SchemaElements
.SingleOrDefault(e => e.Name == "TestName2"));
Assert.NotNull(model.SchemaElements
.SingleOrDefault(e => e.Name == "TestName3"));
Assert.NotNull(model.EntityContainer);
Assert.NotNull(model.EntityContainer.Elements
.SingleOrDefault(e => e.Name == "TestEntitySet"));
Assert.NotNull(model.EntityContainer.Elements
.SingleOrDefault(e => e.Name == "TestEntitySet2"));
Assert.NotNull(model.EntityContainer.Elements
.SingleOrDefault(e => e.Name == "TestEntitySet3"));
}
示例4: GetModelAsyncRetriableAfterFailure
public async Task GetModelAsyncRetriableAfterFailure()
{
var services = new ServiceCollection();
var service = new TestRetryModelBuilder();
services.CutoffPrevious<IModelBuilder>(service);
var configuration = services.BuildApiConfiguration();
using (var wait = new ManualResetEventSlim(false))
{
var tasks = PrepareThreads(6, configuration, wait);
wait.Set();
await Task.WhenAll(tasks).ContinueWith(t =>
{
Assert.True(t.IsFaulted);
Assert.True(tasks.All(e => e.IsFaulted));
});
Assert.Equal(1, service.CalledCount);
tasks = PrepareThreads(150, configuration, wait);
var models = await Task.WhenAll(tasks);
Assert.Equal(2, service.CalledCount);
Assert.True(models.All(e => object.ReferenceEquals(e, models[42])));
}
}
示例5: ModelBuilderShouldBeCalledOnlyOnceIfSucceeded
public async Task ModelBuilderShouldBeCalledOnlyOnceIfSucceeded()
{
var services = new ServiceCollection();
var service = new TestSingleCallModelBuilder();
services.CutoffPrevious<IModelBuilder>(service);
var configuration = services.BuildApiConfiguration();
using (var wait = new ManualResetEventSlim(false))
{
for (int i = 0; i < 2; i++)
{
var tasks = PrepareThreads(50, configuration, wait);
wait.Set();
var models = await Task.WhenAll(tasks);
Assert.Equal(1, service.CalledCount);
Assert.True(models.All(e => object.ReferenceEquals(e, models[42])));
}
}
}
示例6: NextInjectedViaProperty
public void NextInjectedViaProperty()
{
var services = new ServiceCollection()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Next = next(),
Value = 1,
})
.ChainPrevious<ISomeService, SomeService>()
.MakeTransient<ISomeService>();
var configuration = services.BuildApiConfiguration();
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("01", value);
// Test expression compilation.
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("01", value);
}
示例7: NextInjectedWithInheritedField
public void NextInjectedWithInheritedField()
{
var services = new ServiceCollection()
.MakeTransient<ISomeService>()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Value = 2,
})
.AddInstance(new SomeService()
{
Value = 0,
})
.ChainPrevious<ISomeService, SomeService4>();
var configuration = services.BuildApiConfiguration();
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("4200", value);
// Test expression compilation
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("4200", value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("4200", value);
}
示例8: ThrowOnNoServiceFound
public void ThrowOnNoServiceFound()
{
var services = new ServiceCollection()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Value = 1,
})
.MakeTransient<ISomeService>()
.AddContributor<string>((sp, next) =>
{
return "0";
})
.MakeTransient<string>()
.ChainPrevious<ISomeService, SomeService3>();
var configuration = services.BuildApiConfiguration();
Assert.Throws<InvalidOperationException>(() =>
{
configuration.GetApiService<ISomeService>();
});
}
示例9: MultiInjectionViaConstructor
public void MultiInjectionViaConstructor()
{
var services = new ServiceCollection()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Value = 1,
})
.MakeTransient<ISomeService>()
.AddContributor<string>((sp, next) =>
{
return "0";
})
.MakeTransient<string>()
.ChainPrevious<ISomeService, SomeService3>()
.AddInstance<SomeService>(new SomeService()
{
Value = 2,
});
var configuration = services.BuildApiConfiguration();
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("0122", value);
// Test expression compilation
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("0122", value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("0122", value);
}
示例10: DefaultValueInConstructorUsedIfNoService
public void DefaultValueInConstructorUsedIfNoService()
{
var services = new ServiceCollection()
.AddContributor<ISomeService>((sp, next) => new SomeService()
{
Value = 2,
})
.MakeTransient<ISomeService>()
.ChainPrevious<ISomeService, SomeService2>();
var configuration = services.BuildApiConfiguration();
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("42", value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("42", value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal("42", value);
}
示例11: ServiceInjectedViaProperty
public void ServiceInjectedViaProperty()
{
var first = new SomeService()
{
Value = 42,
};
var services = new ServiceCollection()
.MakeTransient<ISomeService>()
.AddContributor<ISomeService>((sp, next) => first)
.ChainPrevious<ISomeService, SomeService2>()
.AddInstance<string>("Text");
var configuration = services.BuildApiConfiguration();
var expected = "Text42";
var value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal(expected, value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal(expected, value);
value = configuration.GetApiService<ISomeService>().Call();
Assert.Equal(expected, value);
Assert.NotEqual(
configuration.GetApiService<ISomeService>(),
configuration.GetApiService<ISomeService>());
}
示例12: ContextApiScopeWorksCorrectly
public void ContextApiScopeWorksCorrectly()
{
var services = new ServiceCollection()
.MakeScoped<ISomeService>()
.ChainPrevious<ISomeService>(next => new SomeService());
var configuration = services.BuildApiConfiguration();
var service1 = configuration.GetApiService<ISomeService>();
var context = new ApiContext(configuration);
var service2 = context.GetApiService<ISomeService>();
Assert.NotEqual(service1, service2);
var context3 = new ApiContext(configuration);
var service3 = context3.GetApiService<ISomeService>();
Assert.NotEqual(service3, service2);
}