gpt4 book ai didi

c# - 在 .Net Core 上模拟 ODataQueryOptions

转载 作者:太空宇宙 更新时间:2023-11-03 18:22:18 26 4
gpt4 key购买 nike

最近我们决定使用包 Microsoft.AspNetCore.OData为我们的服务解决方案。这些服务具有 ODataQueryOptions 参数,并使用它来过滤它们提供的数据。为了对此进行单元测试,我需要以某种方式模拟 ODataQueryOptions。它过去使用 System.Web.Http.OData.Query.ODataQueryOptions 更容易,因为您可以使用 HttpRequestMessage 作为参数创建它,但现在不行了。

我有这个代码

 public static ODataQueryOptions<T> Create<T>(string url = "", Action<ODataConventionModelBuilder> reconfigure = null)
where T : class
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddMvcCore().AddApplicationPart(typeof(ODataFactory).Assembly);

ODataConventionModelBuilder builder = new ODataConventionModelBuilder(serviceCollection.BuildServiceProvider());
builder.EntitySet<T>("Entity");

reconfigure?.Invoke(builder);

ODataQueryContext context = new ODataQueryContext(builder.GetEdmModel(), typeof(T), new Microsoft.AspNet.OData.Routing.ODataPath());

var httpContext = new DefaultHttpContext();

var httpRequest = new DefaultHttpRequest(httpContext);

// throws exception: Value cannot be null. Parameter name: provider
return new ODataQueryOptions<T>(context, httpRequest);
}

此代码抛出下一个异常:

System.ArgumentNullException: Value cannot be null. Parameter name: provider at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestScope(HttpRequest request, String routeName) at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestContainer(HttpRequest request, String routeName) at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.GetRequestContainer(HttpRequest request) at Microsoft.AspNet.OData.Query.ODataQueryOptions..ctor(ODataQueryContext context, HttpRequest request) at Microsoft.AspNet.OData.Query.ODataQueryOptions1..ctor(ODataQueryContext context, HttpRequest request)
at Services.Test.Internal.ODataFactory.Create[T](String url, Action
1 reconfigure) in C:\Users\wboun\source\repos\Services.Test\Internal\ODataFactory.cs:line 36

最佳答案

我想出了如何克服 Value cannot be null 的问题。参数名称:提供者 错误和找不到非 OData 路由的服务容器。

首先,当您创建 DefaultHttpContext 对象时,您必须将 RequestServices 设置为 collection.BuildServiceProvider()。

接下来,您必须启用依赖注入(inject)。我能够通过使用最小起订量做到这一点。

var routeBuilder = new RouteBuilder(Mock.Of<IApplicationBuilder>(x => x.ApplicationServices == provider));
routeBuilder.EnableDependencyInjection();

我的完整代码是:

var collection = new ServiceCollection();

collection.AddOData();
collection.AddODataQueryFilter();
collection.AddTransient<ODataUriResolver>();
collection.AddTransient<ODataQueryValidator>();
collection.AddTransient<TopQueryValidator>();
collection.AddTransient<FilterQueryValidator>();
collection.AddTransient<SkipQueryValidator>();
collection.AddTransient<OrderByQueryValidator>();

var provider = collection.BuildServiceProvider();

var routeBuilder = new RouteBuilder(Mock.Of<IApplicationBuilder>(x => x.ApplicationServices == provider));
routeBuilder.EnableDependencyInjection();

var modelBuilder = new ODataConventionModelBuilder(provider);
modelBuilder.EntitySet<MyType>("MyType");
var model = modelBuilder.GetEdmModel();

var uri = new Uri("http://localhost/api/mytype/12345?$select=Id");

var httpContext = new DefaultHttpContext{
RequestServices = provider
};
HttpRequest req = new DefaultHttpRequest(httpContext)
{
Method = "GET",
Host = new HostString(uri.Host, uri.Port),
Path = uri.LocalPath,
QueryString = new QueryString(uri.Query)
};

var context = new ODataQueryContext(model, typeof(MyType), new Microsoft.AspNet.OData.Routing.ODataPath());
var options = new ODataQueryOptions<MyType>(context, req);

关于c# - 在 .Net Core 上模拟 ODataQueryOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48499119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com