XPO

XPO – 11.1 Sneak Peek - WCF services for IDataStore

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!

Published Apr 19 2011, 02:30 AM by
Bookmark and Share

Comments

Luca Poretti

This is great !

But query on slow wan can be expensive ... Can you expand the example to add caching (with DataCacheRoot on the server and DataCacheNode on the client) ?

April 19, 2011 5:56 AM

Bjoern Isemann_1

can't wait to see that functionality in XAF in the future. Would be great if you can use a wcf service to build server-client apps with XAF. Thats something i'm missing (or maybe haven't found yet).

April 19, 2011 6:32 AM

Slava D (DevExpress)

@Luca Poretti:

A blog post about data caching is being prepared and will be ready soon.

April 19, 2011 7:01 AM

Dennis (DevExpress Support)

@Bjoern:

XAF is still a regular XPO application by its domain, and so it was always possible to build client-server applications, communicating via WCF and other communication channels.

There are some examples in the Support Center and blogs: 1) search.devexpress.com

2) community.devexpress.com/.../xaf-10-advanced-data-layer-features.aspx

If you need any further assistance on this, please feel free to contact our Support Team.

What we did as part of the feature described in this blog is that we just made it much easier for our customers to build service and client applications communicating via WCF. Now instead of implementing your own IDataStore and other implementations, you can use the out-of-the-box solution with ease - just create a service derived from the standard XPO service class and specify the service's address on the client. That's it.

April 19, 2011 7:11 AM

Paul Cunningham

Hello Dennis,

What's the difference between DataStoreService and XPODataService (already defined in DevExpress.Xpo.Services) ?

Regards,

Paul

April 19, 2011 10:25 AM

Nate Laff

Are there speed advantages to this method vs. direct sql queries over the wire in an internet app scenario?

April 19, 2011 1:10 PM

Dennis (DevExpress Support)

@Paul: The latter class is a part of the OData Provider for XPO. I suggest you check out the tv.devexpress.com webinar video, for basic information on it.

It is also mentioned at the end of this awesome video: tv.devexpress.com (stop at ~1.05). I hope you find this information helpful.

April 19, 2011 2:02 PM

Dennis (DevExpress Support)

@Nate: I doubt whether anything can be faster than a direct call to the database. In any case, the difference in speed between these two methods will not be enough to worry about...

I suggest you review a similar ticket in the Support Center, for more information: www.devexpress.com/issue=Q309614

The main point of using WCF for IDataStore is to build distributed client-server applications. For instance, a client can be connected to a middle-tier service and in general you may not know anything about what stays behind this service (database or other services). I hope this makes more sense.

April 19, 2011 2:15 PM

Nate Laff

Dennis, thanks. Yeah, I get the idea of distrubuted services. Was hoping for an additional "Server Mode" effect, where the call is made to the service, and all processing happened on the server end rather than client creating increased performance in scenarios like my own :)

April 19, 2011 3:06 PM

Glen Harvy

At last there's something in 2011 that I am really looking forward to.

Can you please zip up the VS Project for this simple example and make it available for download. I can then use that as a training aid as well as a base to build on.

Thanks ...

April 19, 2011 5:34 PM

Sascha

Great to see that. I'm really looking forward to get my hands on this!

April 20, 2011 1:18 AM

Dennis (DevExpress Support)

@Glen: Thank you for your input. As always, any published feature in the product should have a demo + docs. This particular one is not an exception. There will be several demos, showing new XPO features.

@Sascha: We are glad to hear that you liked this feature! Thank you for your feedback!

April 20, 2011 2:07 AM

Dennis (DevExpress Support)

@Nate: Again about speed, we would bring more impression of what happens behind the scenes and how it may affect the performance.

When using WCF,  queries and results are packed in single batches (the SelectData and ModifyData methods of IDataStore). When using direct queries, the select statements are transported one by one, the update statements may be packed in batches for certain connection providers only.

So, if you have a fast channel with a large ping, WCF may be faster in certain cases, especially if you enable compression for it (there should be an SDK sample, showing how to do this. However, there is no definite answer on what is faster - everything depends on certain situations.

Hope you find this information helpful.

April 20, 2011 4:27 AM

Theodore p

It’s always great to see new features in XPO! I attempted to create a publisher using WCF almost three years ago. It turned out to be a nightmare that cost me many days of hard work and ended up in the trash bin. This one looks ridiculously easy! Well done!

April 20, 2011 8:29 AM

Dennis (DevExpress Support)

@Theodore: Thank you for your feedback! I hope you will really enjoy this new approach once you get your hands on 11.1. Please share with us your feeling after that. Thank you in advance!

April 21, 2011 3:42 AM

Dennis (DevExpress Support)

April 21, 2011 5:30 PM

Sascha

Will this play nicely with authentication? Something like BASIC auth or some based on membership providers? That would be really nice!

April 29, 2011 12:12 AM

Vassilios Pallis

Is that means that we can at last use WCF with XPO in Silverlight 4 Applications ?

May 4, 2011 2:15 PM

Lucy

Dennis, is there better XPO support for RIA Services coming? I tried it, and there is a SC article about it, but it's very hard to do.

May 4, 2011 2:51 PM

XPO

In this blog post we will talk about peculiarities of using IDataStore WCF services in Silverlight projects

May 16, 2011 7:15 AM

XPO

In the two previous blog posts, we already described how to provide the IDataStore and ICachedDataStore

May 17, 2011 4:58 PM

Vassilios Pallis

I followed exactly the example but i cannot get it to work. The connection is always empty. You don't mention anything about connection string and how to build it.

May 23, 2011 5:11 AM

Dennis (DevExpress Support)

@Vassilios:

Please check out the following line in the service code:

string connectionString = MSSqlConnectionProvider.GetConnectionString("localhost", "ServiceDB");

Then we connect to our service from the client:

XpoDefault.DataLayer = XpoDefault.GetDataLayer("localhost/Service1.svc", AutoCreateOption.DatabaseAndSchema);

Please feel free to contact our Support Team in case of any further difficulties.

June 10, 2011 5:13 AM

Ivan Rodriguez_1

Is not possible to use the Target Framework 4.0 ? It's a must in my project. I tried to use Framework 4.0 but did't work, only worked wtih 3.0

November 11, 2011 9:07 AM

stefano del furia

Hi, what about authentication? Something like BASIC auth or some based on membership providers?

I have created an HttpModule but i don't know how to pass the credential !!!

December 18, 2011 9:14 AM

Dennis (DevExpress Support)

@Ivan: Yes, it is possible to use it with .NET 4+

You can check a working example at www.devexpress.com/.../Q532928

We are also planning to make these sample downloable from www.devexpress.com/.../List

October 23, 2013 3:45 AM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners