在 asp.net web api 中支持 @odata.bind(在 post 期间对现有实体 link)

support of @odata.bind in asp.net web api (link to existing entity during post)

我很难从 WCF 数据服务迁移到 Web api odata v4。我遇到了以下问题:

odata web api 好像不支持@odata.bind.

我发现了以下 link: https://github.com/OData/WebApi/issues/158 有人建议像这样实现自己的 ODataEntityDeserializer 派生 class:

public class ExtendedODataEntityDeserializer : ODataEntityDeserializer
{
    public ExtendedODataEntityDeserializer(ODataDeserializerProvider deserializerProvider) : base(deserializerProvider)
    {
    }

    public override void ApplyNavigationProperty(
        object entityResource, 
        ODataNavigationLinkWithItems navigationLinkWrapper, 
        IEdmEntityTypeReference entityType, 
        ODataDeserializerContext readContext)
    {
        base.ApplyNavigationProperty(entityResource, navigationLinkWrapper, entityType, readContext);

        foreach (var childItem in navigationLinkWrapper.NestedItems)
        {
            var entityReferenceLink = childItem as ODataEntityReferenceLinkBase;

            if (entityReferenceLink != null)
            {
                var navigationPropertyName = navigationLinkWrapper.NavigationLink.Name;
                Uri referencedEntityUrl = entityReferenceLink.EntityReferenceLink.Url;

                if (!referencedEntityUrl.IsAbsoluteUri)
                {
                    referencedEntityUrl = new Uri(readContext.Request.RequestUri, referencedEntityUrl);
                }

                var linkedEntities = (Model.LinkedEntityCollection)entityResource;
                linkedEntities.Add(navigationPropertyName, referencedEntityUrl);
            }
        }
    }
}

LinkedEntityCollection 是用作字典的实体 class 的基础 class(MyEntity : LinkedEntityCollection 实现很简单)。

public class ExtendedODataDeserializerProvider : ODataDeserializerProvider
{
    private static ExtendedODataDeserializerProvider _instance = null;

    private ExtendedODataDeserializerProvider()
    {
        _instance = this;
    }

    public static ExtendedODataDeserializerProvider Instance
    {
        get { return _instance ?? new ExtendedODataDeserializerProvider(); }
    }

    public override ODataEdmTypeDeserializer GetEdmTypeDeserializer(Microsoft.OData.Edm.IEdmTypeReference edmType)
    {
        return DefaultODataDeserializerProvider.Instance.GetEdmTypeDeserializer(edmType);
    }

    public override ODataDeserializer GetODataDeserializer(Microsoft.OData.Edm.IEdmModel model, Type type, System.Net.Http.HttpRequestMessage request)
    {
        return new ExtendedODataEntityDeserializer(DefaultODataDeserializerProvider.Instance);
    }
}

正在 http 配置中注册:

public static void Register(HttpConfiguration config)
{
    ODataModelBuilder builder = new ODataConventionModelBuilder();

    // registering entities: builder.EntitySet<T>("EntityName") ...

    ODataBatchHandler batchHandler = new DefaultODataBatchHandler(new HttpServer(config));
    batchHandler.MessageQuotas.MaxOperationsPerChangeset = 10;
    batchHandler.MessageQuotas.MaxPartsPerBatch = 10;

    var odataFormatters = ODataMediaTypeFormatters.Create(DefaultODataSerializerProvider.Instance, ExtendedODataDeserializerProvider.Instance);

    config.Formatters.Clear();
    config.Formatters.AddRange(odataFormatters);

    config.MapODataServiceRoute(routeName: "Central", routePrefix: "Odata", model: builder.GetEdmModel(), batchHandler: batchHandler);
}

然而,如果你确实使用了这个 hack,你就失去了在实体之间创建 links 的能力,如本文所述:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4

“@odata.id”未被解析,假定它是函数 ODataEntityDeserializer.Read 中的一个实体。也许我在这里从根本上遗漏了一些东西,因为我对网络 api 还很陌生(config.Formatters.Clear() 似乎有点苛刻)。或者是否有任何其他解决方法来支持@odata.bind?

是否有可能在不久的将来实现此功能?

更新

我的 hack 开始工作了。问题是我应该从 DefaultODataDeserializerProvider 而不是 ODataDeserializerProvider 和 return 中导出 ExtendedODataDeserializerProvider 相应的反序列化器:

public override ODataDeserializer GetODataDeserializer(
     Microsoft.OData.Edm.IEdmModel model, 
     Type type,     
     System.Net.Http.HttpRequestMessage request)
{
    if (type == typeof(ODataActionParameters) || 
        type == typeof(ODataUntypedActionParameters) || 
        type == typeof(Uri))
    {
        return base.GetODataDeserializer(model, type, request);
    }

    return new ExtendedODataEntityDeserializer(DefaultODataDeserializerProvider.Instance);
 }

所以剩下的问题是:我们什么时候在 asp.net webapi odata 中获得 @odata.bind 支持?

正在尝试回答

So the question that remains: when do we get @odata.bind support in asp.net webapi odata?

vnext 仓库中似乎有与此相关的功能。但是,该问题被标记为影响较小。 https://github.com/OData/WebApi/milestones/vNext 因此无法知道何时实施此功能。