OpenLightGroup Blog

rss

Blogs from OpenLightGroup.net


An ADO.NET Entity Framework / WCF 4.0 Version Of Silverlight CRUD

I decided to make a ADO.NET Entity Framework (EF) / Windows Communication Foundation (WCF) version of my tutorial: “RIATasks: A Simple Silverlight CRUD Example (using View Model and Rx Extensions)”. After doing so, I see that EF does not make much of a difference either way, but, while they have made great strides in easing deployment of WCF 4.0, major issues still remain.

ADO.NET Entity Framework

EF could have been used without too much problem. It only took three minutes to convert the application from using Linq to SQL to EF. After using the wizard to create a .edmx file, I only had to make trivial changes to the Linq syntax. For example I changed this:

    public string UpdateTask(Task objTask)
    {
        string strError = "";
        RIATasksDBDataContext DB = new RIATasksDBDataContext();
        try
        {
            var result = (from Tasks in DB.Tasks
                            where Tasks.TaskID == objTask.TaskID
                            where Tasks.UserID == GetCurrentUserID()
                            select Tasks).FirstOrDefault();
            if (result != null)
            {
                result.TaskDescription = objTask.TaskDescription;
                result.TaskName = objTask.TaskName;
                DB.SubmitChanges();
            }
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        return strError;
    }

 

To this:

 

    public string UpdateTask(Task objTask)
    {
        string strError = "";
        int intCurrentUser = GetCurrentUserID();
        RIATasksEntities DB = new RIATasksEntities();
        try
        {
            var result = (from Tasks in DB.Tasks
                            where Tasks.TaskID == objTask.TaskID
                            where Tasks.UserID == intCurrentUser
                            select Tasks).FirstOrDefault();
            if (result != null)
            {
                result.TaskDescription = objTask.TaskDescription;
                result.TaskName = objTask.TaskName;
                DB.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        return strError;
    }

 

The main reason I continue to use Linq to SQL in most of my code examples is DotNetNuke works seamlessly with it and it does not with EF. Also, Linq to SQL is simple, it is very hard to mess it up. But, EF would not have been a big deal to use instead of Linq to SQL.

 

Windows Communication Foundation 4.0

Converting the .asmx web services to WCF was not hard. it also took only three minutes. Mostly just changing the attribute on each web method from [WebMethod] to [OperationContract].

The main problem is that it automatically put a ton of stuff in the Web.config:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="RIATasks.Web.WebService.customBinding0">
          <binaryMessageEncoding />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="RIATasks.Web.WebService">
        <endpoint address="" binding="customBinding" bindingConfiguration="RIATasks.Web.WebService.customBinding0"
          contract="RIATasks.Web.WebService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

 

That is a lot of stuff that can go wrong as you move an application from environment to environment. Also this is just for one service, what if there are multiple services?

According to the documentation at this link,

…when you consider the net complexity of using WCF 3.x, one might reasonably conclude that it’s harder to use than its predecessor ASP.NET Web services (ASMX). With ASMX, you were able to define a [WebMethod] operation and the runtime automatically provided a default configuration for the underlying communications. When moving to WCF 3.x, on the other hand, developers have to know enough about the various WCF configuration options to define at least one endpoint. And the daunting number of configuration options often scares some developers away.

In an effort to make the overall WCF experience just as easy as ASMX, WCF 4 comes with a new “default configuration” model that completely removes the need for any WCF configuration. If you don’t provide any WCF configuration for a particular service, the WCF 4 runtime automatically configures your service with some standard endpoints and default binding/behavior configurations. This makes it much easier to get a WCF service up and running, especially for those who aren’t familiar with the various WCF configuration options and are happy to accept the defaults, at least to get started.

This goes a long way toward making things easier, however yank all the configuration stuff out of the web.config (none of which is needed when using .asmx web services) and you get helpful screen indicating that there is SOME configuration that is needed.

Also, while WCF will now automatically configures a binding to a HTTP address, it only configures one. If I have a service at adefwebserver.com and www.adefwebserver.com, only one will work, the other will silently fail.

One more thing, if you are deploying WCF on a Windows 2003 IIS web server, you have to ensure that your IIS settings specify only one authentication scheme.

All of these settings are implemented by millions of people each day, but I hope you can understand why I omitted all this from a beginner’s tutorial.

I am making the download available here:

http://silverlight.adefwebserver.com/files/RIATasksEF_WCFVersion.zip

However, I do not recommend using it because if you try to insert or update a large amount of text it will throw an error. I suspect it is a WCF configuration problem, but again the .asmx web services work fine under the same conditions.





Comments are closed.