Pergunta

I have the following WCF Data Service

[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); 
    }

}

On my client side I add a service reference to that service and I am able to query the database as:

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();

I know I can invoke the operation by making a request to http://localhost:10144/Services/MyWcfDataService.svc/GetActiveCustomers . But then what is the point of visual studio downloading the metadata. Since I am consuming that service also from a .net application it will be nice if I can access that method with intellicense.

In other words on my client when I added a reference to that service I could see:

<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 />

on service.edmx. That should be used by Visual Studio to invoke the methods?

Foi útil?

Solução

I'm afraid the Add Service Reference working on OData V3 (WCF data service) doesn't support automatically generating service operations in .NET client. However, you can write your own methods in generated service context class to invoke the operations. Like this:

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

With this you can write var actCusts = context.GetActiveCustomers(); and get the results back in your .NET application.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top