C# IContainerBuilder.AddService在添加EdmModel的IRouteBuilder OData.Net核心中不工作
我在OData.Net中使用了这部分代码。此代码在.Net Core中工作。这是我的配置类:C# IContainerBuilder.AddService在添加EdmModel的IRouteBuilder OData.Net核心中不工作,c#,asp.net-core,.net-core,odata,C#,Asp.net Core,.net Core,Odata,我在OData.Net中使用了这部分代码。此代码在.Net Core中工作。这是我的配置类: public static class Configuration { public static Action<IApplicationBuilder> ConvertToAppBuilder(Action<object> myActionT) { if (myActionT == null) return nu
public static class Configuration
{
public static Action<IApplicationBuilder> ConvertToAppBuilder(Action<object> myActionT)
{
if (myActionT == null) return null;
else return new Action<object>(o => myActionT(o));
}
public static Action<IApplicationBuilder> GetBuilder()
{
return ConvertToAppBuilder(Configure);
}
public static void Configure(object appBuilder)
{
var app = appBuilder as IApplicationBuilder;
var config = new RouteBuilder(app);
var builder = new ODataConventionModelBuilder(config.ApplicationBuilder.ApplicationServices) { Namespace = "Model.Entities", ContainerName = "DefaultContainer" };
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Filter();
routeBuilder.MapODataServiceRoute("odata", "odata", configureAction: c => c
.AddService(Microsoft.OData.ServiceLifetime.Transient, typeof(IEdmModel), sp => GetEdmModel()));
});
}
static IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<Student>("Student");
return odataBuilder.GetEdmModel();
}
}
但是我需要使用第二个构造器,因为我将在那里添加自定义的ODaUriResolver。那么我该如何解决这个问题呢?我也有同样的问题。Seams源代码在添加自定义约定时遇到问题。以下代码对我有效:
routeBuilder.MapODataRoute("OData", "odata", b =>
{
b.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => edmModel);
var customRoutingConvention = new ODataCustomRoutingConvention();
var conventions = ODataRoutingConventions.CreateDefault();
//Workaround for https://github.com/OData/WebApi/issues/1622
conventions.Insert(0, new AttributeRoutingConvention("OData", app.ApplicationServices, new DefaultODataPathHandler()));
//Custom Convention
conventions.Insert(0, customRoutingConvention);
b.AddService<IEnumerable<IODataRoutingConvention>>(Microsoft.OData.ServiceLifetime.Singleton, a => conventions);
});
routeBuilder.MapODataRoute(“OData”,“OData”,b=>
{
b、 AddService(Microsoft.OData.ServiceLifetime.Singleton,sp=>edmModel);
var customRoutingConvention=新ODataCustomRoutingConvention();
var conventions=ODataRoutingConventions.CreateDefault();
//解决方法https://github.com/OData/WebApi/issues/1622
插入(0,新的AttributeRoutingConvention(“OData”,app.ApplicationServices,新的DefaultODataPathHandler());
//习俗
约定。插入(0,customRoutingConvention);
b、 AddService(Microsoft.OData.ServiceLifetime.Singleton,a=>conventions);
});
routeBuilder.MapODataRoute("OData", "odata", b =>
{
b.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => edmModel);
var customRoutingConvention = new ODataCustomRoutingConvention();
var conventions = ODataRoutingConventions.CreateDefault();
//Workaround for https://github.com/OData/WebApi/issues/1622
conventions.Insert(0, new AttributeRoutingConvention("OData", app.ApplicationServices, new DefaultODataPathHandler()));
//Custom Convention
conventions.Insert(0, customRoutingConvention);
b.AddService<IEnumerable<IODataRoutingConvention>>(Microsoft.OData.ServiceLifetime.Singleton, a => conventions);
});