XPO – 11.1 Sneak Peek - WCF services for IObjectLayer

XPO Team Blog
17 May 2011

In the two previous blog posts, we already described how to provide the IDataStore and ICachedDataStore implementations, working via WCF Services. In this blog post, we will talk about implementing a distributed object layer service (IObjectLayer/ISerializableObjectLayer), working via WCF. Creating a WCF service for ISerializableObjectLayer is a bit more complex than for IDataStore and ICachedDataStore, but it is still very similar.

First, we will create a new Class Library project and add a Customer class via the Persistent Object item template to it (you can see the source code of this class in our first blog). Then, we will create a new WCF Service Application project and remove files with auto-generated interfaces for the service. Since our service needs to know about persistent classes, we will add a project reference to our class library and modify our service class as follows:

  1: using DevExpress.Xpo;
  2: using DevExpress.Xpo.DB;
  3: using DevExpress.Xpo.Metadata;
  4: using ClassLibrary1;
  5: ...
  6: public class Service1: SerializableObjectLayerService {
  7:     public Service1()
  8:         :base(new ObjectServiceProxy()){
  9:     } 
 10:     static Service1() {
 11:         string connectionString = MSSqlConnectionProvider.GetConnectionString("localhost", "ServiceDB");
 12:         IDataStore dataStore = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema);
 13:         XPDictionary dictionary = new ReflectionDictionary();
 14:         dictionary.CollectClassInfos(typeof(Customer).Assembly);
 15:         XpoDefault.DataLayer = new ThreadSafeDataLayer(dictionary, dataStore);
 16:         XpoDefault.Session = null; 
 17:     }
 18: }
 19: public class ObjectServiceProxy : SerializableObjectLayerProxyBase {
 20:     protected override SerializableObjectLayer GetObjectLayer() {
 21:         return new SerializableObjectLayer(new UnitOfWork(), true);
 22:     }
 23: }
 24: 

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

  1: <system.serviceModel>
  2:  <services>
  3:    <service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
  4:      <!-- Service Endpoints -->
  5:      <endpoint address="" binding="basicHttpBinding"
  6: contract="DevExpress.Xpo.DB.ISerializableObjectLayerService">
  7:        <identity>
  8:          <dns value="localhost"/>
  9:        </identity>
 10:      </endpoint>
 11:    </service>
 12:  </services>
 13:  <behaviors>
 14:    <serviceBehaviors>
 15:      <behavior name="WcfService2.Service1Behavior">
 16:        <serviceMetadata httpGetEnabled="true"/>
 17:        <serviceDebug includeExceptionDetailInFaults="false"/>
 18:      </behavior>
 19:    </serviceBehaviors>
 20:  </behaviors>
 21: </system.serviceModel>
 22: 

At this time, our service is ready to use and all that we need to do is to write the client part.

For the client, we will add a Console Application into our existing solution and add a project reference to the class library with our Customer persistent class. The Main() method should also be modified as follows:

  1: using DevExpress.Xpo;
  2: using DevExpress.Xpo.DB;
  3: using DevExpress.Xpo.Metadata;
  4: using ClassLibrary1;
  5: ...
  6: namespace ObjectWcfClient {
  7:     class Program {
  8:         static IObjectLayer ObjectLayer;
  9:         static void Main(string[] args) {
 10:             ISerializableObjectLayer serializableObjectLayer =  
 11:                 new SerializableObjectLayerServiceClient("BasicHttpBinding_ObjectLayer");
 12:             serializableObjectLayer.CanLoadCollectionObjects.ToString(); //Check connection
 13:             XpoDefault.DataLayer = null;
 14:             XpoDefault.Session = null;
 15:             ObjectLayer = new SerializableObjectLayerClient(serializableObjectLayer);
 16:  
 17:             using(UnitOfWork uow = new UnitOfWork(ObjectLayer)) {
 18:                 using(XPCollection<Customer> customers = new XPCollection<Customer>(uow)){
 19:                     foreach(Customer customer in customers) {
 20:                         Console.WriteLine("Company Name = {0}; ContactName = {1}", 
 21:                             customer.CompanyName, customer.ContactName);
 22:                     }
 23:                 }
 24:             } 
 25:             Console.WriteLine("Press any key...");
 26:             Console.ReadKey();
 27:         }
 28:     }
 29: }
 30: 

The final step will be adding the App.config file into the client project and modifying it as follows:

  1: <?xml version="1.0" encoding="utf-8" ?>
  2: <configuration>
  3:   <system.serviceModel>
  4:     <bindings>
  5:       <basicHttpBinding>
  6:         <binding name="BasicHttpBinding_ObjectLayer" maxBufferSize="2147483647"
  7:             maxReceivedMessageSize="2147483647">
  8:           <security mode="None" />
  9:         </binding>
 10:       </basicHttpBinding>
 11:     </bindings>
 12:     <client>
 13:       <endpoint address="http://localhost:64466/Service1.svc" binding="basicHttpBinding"
 14:           bindingConfiguration="BasicHttpBinding_ObjectLayer"
 15:           contract="DevExpress.Xpo.DB.ISerializableObjectLayerService" name="BasicHttpBinding_ObjectLayer" />
 16:     </client>
 17:   </system.serviceModel> 
 18: </configuration>
 19: 

Now if we run the service and client parts, we will see the following output:

Result
As you can see, our client connects to the service and loads serialized persistent objects from it, and not just plain statements (insert, update, delete, select). You can learn more on which capabilities it opens for XPO customers at the end of our introductory blog for the object layer.

Happy XPOing!Winking smile

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.