3

我已阅读WCF Data Service 5.0 release notes,其中提到了OData 操作(不是服务方法)的实现。

在分析了一个教程后,我得出结论,我需要为自己构建实体框架的 WCF 数据服务操作提供程序

那个项目只有一次签到,从那以后就没有更新过。我认为它没有错误,但根据统计数据,我不希望有太多的客户群可以分享经验。

您知道在 .NET 中实现 OData 操作的任何其他选项吗?

4

2 回答 2

2

您是否考虑过使用 ASP.NET Web API OData?它支持 OData 操作,并且可以直接使用 Web API 实现它们。您可以在此博客文章中找到更多信息。

于 2013-01-16T19:58:11.827 回答
1

我认为你是对的。基本思想类似于:

/// <summary>
/// Invokation helper for Actions in the ReSTful world, this is provided since currently no methods exist to 
/// do this in the Microsoft Services framework that I could find.
/// </summary>
/// <typeparam name="InType">The type of the entity to invoke the method against</typeparam>
public class InvokationHelper<InType, OutType>
{

    /// <summary>
    /// Invokes the action on the entity passed in.
    /// </summary>
    /// <param name="container">The service container</param>
    /// <param name="entity">The entity to act upon</param>
    /// <param name="ActionName">The name of the action to invoke</param>
    /// <returns>OutType object from the action invokation</returns>
    public OutType InvokeAction(Container container, InType entity, string ActionName)
    {
        string UriBase = container.GetEntityDescriptor(entity).SelfLink.AbsoluteUri;
        string UriInvokeAction = UriBase + "/" + ActionName;

        Debug.WriteLine("InvokationHelper<{0}>.InvokeAction: {1}", typeof(InType), UriInvokeAction);

        try
        {
            IEnumerable<OutType> response;

            response = container.Execute<OutType>(
                new Uri(UriInvokeAction),
                "POST",
                true
                );
            return response.First();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    /// <summary>
    /// Invokes the action on the entity passed in.
    /// </summary>
    /// <param name="container">The service container</param>
    /// <param name="entity">The entity to act upon</param>
    /// <param name="ActionName">The name of the action to invoke</param>
    /// <returns>An enumeration of OutType object from the action invokation</returns>
    public IEnumerable<OutType> InvokeActionEnumerable(Container container, InType entity, string ActionName)
    {
        string UriBase = container.GetEntityDescriptor(entity).SelfLink.AbsoluteUri;
        string UriInvokeAction = UriBase + "/" + ActionName;

        Debug.WriteLine("InvokationHelper<{0}>.InvokeAction: {1}", typeof(InType), UriInvokeAction);

        try
        {
            IEnumerable<OutType> response;

            response = container.Execute<OutType>(
                new Uri(UriInvokeAction),
                "POST",
                true
                );
            return response;
        }
        catch (Exception e)
        {
            throw e;
        }
    }
}

}

我相信有很多更优雅的方法可以做到这一点。如果您为此编写了任何代码,我很乐意看到它。我在语言边缘的 C#(比如创建通用方法来调用直到运行时才定义的类型的方法等)并不是最强的。

调用类似于:

 InvokationHelper<MyObjectType, MyObjectType> helper = new InvokationHelper<MyObjectType, MyObjectType>();

 try
 {
    MyObjectType resultObject = helper.InvokeAction(container, myServiceObject, "MyActionName");
 }
 catch (Exception e)
 {
    // Handle failure of the invokation
 }

需要注意的是,为了获得扩展类型,您需要使用 [FromODataUri] 属性装饰您的 EntitySetControllers Action 方法。这将强制对传递的关键参数进行适当的 Edm 键入。如果没有这个,你会从 Edm 中得到 URI 行中修饰的类型的解析错误。例如,具有 .../EntitySet(12345L)/ActionName 之类的 URI 的 EntitySet 将在解析时引发错误。名义上,目的是将参数解码为 Edm.Int64 类型,但如果没有 [FromODataUri] 属性,则不会发生这种情况:

[HttpPost]
public ReturnEntityType ActionName([FromODataUri]long key)
{
    ...
}

这对我来说是一个非常令人沮丧的错误,我已经将它作为错误提交给 MS。原来它只需要在输入参数上进行类型修饰。

于 2013-03-07T09:18:57.793 回答