从 .NET 客户端使用 ADO.NET 数据服务的服务操作

发布于 2024-08-07 19:00:48 字数 1198 浏览 11 评论 0原文

我正在尝试构建一个包含大量实体和一些服务操作的 ADO.NET 数据服务。一方面,我创建了一个 ASP.NET Web 应用程序,其中包含 ADO.NET 实体数据模型和 ADO.NET 数据服务。另一方面,我创建了第二个 ASP.NET Web 应用程序,它具有对数据服务的服务引用。

实体运行得很好,我可以使用 LINQ 来检索我想要的数据:

TestEntities entities = new TestEntities(
            new Uri("http://localhost/service/service.svc"));

var query = from customer in entities.Customers
                    where customer.ID == 1234
                    select customer;

query.ToList();

这很有效。然而,我完全无法通过服务运营检索信息。 数据服务端代码:

public static void InitializeService(IDataServiceConfiguration config) {
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
}

[WebInvoke]
public IQueryable<Customer> GetSomeCustomers() {
    TestEntities entities = new TestEntities();
    return from customer in entities.Customers
        where customer.ID > 0 && customer.ID < 20
        select customer;
}

当我将服务引用添加到客户端项目时,Visual Studio 没有识别任何服务操作。我知道我可以通过构造的 URI 以及 DataServiceContext 对象或 TestEntities 对象(在本例中)的 BeginExecute 方法或类似的方法来访问它们,但这不是我想要的方式。

我想要的是使用LINQ来遍历服务操作的返回数据。 这可能吗?应该是吧?

I am trying to build an ADO.NET Data Service with lots of entities and a few service operations. On one side I created a ASP.NET Web Application, in which an ADO.NET Entity Data Model and an ADO.NET Data Service are located. On the other side I created a second ASP.NET Web Application that has a Service Reference to the Data Service.

Entities are coming through very well, I can use LINQ to retrieve the data I want:

TestEntities entities = new TestEntities(
            new Uri("http://localhost/service/service.svc"));

var query = from customer in entities.Customers
                    where customer.ID == 1234
                    select customer;

query.ToList();

This works. However, retrieving information through Service Operations completely eludes me.
Data Service-side code:

public static void InitializeService(IDataServiceConfiguration config) {
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
}

[WebInvoke]
public IQueryable<Customer> GetSomeCustomers() {
    TestEntities entities = new TestEntities();
    return from customer in entities.Customers
        where customer.ID > 0 && customer.ID < 20
        select customer;
}

When I added the Service reference to my client project, Visual Studio didn't pick up on any Service Operations. I know I can access them through constructed URIs and the BeginExecute method of either the DataServiceContext object or the TestEntities object (in this case), or something like that, but that is not how I want it.

What I want is to use LINQ to go through the returned data of the Service Operation.
Is this possible? It should be, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-08-14 19:00:49

简单的事情一旦你知道了。

只需了解几件事:

当前 DataServiceClientGenerator(使用 EntityClassGenerator)不会为服务操作创建方法。

服务操作不支持在上下文中使用 CreateQuery 方法,目前它们可以工作,因为客户端没有对此进行验证(您会注意到,如果使用 CreateQuery,“()”将添加到查询方法的末尾像这样“http://localhost/service.svc/method()?parameter=2 ”,您可以使用 CreateQuery,但不建议使用。

并非所有服务操作都返回值,但在本示例中,我将仅显示返回值的示例。

public partial class NorthwindEntities
{ 
    public IQueryable<Order> OrdersByRegion(int regionId)
    {
     return this.Execute<Orders>(new Uri(string.Format("{0}OrdersByCountry?regionId={1}", this.BaseUri, regionId), UriKind.RelativeOrAbsolute));
    }
}

如果您需要更多信息,请随时询问PS

.:在您的示例中,您不需要在服务操作(服务器端)上创建新的数据上下文,当调用服务时,DataService 已经实例化了一个引用,

您实际上可以覆盖在服务操作上创建的数据上下文。服务端是这样的:

protected override NorthwindEntities CreateDataSource()
{
     return new NorthwindEntities();
}

Simple stuff once you know.

Just a few things to know:

Currently DataServiceClientGenerator (which uses the EntityClassGenerator) doesnt create methods for the service operations.

Using CreateQuery method on the context is not supported for service operations, currently they work because there is no validation on the client side for that (you will notice that if you use CreateQuery the "()" is added to the end of the Query Method like this "http://localhost/service.svc/method()?parameter=2", you can use CreateQuery but it is not recommended.

Not all Service operations return values, but for this example i will only show an example for the ones that do.

public partial class NorthwindEntities
{ 
    public IQueryable<Order> OrdersByRegion(int regionId)
    {
     return this.Execute<Orders>(new Uri(string.Format("{0}OrdersByCountry?regionId={1}", this.BaseUri, regionId), UriKind.RelativeOrAbsolute));
    }
}

If you require more information please feel free to ask any questions.

PS.: On your example you dont need to create a new data context on your service operation (server side) the DataService has already a reference instantiated when the service is called.

You can actually override the create of the data context on the service side like this:

protected override NorthwindEntities CreateDataSource()
{
     return new NorthwindEntities();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文