.NET, Featured, RIA, Headline, WCF RIA Services »

[6 Dec 2011 | 6 Comments]

WCF RIA Services Contrib is an open-source CodePlex project, maintained by Colin Blair, which consists of a collection of community contributions that can be used with WCF RIA Services.  At the moment, it consists of ComboBoxExtensions, a Data Validation Framework, Entity Tools, Entity Graph, Fluent Metadata API & T4RIA – you can find more info on those here.

 

Recently, I contributed a bunch of extension methods to the Entity Tools component.  These are methods I tend to use in projects, so I figured: why not release them to the community? :)

 

An overview:

 

  • public static IEnumerable YieldChildEntities(this Entity parent, EntityChangeSet changeSet,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static IEnumerable YieldChildEntities(this Entity parent, EntityContainer container,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static void DeleteChildEntities(this Entity parent, EntityContainer container,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static void DetachChildEntities(this Entity parent, EntityContainer container,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static void RejectChangesOnChildEntities(this Entity parent, EntityContainer container,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static void RejectChangesOnChildEntities(this Entity parent, EntityChangeSet changeSet,
    bool includeDirectChildrenOfEntityBaseType = false)
  • public static void AttachRange<T>(this EntitySet<T> entitySet, IEnumerable<T> entitiesToAttach) where T : Entity
  • public static IEnumerable<T> GetEntitiesOfType<T>(this EntityChangeSet changeSet, bool includeAddedEntites = true, bool includeModifiedEntities = true, bool includeDeletedEntities = false) where T : Entity
  • public static bool HasValidationErrors(this System.ServiceModel.DomainServices.Client.SubmitOperation submitOperation)
  • public static bool HasValidationErrors<T>(this System.ServiceModel.DomainServices.Client.SubmitOperation submitOperation) where T:Entity
  • public static IEnumerable<ValidationResult> GetAllValidationErrors(this System.ServiceModel.DomainServices.Client.SubmitOperation submitOperation)
  • public static IEnumerable<ValidationResult> GetAllValidationErrors<T>(this System.ServiceModel.DomainServices.Client.SubmitOperation submitOperation) where T:Entity

 

At the moment, the only way to access these is by downloading the source code, but in the (near) future, they will be included in the NuGet package for Entity Tools, so you might want to keep an eye on that. 

 

Enjoy! :)

.NET, Featured, Silverlight, Headline, WCF RIA Services »

[21 Nov 2011 | 1 Comments]

design-museum-extra-normal-clock-2009Sooner or later, you’ll run into this, if you haven’t already: the dreaded timeout error (or NotFound error), especially with long running operations or queries. The problem? Not all timeouts are the same (to quote a famous writer: all timeouts are created equal, but some timeouts are more equal than others). An error like that can come from your binding (which has a SendTimeout property), but it can also come from your data layer: there are timeouts that can be set on your SQL commands, and on your SQL connections.

 

Let’s look into the binding timeout first. We’re using WCF RIA Services, so you’ll need to gain access to the binding on your endpoint. This can easily be achieved through the OnCreated method of your DomainContext, as such:

 

public  partial class MyDomainContext
{
    partial void OnCreated()
    {
        PropertyInfo channelFactoryProperty = this.DomainClient.GetType()
                                                  .GetProperty("ChannelFactory");
        if (channelFactoryProperty == null)
        {
            throw new InvalidOperationException(
              "There is no 'ChannelFactory' property on the DomainClient.");
        }

        ChannelFactory factory = (ChannelFactory)channelFactoryProperty
                                 .GetValue(this.DomainClient, null);

        factory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0); 
    }
}

 

With this code in place (you put this where the generated DomainContext code resides – make sure you use the correct namespace, this must reside in the same namespace as your DomainContext), the SendTimeout on your binding is now set to 10 minutes: this makes sure the request can now last 10 minutes. You might notice there are other timeouts you can set here as well: CloseTimeout, OpenTimeout and ReceiveTimeout, but: SendTimeout is the one that defines the interval for the operation to complete before a timeout error is thrown.

 

Next on the list: the command timeout. If you’re accessing a database in your domain operation, this can timeout as well: the command timeout is what defines the maximum amount of time that’s given to the command to execute the provided SQL code (default: 60 seconds). For long running queries on your database, you will need to change this. We’re using WCF RIA Services, and let’s assume we’re also using the Entity Framework: on your DomainService, there’s an overrideable method, CreateObjectContext. This is where you can define the command timeout, as such:

 

protected override MyEntities CreateObjectContext()
{
    var objectContext = base.CreateObjectContext();
    objectContext.CommandTimeout = 1200;
    return objectContext;
}

 

And on the the last one: connection timeout. This is the time you allow to open a connection to your database. Typically, this should be small: if it takes more than, say, a minute to open your connection, you know something is seriously wrong. But if you want to change this (the default is 15 seconds), you can do it via the connection string:

 

Data Source=(local);Initial Catalog=AdventureWorks;
Integrated Security=SSPI;Connection Timeout=30

 

If you keep these 3 timeout values in mind, timeouts for (long running) operations should be a thing of the past.

 

Oh, one more thing: if you’re working with authentication, and your require authenticated access to your services, the authentication ticket could time out as well (you can define this value yourself), resulting in an Access Denied error. To avoid this, using sliding expiration (true by default) will already help a lot (and is a good practice for most applications). Next to that, make sure you correctly catch & handle errors of this type, by asking the user to log in again when his authentication ticket has expired. This is how this could look in your web.config file:

 

<authentication mode="Forms">
  <forms slidingExpiration="true" timeout="180"/>
</authentication>



Happy coding! :-)

Silverlight, WCF RIA Services, Featured »

[4 Nov 2011 | 3 Comments]

When you’re working with WCF RIA Services, there are various ways to handle your validation: you can use the built-in data annotations, you can write custom validators on property level, on entity level, you can do cross-field validation, … and sometimes, you’ll need information in your validators that isn’t related to your Entity.  For example: your validation rule could be different depending on the role of the logged in user, depending on the location he’s at, …

 

For scenarios like this, you can use the ValidationContext: you can pass in a Dictionary<object, object> containing any state information you might need to ensure your validation rule works as expected.  For more information on this, you can find a great explanation at Jeff Handley’s blog.

 

That said, when you’re working with this, you might notice something strange when you put breakpoints in your validator: even though you’re passing in the correct state information through the ValidationContext, sometimes the Items collection might seem empty (just have a look around the internet, and you’ll notice quite a few people ran into this problem).  When someone mentions this problem, they’re typically using controls like a DataForm or DataGrid.  So, why is this?

 

Well: validation can be triggered at different moments, and by different controls.  Take a DataGrid for example: it triggers its own validation (for example: when you start editing a property). But the DataGrid control doesn’t have any knowledge of WCF RIA Services (Jeff also mentions this in his posts: you can never assume your validator will have access to the Items collection!). So: when validation is triggered by the DataGrid, it will execute the validators without the custom ValidationContext – therefore, the Items collection is empty (the same applies to services you provide) in those cases.

 

Luckily, validation is also triggered in the property setters (amongst other places), which does provide the correct ValidationContext, ensuring our validator works as expected.

.NET, Silverlight, Headline, WCF RIA Services »

[18 Oct 2011 | 0 Comments]

imageA quick heads-up: my 2-part article series on authorized navigation in Silverlight has been published at SilverlightShow

 

The first article tackles authorized navigation: allowing or refusing access to a certain page/View in your Silverlight application depending on the role of the user.

The second article concerns authorizing access to certain UI elements IN a View (instead of the complete View), depending on the role of the logged in user, and deciding what to do when the user hasn’t got the correct role (eg: do you want to disable the element, or do you want to hide it completely?).


The complete source code is included in both articles.

 

Update: a third part has been released, focussing on securing your service calls.


Happy coding! :-)

.NET, Silverlight, WCF RIA Services, WCF, RIA »

[14 Jul 2011 | 0 Comments]

Just a quick heads-up: in case you haven’t noticed, Silverlight Show started publishing e-books a few weeks ago.  These provide a convenient way to read up on some of their most popular article series, including source code, while you’re offline.

 

I’m glad to announce that two of my article series are now available as e-books:

 

 

Happy coding! :-)

.NET, MVVM, RIA, Silverlight, WCF RIA Services »

[9 Jun 2011 | 0 Comments]

I’ve just finished my session on Silverlight, MVVM and RIA Services – an architectural story at NDC2011.  Almost a full room, thanks for such a great turnout!

 

riaservarch

 

As promised, the slides + democode are available for download.  Any questions on the session or the principles discussed therein?  Drop me a line or contact me on Twitter.

 

Cya around! :-)

.NET, Featured, RIA, Silverlight, Headline, WCF RIA Services »

[10 May 2011 | 0 Comments]

With the release of SP1 for WCF RIA Services, a bunch of new and/or enhanced collection types became available to developers: the EntitySet, EntityList, ICollectionView & DomainCollectionView. 

 

I wrote a round-up of these collection types for SilverlightShow, explaining how they can be used and for what scenarios they are best used.  The first part of this series has been published today, and the second part will follow shortly.

 

If you’re working with WCF RIA Services, knowledge of these new types is essential, as they offer a lot of productivity value for your applications.  Have a look at the series, and feel free to let me know how you feel about them!  Source code is, as usual, included.

 

Happy coding! :-)