Some of the disadvantages of WCF Data Services

Posted by on Comments (1)

ODataSince I’m working with WCF Data Services as my project’s data source, I’ve experienced some cons in WCF data services and going to share with you now. I hope Microsoft find a solution for these:

  • Some of lambda expressions don’t work on WCF Data Services. I want to list a few of these. (In some cases, lambda expressions can be replaced with old style LINQ but I prefer to use lambda):
  • Sub-queries are not supported in WCF data services.
  • Debugging in WCF data services is such a pain in the neck.
  • Application security. Implementing role based security in WCF data services is very difficult as I’m currently planning to implement a role based security in my application.

Maybe there are solutions for any of the items above but I don’t know about them. If you really know, update this post by sending comments.

A brief overview of OData and WCF data services

Posted by on Comments (0)

People all around the world want to share data. They want to share their interests with others. Companies want to share data too. For example, EBay wants to distribute data of latest products and interests to the web and enable other companies to use that data in their own websites. There were ways to do that:

  • RSS/ATOM feeds.
  • XML Web services.

But each of the items above has their own problems and both of them have lots of limitations. In fact, there is no way to query the source’s database. In the EBay example, it’s impossible to select some data with specific parameters except the EBay has been developed some methods in order to that but it’s not enough in some cases, as you know.

But there’s a new way to distribute data and that’s OData!

OData is the short form of “Open Data Protocol” which is a new standard way for distributing data. The result of OData is standard XML so you can access to it using any programming language/tools. Here’s the OData definition from its official website:

The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years.  OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.

Companies have started to use OData too; for example you can access Netflix’s latest movies and use them in your application using OData! In order to do so, visit http://odata.netflix.com.

The table below shows the libraries which currently support OData:

OData Currently supported programming languages' library

 

You can download any library you want depend on what programming language you’re working with. As you can see in the table above, .NET Framework 3.5 SP1 and 4 is supported. In order to use OData in .NET 3.5 SP1, you have to download and install Data Service Support for .NET Framework 3.5 SP1 and then start working with but if you’re developing your application based on .NET Framework 4, you don’t need anything at all because Data Services is supported built-in.

In order to get more information about OData standards and documentations, you can checkout: http://odata.org.

WCF Data Services

WCF Data Services is the implementation of OData in .NET Framework. Hopefully, Microsoft has done a great job on this and it’s very easy to use. Data Services is not dedicated to databases; in fact it’s all about entities. You can build any entities and access it via Data Services. For example you can create an Entity Framework 4 entity and the access it via a Data Service. It’s the same story for LINQ to SQL, XML, etc.

As an example, I decided to create a Data Service for my EF 4 database model. Here are steps you can create a Data Service:

  1. As I mentioned, I preferred to use LINQ to Entity in my project so first you need to do is to create a database model by creating an ADO.NET Entity Data Model.
  2. After creating the database model, it’s time to create the data service. There’s nothing else to do! If you want to create a data service, you can add new WCF Data Service file to the project.
    Adding a WCF Data Service
  3. After a WCF Data Service file to your project, Visual Studio will create a .NET class which is inherited to DataService class. The DataService class in a generic class so you have to assign a data source to it. If you’re using EF, all you need is to type the name of your entity. For example “NorthwindEntities”.
    wcf-data-service-class-view
  4. Then you need to set permissions for the data service. The WCF data service that is created by Visual Studio contains a static InitializeService method that has an input parameter as a DataServiceConfiguration class which includes some methods in order to set permissions for the data service. The most important method for setting permissions is SetEntitySetAccessRule that you can set access rule for your entity. Here’s an example:
  5. As you can see, SetEntitySetAccessRule accepts two parameters. The first parameter is the name of the entity as string and the second parameter is the entity set rights. In the example above, the entity set rights is set to AllRead which means that users can only read from this entity. If you want to allow users to read and also write data using your service, you can set the entity rights to “All”.

Now your service is ready to use! It’s that easy! You can add your service to another application as same as adding a WCF service and use it. But the difference is that the service is using OData to publish the data. Also you can use LINQ expressions to access whatever you want!

Unfortunately, some of the lambda expressions are not supported by WCF Data Services. For instance, you can’t use Single method like this:

var x = myService.Table.Single(p=>p.Id == 1);

You have to use Single like this:

var x = (from i in myService.Table where i.Id == 1 select i).Single();

I hope Microsoft fix this problem as soon as possible.

I’ll post about WCF Data Service in my future posts but for now if you want to know more about it, you can check out WCF Data Service Client Library in MSDN

Posted in: LINQ | WCF   Tags:

A start point to work with WCF services

Posted by on Comments (0)

It’s about 3 months that I’m researching on WCF because of a reservation system I’m developing right now. We’re working on a web service which enables our customers to reserve cars, tickets, etc.; so we need something like XML Web Service to do such a thing.

WCF is a new framework for building service-oriented applications. Microsoft wanted to provide its developers with a framework to quickly get a proper service-oriented architecture up and running. Using the WCF, you will be able to take advantage of all the items that make distribution technologies powerful. WCF is the answer and the successor to all these other message distribution technologies.

In this post, I’m going to show you how to develop your first WCF application using Microsoft Visual Studio 2008.

This first step is to create a new WCF application. Visual Studio implemented a template for it so you can create it easier:

Creating a new WCF application - Photo taken by myself.

Note that WCF Service Applications are available in .NET Framework 3 and 3.5 so you cannot make a use of WCF when you’re developing applications using .NET Framework 2 or below!

When want to create a WCF application, you’ll need to follow some rules:

First Step

The first step is to create an interface and list your methods you want to use in your service:

[ServiceContract]
public interface IService1
{
 
    [OperationContract]
    string GetData(int value);
 
}

Everything is normal except that, you must use some attribute in order to create WCF service’s interface. First, you must apply an attribute named “ServiceContract” to your interface. Second, you have to apply another attribute named “OperationContract” to each of your methods you want to use in your WCF service. Note that you need to import System.ServiceModel name space to your interface in order to make use of “ServiceContract” and “OperationContract” attributes.

Second Step

Now it’s time to create our WCF service by adding a new WCF service to our application. The important part when you added a new WCF service to your application is that you must implement your WCF service class from the interface you’ve just created. After you created your service class, you can start writing codes in your specified methods:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("Here is your value: {0}", value); 
    }
 
}

Third Step

When you create a new interface and class in a WCF application, you must apply some settings to your Web.Config file. Visual Studio automatically adds some lines of settings to your Web.Config file when you create a WCF application that contains the name of your interface and your service class.

Keep in mind that, you must update those file names in your Web.Config file if you want to change the name of that Service1.cs and Service1.svc. You can also add more interfaces and services if you want but you must apply them in Web.Config file:

<system.serviceModel>
<services>
  <service name="SampleService.Service1" 
  behaviorConfiguration="SampleService.Service1Behavior">
    <endpoint address="" binding="wsHttpBinding" 
    contract="SampleService.IService1">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" 
    contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="SampleService.Service1Behavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>