XPO – 11.1 Sneak Peek - WCF services for IDataStore

XPO Team Blog
19 April 2011

In XPO v2011.1 vol 1, we added support for WCF Services. These improvements provide the capability to create service and client for IDataStore as easy as it can only be.

In order to support this feature, the DevExpress.Xpo assembly will reference assemblies from .NET 3.0. Unfortunately, Visual Studio 2010 does not allow building projects whose Target Framework is set to 2.0, if they reference .NET 3.0 assemblies. To bypass this, it is necessary to set Target Framework to 3.0. Quite surprisingly, other Visual Studio versions do not demonstrate the above problem.
This does not mean that an application will not work on a machine that has only .NET 2.0 installed on it. If the project does not use the functionality of .NET 3.0, it will work just as before. Therefore, it should not affect your clients’ installations at all.

Let’s learn more about the implemented feature from a simple example.
For the beginning, we will create a new WCF Service Application project. Then, we will add references to the DevExpress.Data and DevExpress.Xpo assemblies and remove files with auto-generated interfaces for the service.

The next step is modifying the service class as follows: 

using DevExpress.Xpo;

using DevExpress.Xpo.DB;

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) {

        }

    }

}

To finish with the service, we will need to change some binding properties in its web.config file:

<system.serviceModel>

    <services>

        <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">

            <!-- Service Endpoints -->

            <endpoint address="" binding="basicHttpBinding" 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>

Now our service is ready, and it is time to implement the client part.
 
For the client, we will add a Console Application into our existing solution. Then, we will use the Persistent Object item template for a  Customer class:

using DevExpress.Xpo;

namespace ConsoleApplication1 {

    public class Customer : XPObject {

        public Customer(Session session) : base(session) { }

        string _CompanyName;

        public string CompanyName {

            get { return _CompanyName; }

            set { SetPropertyValue("CompanyName", ref _CompanyName, value); }

        }

        string _CompanyAddress;

        public string CompanyAddress {

            get { return _CompanyAddress; }

            set { SetPropertyValue("CompanyAddress", ref _CompanyAddress, value); }

        }

        string _ContactName;

        public string ContactName {

            get { return _ContactName; }

            set { SetPropertyValue("ContactName", ref _ContactName, value); }

        }

        string _Country;

        public string Country {

            get { return _Country; }

            set { SetPropertyValue("Country", ref _Country, value); }

        }

        string _Phone;

        public string Phone {

            get { return _Phone; }

            set { SetPropertyValue("Phone", ref _Phone, value); }

        }

    }

}

The final step is to modify the Main() of our console application as shown in the code below:

using System;

using DevExpress.Xpo;

using DevExpress.Xpo.DB;

namespace ConsoleApplication1 {

    class Program {

        static void Main(string[] args) {

            XpoDefault.DataLayer = XpoDefault.GetDataLayer("http://localhost:64466/Service1.svc",

            AutoCreateOption.DatabaseAndSchema);

            XpoDefault.Session = null;

            using (UnitOfWork uow = new UnitOfWork()) {

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

        }

    }

}

As you can see, we pass the address of our service into the GetDataLayer method of the XpoDefault class. It is very similar to what was already supported for Web Services.

Now if we run the service and client parts, we will see the following output:
screenshot
Take special 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:
screenshot

Happy XPOing!

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.