Description:
This article provides a step-by-step instruction on how to create a WCF Data Service, associate the WcfServerModeSource component with it and use it as a GridControl datasource
Answer:
- Create a new Windows Forms application.
- Add new WCF Service Application project to a solution:
- Add an Entity Data Model to the WCF Service Application project:
- Generate Entity Data Model from a database (in current tutorial we will use the AdventureWorks database):
- Add a Wcf Data Service to the WCF Service Application project and configure it as follows (note that the WCF Data Service template is available in Visual Studio 2015, but not in Visual Studio 2017):
C#public class WcfDataService1 : DataService<AdventureWorksEntities>{
// 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.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
Visual BasicPublic Class WcfDataService1
Inherits DataService(Of AdventureWorksEntities)
' This method is called only once to initialize service-wide policies.
Public Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
' TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
' Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead)
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All)
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2
End Sub
End Class
- Build the solution and then add a Service Reference to the Wcf Data Service:
- Place the GridControl on a form. Make sure that the DevExpress.Data.vXX.Y assembly is added to the project references.
- Import a WcfServerModeSource.ServiceReference1 namespace to a Form1 code and create an instance of the AdventureWorksEntities type:
C#AdventureWorksEntities db;
public Form1(){
InitializeComponent();
db = new AdventureWorksEntities(new Uri("http://localhost:61617/WcfDataService1.svc"));
}
Visual BasicPrivate db As AdventureWorksEntities
Public Sub New()
InitializeComponent()
db = New AdventureWorksEntities(New Uri("http://localhost:61617/WcfDataService1.svc"))
End Sub
- Create an instance of the WcfServerModeSource type and assign it to the GridControl.DataSource property:
C#private void Form1_Load(object sender, EventArgs e){
gridControl1.DataSource = new WcfServerModeSource()
{
KeyExpression = "EmployeeID",
Query = db.Employee
};
}
Visual BasicPrivate Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
gridControl1.DataSource = New WcfServerModeSource() With {.KeyExpression = "EmployeeID", .Query = db.Employee}
End Sub
Why do we need "var query = from […]" statement in step 9?
We don't use it for anything anyway.
Hi,
Yes, you are right. This query is not used. Thank you for drawing our attention to this. We will update this article and example shortly.
how to use where condition in WcfServerModeSource()
Hello Aiswarriya,
To process your recent post more efficiently, I created a separate ticket on your behalf: T233548: How to add fixed filter condition in WCF Server Mode?. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.
Hi
Can you please post a Where condition example public as the ticket T233548 is not viewable.
Thanks
Hi Nigel,
To add a fixed filter condition, use the WcfServerModeSource.FixedFilterCriteria property. Refer to the Criteria Language Syntax document that describes how to build a filter string.
How to use pagination on a winforms project without creating an wcf service project ?
Can I just put wcfServerModeSources on the winforms project itself ?
Or some other way ?
Also I am not using entity framework, but DataTables build and filled in code
Hello,
To avoid any misunderstanding, I've created a separate How to use pagination on a winforms project without creating an wcf service project ? ticket on your behalf. Please refer to it for further discussion.