RIA Service in 5 minutes

 

Hi all,

 

I’ve previously blogged about how mind blowing WCF RIA Services are here

This week i found myself working with RIA Services again and this time I’ve got to show you just how easy it is (is case you didn’t take my advise and go look it up Punk

So, fire up VS2010 and create a new Silverlight Business Application

image

Two projects will be created for you, the Web project is where your DataAccess and Business logic lies, the other project is your Silverlight application.

Create an entity framework model ORM for your data (you know how to do this right?)

Next create a new Domain Service Class in the Web/Services folder

image

image

Compile.. that’s your server ready to serve up your data.

Now lets move into the Silverlight app, and add the following mark-up to your xaml

First add the domain datasource, set it to auto load and set the Query name to the name of the query that has been injected into your client app, (let intellisense help you; it will be in the Web.Services namespace context object.

        <domainControls:DomainDataSource x:Name="dataPads" LoadSize="20" AutoLoad="True" QueryName="GetPadocks">
            <domainControls:DomainDataSource.DomainContext>
                <data:FarmContext />
            </domainControls:DomainDataSource.DomainContext>
        </domainControls:DomainDataSource>

 

Add a datagrid use ElementBinding to the domainDataSource above (i’ve also put it in a telerik busyindicator to provide feedback that the data is loading)

<telerik:RadBusyIndicator IsBusy="{Binding ElementName=dataPads, Path=IsLoadingData}">
    <dataControls:DataGrid x:Name="padocksGrid" MinHeight="100"
         ItemsSource="{Binding ElementName=dataPads, Path=Data}"  />
</telerik:RadBusyIndicator>
 

 

Ok I’ve once again skimped on some of the detail but it’s a really nifty approach and i encourage you to give it a go.

(Also seeming your service is a DomainContext you can expose your data as OData a post for another day Secret telling smile )

ASP Glimpse Module

 

Now there’s a pretty sweet way of looking at what’s happening on your server, I’ve picked this tip up from watching SHanselmans MS Stack Of Love (recommended watch) @ MIX11 this year.

Here’s how, (pardon the screenshot file sizes as usual ..)

1) Create an application

image

2) Add a package

image

3) Search online for glimpse and install

image

4) Add at least one Trace to demo an additional Glimpse feature

image

5) Fire up your app and go to <appurl>/Glimpse/Config, and Turn Glimpse On

image

 

6) Click on the beady little eye on the bottom right corner of your webpage

image

7) Voilá

image

Cloud Computing (Azure) here I come

 

image

 

I’ve been playing (seriously playing) with Visual Studio LightSwitch for the last few weeks and I have to say it’s knocking my socks off, not since I programmed in MS Access (am I allowed to say that? Should I even admit where it all started for me back in 1995….), anyway,,, not since I programmed in MSAccess have I really seen a real a tool that really puts the R into RAD when it comes to small system data managment.

So now I want to publish my efforts as I’ve suckered a friend into some peer review, I could publish it to one of my webservers, manually set up a Sql Database etc (don’t have lightswitch deployment support on any of my servers just yet); but this Azure option was just too tempting.

image

 

Ok.. it didn’t take 15 minutes (more like 10) but I’ve now got access, hopefully I’ll do a follow up after I publish the final version of my app… stay tuned..

image

RESTful WCF Services

 

WCF not only provides SOAP, it’s also capable of providing RESTful services through use of the WebGet and WebInvoke attributes in the System.ServiceModel.Web assembly in conjunction with the webHttpBinding and webHttp behaviour. In fact, WebGet/WebInvoke are not really required required (however PUT is the default Http Verb and you won’t be able to specify what methods correspond to urls.

Here’s a sample;

[ServiceContract]
    public interface IPersonInfo
    {
        [OperationContract]
        [WebGet(UriTemplate = "/Details")]
        string Details();
 
        [OperationContract]
        [WebGet(UriTemplate = "/Person")]
        Person GetRandomPerson();
 
        [OperationContract]
        [WebGet(UriTemplate = "/Person?format=xml", ResponseFormat = WebMessageFormat.Xml)]
        Person GetRandomPersonXml();
 
        [OperationContract]
        [WebGet(UriTemplate = "/Person?format=json", ResponseFormat = WebMessageFormat.Json)]
        Person GetRandomPersonJson();
    }

 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class PersonService : IPersonInfo
    {
        public string Details()
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
            return "Mon-Fri 9am to 5pm";
        }
 
        public Person GetRandomPerson()
        {
            return new Person { Address = "Limerick", Name = "Brian", Country = "Ireland" }; // not so random..
        }
 
        public Person GetRandomPersonXml()
        {
            return new Person { Address = "Limerick", Name = "Brian", Country = "Ireland" }; // not so random..
        }
 
        public Person GetRandomPersonJson()
        {
            return new Person { Address = "Limerick", Name = "Brian", Country = "Ireland" }; // not so random..
        }
    }

Host this service in IIS for example and you’ll be able to get the following results

image

 

As REST support only arrived in WCF 3.5 there is still a lack of support in certain areas, e.g. error handling, caching etc; what I’ve seen done in practise is people continue to use the REST Starter kit from codeplex.

OrderBy multiple fields with Linq extension methods.

 

Not many comments needed I imagine.

The “ThenBy” extension method is what you’re looking for.

image

ASP MVC Controller Exception handlers

 

When you crate a new MVC project a view called Error.aspx is created for you in the Views/Shared folder.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

 
<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Error
</asp:Content>
 
<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
</asp:Content>

To instruct controller actions to use this Error handler you need to use the HandlErrorAttribute action filter, this is the default exception handler present in MVC.
HandleError is used to specify an exception type to handle and a View (and Master View if necessary) to display if an action method throws an unhandled exception that matches or is derived from the specified exception type.

Some points to note:

  • If no exception type is specified, then the filter handles all exception.
  • If no view is specified then the default Error view is used.
  • As mentioned earlier, exceptions are caught by base types, so it’s important to specify an order catching the most specific exception types first (much like a standard try catch code block.
  • The handler won’t get called in debug builds as it checks the HttpContext.IsCustomErrorEnabled (yellow screen of death is preferred)
  • e.g.
// Dont do this
[HandleError(Order=1, ExceptionType=typeof(Exception)]
[HandleError(Order=2, ExceptionType=typeof(ArgumentNullException, View=ArgNull)]

 

You’ll need to reverse the order, because if you want the ArgumentNullException to be handled differently, the exception shouldn’t get swallowed by the typeof(Exception) handler.

Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation

Calendar

<<  July 2017  >>
MoTuWeThFrSaSu
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar

Month List