使用intellisense调用wcf数据服务操作

时间:2014-04-24 19:14:14

标签: c# metadata wcf-data-services

我有以下WCF数据服务

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyWcfDataService: DataService<MyEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }

    [WebGet]
    public IEnumerable<Customer> GetActiveCustomers()
    {
        return this.CurrentDataSource.Customers.Where(x=> ! x.IsDeleted); 
    }

}

在我的客户端,我添加了对该服务的服务引用,我能够将数据库查询为:

var context = new ServiceReference.MyWcfDataService(new Uri("http://localhost:10144/Services/MyWcfDataService.svc"));

// I am able to get reports as
var reports = context.Reports.ToList(); 

// now how do I invoke the operation 'GetActiveCustomers'?
// I am looking for something like:
var actCusts = context.Operations.GetActiveCustomers();

我知道我可以通过向http://localhost:10144/Services/MyWcfDataService.svc/GetActiveCustomers发出请求来调用该操作。但是,视觉工作室下载元数据的重点是什么。由于我也从.net应用程序中使用该服务,如果我可以使用intellicense访问该方法,那将是很好的。

换句话说在我的客户端上添加对该服务的引用时我可以看到:

<Schema Namespace="WebService.Data" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
      <EntityContainer Name="MyEntities" >
        <EntitySet Name="Customers" ..... etc />
        ... etc
        <FunctionImport Name="GetActiveCustomers" ReturnType="Collection(Customer)"  ... etc />
<{1>} service.edmx。 Visual Studio应该使用它来调用方法吗?

1 个答案:

答案 0 :(得分:1)

我担心在OData V3(WCF数据服务)上运行的添加服务引用不支持在.NET客户端中自动生成服务操作。但是,您可以在生成的服务上下文类中编写自己的方法来调用操作。像这样:

IEnumerable<Customer> GetActiveCustomers()
{
    Uri requestUri = new Uri(this.BaseUri.OriginalString.Trim('/') + "/GetActiveCustomers");
    return this.Execute<Customer>(requestUri);
}

使用此功能,您可以编写var actCusts = context.GetActiveCustomers();并将结果返回到.NET应用程序中。