Files to look at:
- Program.cs (VB: Program.vb)
- Service1.svc.cs (VB: Service1.svc.vb)
- Web.config (VB: Web.config)
Scenario
In this example, we will create a WCF IDataStore service that will be used by our client (Console Application) as a data layer. Instead of the direct connection to the database, our client will connect to a remote service, which is way more secure and thus important in many enterprise scenarios as database connection settings are not exposed to the client.
Steps to implement
1. Create a new WCF Service Application project and add references to the DevExpress.Data and DevExpress.Xpo assemblies and remove files with auto-generated interfaces for the service.
2. Modify the service class as shown in the Service1 file. This service initializes a connection provider and stores it in the static DataStore property, which is then used by the base DataStoreService class.
3. Change some binding properties as shown in the example's web.config file. At this stage, the service part is ready to work and we need to implement a client to consume data from our data store service (for demonstration purposes, we will create a Console Application).
4. Add the Console Application into the existing solution.
5. Add a new code file for a Customer class using the DevExpress v1X.X ORM Persistent Object item template. See a code of Customer class in the ConsoleApplication\Customer code file.
6. Pass the address of our service into the GetDataLayer method of the XpoDefault class. For this, modify the Main method of the Console Application as shown in the ConsoleApplication\Program code file. Please note that the port number in the connection string may be different. You can check it in the properties of the service project in the Solution Explorer:
As a result, we will see the following output:
Important notes
If you are using an XAF client, then in the simplest case, you can just set the XafApplication.ConnectionString to the address of your data store service (http://localhost:55777/Service1.svc). Refer to the Connect an XAF Application to a Database Provider help article for more details.
Troubleshooting
-
If WCF throws the "Request Entity Too Large" error, you can apply a standard solution from StackOverFlow: http://stackoverflow.com/questions/10122957/
-
If WCF throws the "The maximum string content length quota (8192) has been exceeded while reading XML data. " error, you can extend bindings in the following manner as per http://stackoverflow.com/questions/6600057/the-maximum-string-content-length-quota-8192-has-been-exceeded-while-reading-x:
XML<bindings>
<basicHttpBinding>
<binding name="ServicesBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed" >
<readerQuotas maxDepth="2147483647"
maxArrayLength="2147483647"
maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
See Also:
How to use XPO with a Web Service
Transferring Data via WCF Services
How to connect to a remote data service from a Silverlight application
How to create a data caching service that helps improve performance in distributed applications
How to implement a distributed object layer service working via WCF
How to connect to remote data store and configure WCF end point programmatically
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.Data.Filtering;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
XpoDefault.DataLayer = XpoDefault.GetDataLayer("http://localhost:55777/Service1.svc", AutoCreateOption.DatabaseAndSchema);
XpoDefault.Session = null;
using(UnitOfWork uow = new UnitOfWork()) {
if(uow.FindObject(typeof(Customer), new BinaryOperator("ContactName", "Alex Smith", BinaryOperatorType.Equal)) == null) {
Customer custAlex = new Customer(uow);
custAlex.ContactName = "Alex Smith";
custAlex.CompanyName = "DevExpress";
custAlex.Save();
Customer Tom = new Customer(uow);
Tom.ContactName = "Tom Jensen";
Tom.CompanyName = "ExpressIT";
Tom.Save();
uow.CommitChanges();
}
using(XPCollection<Customer> customers = new XPCollection<Customer>(uow)) {
foreach(Customer customer in customers) {
Console.WriteLine("Company Name = {0}; ContactName = {1}", customer.CompanyName, customer.ContactName);
}
}
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
}
}
C#using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using System.ServiceModel;
namespace WcfService1 {
public class Service1 : DataStoreService {
public static IDataStore DataStore;
static Service1() {
string connectionString = MSSqlConnectionProvider.GetConnectionString("localhost", "ServiceDB");
DataStore = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema);
}
public Service1()
: base(DataStore) {
}
}
}
Code<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings />
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServicesBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="ServicesBinding" contract="DevExpress.Xpo.DB.IDataStoreService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>