Silverlight & XAML Playground
about XAML and other Amenities

A base class for threaded Application Services

2011-02-21T23:26:16+01:00 by Andrea Boschin

Continuing of my series about IApplicationServices I would like to propose a base class I've created to easily develop Application Services that runs a thread. There are lot of cases when you implement IApplicationService to run a parallel task that accomplish some kind of background tasks. I've collected all the redundant code inside a class I called ThreadedService. Extending this class you have a thread automatically started and stopped according with the application lifetime. Here is the class:

public abstract class ThreadedService : IDisposable, IApplicationService, IApplicationLifetimeAware
{
    /// <summary>
    /// Gets or sets the exit event.
    /// </summary>
    /// <value>The exit event.</value>
    private ManualResetEvent ExitEvent { get; set; }
 
    /// <summary>
    /// Gets or sets the wait handles.
    /// </summary>
    /// <value>The wait handles.</value>
    protected WaitHandle[] ExitHandles { get; set; }
 
    /// <summary>
    /// Initializes a new instance of the <see cref="ThreadedObject"/> class.
    /// </summary>
    public ThreadedService()
    {
        this.ExitEvent = new ManualResetEvent(false);
        this.ExitHandles = new WaitHandle[] { this.ExitEvent };
    }
 
    /// <summary>
    /// The main thread body
    /// </summary>
    protected abstract void ThreadProc();
 
    #region IDisposable Members
 
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public virtual void Dispose()
    {
        this.StopService();
    }
 
    #endregion
 
    #region IApplicationLifetimeAware
 
    public virtual void Starting()
    { }
 
    public virtual void Started()
    {
        ThreadStart listenThreadStart = new ThreadStart(ThreadProc);
        Thread thread = new Thread(listenThreadStart);
        thread.Start();
    }
 
    public virtual void Exiting()
    {
        this.ExitEvent.Set();
    }
 
    public virtual void Exited()
    { }
 
    #endregion
 
    #region IApplicationService
 
    public virtual void StartService(ApplicationServiceContext context)
    { }
 
    public virtual void StopService()
    { }
 
    #endregion
}

As already said the class is able to manage the background thread automatically but require a little level of collaboration when you develop the body of the thread. Infact after overriding the ThreadProc method you have to listen for the ExitEvent to be raised. When this event is set you have to exit from the service as soon as possible.

Saying we have to poll the newtork once every minute you can implement the ThreadProc the way I show it in the figure.

protected override void ThreadProc()
{
    this.Update();
 
    while (WaitHandle.WaitAny(this.ExitHandles, 5000) == WaitHandle.WaitTimeout)
        this.Update();
}

The procedure wait for the ExitEvent for a timeout. During this time the runtime is able to do something else but once the event is set the process is immediately exited. Something very simple but effective.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Inject ApplicationServices with MEF reloaded: supporting recomposition

2011-02-17T16:35:03+01:00 by Andrea Boschin

In my I've presented a simple solution that allows people to adds instances of ApplicationServices, without the need of adding the markup to the App.xaml file. The solution was interesting as a demonstration of how MEF works, but after some tests and some feedbacks I had I realized it is not applicable in a real world solution because of a subtle problem.

The problem is given by a conflict in the lifetime of the application between the Application_Start event and the StartService method. If you put the code to inizialize the CompositionContainer inside of the Application_Start event you may get an error stating that the composition has already initialized, just because when the StartService method call the CompositionInitializer method, to compose the services, the container is implicitly initialized. Unfortunately the Application_Start method is called after the StartService so here is the reason of the error you get.

For a long while I've thinked this may remain only a beautiful example because I do not discovered a valid solution, but today I convinced myself that a correction to this problem is possible and that it also add the ability of supporting recomposition to the HostService. As far as I know there is not any concern in calling the methods of the service with a delay respect the runtime raises the corrisponding method, almost until the method is not one of the Exit flavour. During the exit we have to free resources, stop threads, and so on and it is important to make these actions as soon as possible to let the runtime able to close the application faster. But the initialization phase may start in a later time, with the sole constraint that the sequence of the methods should be the same.

So this morning I've worked in this direction and in the following box you can see the resulting solution:

   1: public class HostService : IApplicationService, IApplicationLifetimeAware, IPartImportsSatisfiedNotification
   2: {
   3:     /// <summary>
   4:     /// Gets or sets the context.
   5:     /// </summary>
   6:     /// <value>The context.</value>
   7:     public ApplicationServiceContext Context { get; set; }
   8:     /// <summary>
   9:     /// Gets or sets the current.
  10:     /// </summary>
  11:     /// <value>The current.</value>
  12:     public static HostService Current { get; private set; }
  13:  
  14:     /// <summary>
  15:     /// Gets or sets the initialized.
  16:     /// </summary>
  17:     /// <value>The initialized.</value>
  18:     private List<IApplicationService> Initialized { get; set; }
  19:  
  20:     /// <summary>
  21:     /// Gets or sets the services.
  22:     /// </summary>
  23:     /// <value>The services.</value>
  24:     [ImportMany(AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.Shared)]
  25:     public List<IApplicationService> Services { get; set; }
  26:  
  27:     /// <summary>
  28:     /// Initializes a new instance of the <see cref="HostService"/> class.
  29:     /// </summary>
  30:     public HostService()
  31:     {
  32:         if (HostService.Current != null)
  33:             throw new InvalidOperationException("Service already exists");
  34:  
  35:         HostService.Current = this;
  36:         this.Initialized = new List<IApplicationService>();
  37:     }
  38:  
  39:     /// <summary>
  40:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  41:     /// </summary>
  42:     public void Exited()
  43:     {
  44:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  45:             service.Exited();
  46:     }
  47:  
  48:     /// <summary>
  49:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  50:     /// </summary>
  51:     public void Exiting()
  52:     {
  53:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  54:             service.Exiting();
  55:     }
  56:  
  57:     /// <summary>
  58:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  59:     /// </summary>
  60:     public void Started()
  61:     {
  62:         CompositionInitializer.SatisfyImports(this);
  63:     }
  64:  
  65:     /// <summary>
  66:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  67:     /// </summary>
  68:     public void Starting()
  69:     {
  70:     }
  71:  
  72:     /// <summary>
  73:     /// Called by an application in order to initialize the application extension service.
  74:     /// </summary>
  75:     /// <param name="context">Provides information about the application state.</param>
  76:     public void StartService(ApplicationServiceContext context)
  77:     {
  78:         this.Context = context;
  79:     }
  80:  
  81:     /// <summary>
  82:     /// Called by an application in order to stop the application extension service.
  83:     /// </summary>
  84:     public void StopService()
  85:     {
  86:         foreach (IApplicationService service in this.Services)
  87:             service.StopService();
  88:     }
  89:  
  90:     /// <summary>
  91:     /// Called when a part's imports have been satisfied and it is safe to use.
  92:     /// </summary>
  93:     public void OnImportsSatisfied()
  94:     {
  95:         // buffer for services that raises exception during initialization
  96:         List<IApplicationService> errors = new List<IApplicationService>();
  97:  
  98:         // call StartService
  99:         foreach (IApplicationService service in this.Services.OfType<IApplicationService>().Except(this.Initialized))
 100:         {
 101:             try
 102:             {
 103:                 service.StartService(this.Context);
 104:             }
 105:             catch
 106:             {
 107:                 errors.Add(service);
 108:             }
 109:         }
 110:  
 111:         // call Starting
 112:         foreach (IApplicationLifetimeAware service in 
 113:             this.Services.OfType<IApplicationLifetimeAware>()
 114:             .Except(this.Initialized.OfType<IApplicationLifetimeAware>())
 115:             .Except(errors.OfType<IApplicationLifetimeAware>()))
 116:         {
 117:             try
 118:             {
 119:                 service.Starting();
 120:             }
 121:             catch
 122:             {
 123:                 errors.Add((IApplicationService)service);
 124:             }
 125:         }
 126:  
 127:         // call Started
 128:         foreach (IApplicationLifetimeAware service in 
 129:             this.Services.OfType<IApplicationLifetimeAware>()
 130:             .Except(this.Initialized.OfType<IApplicationLifetimeAware>())
 131:             .Except(errors.OfType<IApplicationLifetimeAware>()))
 132:         {
 133:             try
 134:             {
 135:                 service.Started();
 136:             }
 137:             catch
 138:             {
 139:                 errors.Add((IApplicationService)service);
 140:             }
 141:         }
 142:  
 143:         // register ad Initialized to avoid duplicated initialization in case of recomposition
 144:         foreach (IApplicationService service in 
 145:             this.Services.OfType<IApplicationService>()
 146:             .Except(this.Initialized)
 147:             .Except(errors))
 148:             this.Initialized.Add(service);
 149:  
 150:         // remove services that raised exceptions
 151:         foreach (IApplicationService service in errors)
 152:             this.Services.Remove(service);
 153:     }
 154: }

As you can see the structure of the service remains the same, and also the management of the Exit, Exited and StopService is not changed. The very important change is in the Started method, that is called after the Application_Start event. In this method I only perform the composition. Then, in the OnImportsSatisfied method I start a sequence to emulate the startup of the service, repeating the flow that an ApplicationService usually follow during it initialization. I've also added some code to manage exceptions raised during the initialization. In these cases the services that raised the errors do not follow the remaining parts of the initialization and finally are removed from the Services collection.

In the service I maintain a collection of Initialized services because if the recomposition happen I must not repeat the inizialization of services that were already initialized previously. This is also the reason that make me asking a Shared CreationPolicy, because when the composition container compose the Services property does not have to create new instances but only have to add the new services coming from the recomposition.

I hope this solution to be more reliable than the previous one. As usual I will be glad to have feedback from you to continue improving the code..

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Singleton ApplicationServices explained

2011-02-17T09:44:39+01:00 by Andrea Boschin

My has opened to some unexpected questions about what may seems a strangeness in my code. I was surprised about this questions, just because I believed it was a known pattern, so I decided to give an explanation to ones that does not know the reason I wrote some lines of code in the constructor

   1: public class SampleService : IApplicationService
   2: {
   3:     public static SampleService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
   7:     /// </summary>
   8:     public SampleService()
   9:     {
  10:         if (SampleService.Current != null)
  11:             throw new InvalidOperationException("Service already exists");
  12:  
  13:         SampleService.Current = this;
  14:     }
  15:  
  16:     // omissis...
  17: }

The problem here is that sometimes it is not useful (and sometimes it is definitely bad) to have more than an instance of an ApplicationService running behind the scenes. Given that none can limit the number of instances in the ApplicationLifetimeObjects collection I use a pattern to avoid this problem.

First of all I expose a static property "Current". This property is useful to interact with the running service from any part of the code. Since after the beta there is not an indexer to access the running service this is the most common pattern used for this purpose.

Then in the constructor I check the property "Current" to be null. If I found a value it means someone is trying to put two instances of the service in the markup, so I raise an exception stating that only one instance is allowed. In the case "Current" is empty I save myself to this property using "this".

So nothing is wrong in my previous example. Only few lines of code to implement a Singleton pattern for IApplicationService, with a tecnique that is widely used also inside the framework classes.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Inject Application Services using MEF

2011-02-06T22:33:47+01:00 by codeblock

For an example I'm writing for my next presentation at the end of February, I’m trying to build an application skeleton that have lot of extensibility points and my goal is to make almost everything pluggable. An interesting thing I did today is to inject in the application a number of ApplicationServices. The application services are an useful tool to have application-wide services available during the entire lifetime, and they are very easy to create and install. First of all you have to implement the IApplicationService interface and if you want a more detailed control on application lifecycle you can also implement the IApplicationLifetimeAware interface. These interfaces give you a number of methods that notify when the application is started of stopped. Here is an example of a service and its installation:

   1: public class SampleService : IApplicationService
   2: {
   3:     public static SampleService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
   7:     /// </summary>
   8:     public SampleService()
   9:     {
  10:         if (SampleService.Current != null)
  11:             throw new InvalidOperationException("Service already exists");
  12:  
  13:         SampleService.Current = this;
  14:     }
  15:  
  16:     /// <summary>
  17:     /// Called by an application in order to initialize the application extension service.
  18:     /// </summary>
  19:     /// <param name="context">Provides information about the application state.</param>
  20:     public void StartService(ApplicationServiceContext context)
  21:     {
  22:         // initialize here the service
  23:     }
  24:  
  25:     /// <summary>
  26:     /// Called by an application in order to stop the application extension service.
  27:     /// </summary>
  28:     public void StopService()
  29:     {
  30:         // release resources here
  31:     }
  32: }
  33:  
  34: // -----------
  35:  
  36: <Application.ApplicationLifetimeObjects>
  37:     <local:SampleService />
  38: </Application.ApplicationLifetimeObjects>

Today I've asked myself how can I inject an application service with MEF. If I have many modules I plug to my skeleton, let say they are customization for different customers, I suppose I can also have one or more application services injected by these modules for the purpose of the customization. To solve this pluggability problem I’ve created a simple HostService that implement both IApplicationService and IApplicationLifetimeAware, then I added a property, imported with MEF, that is a collection of IApplicationService. So when I load the ApplicationService in the App.xaml the constructor perform the Composition and I have a bunch of services loaded into the host service.

   1: public class HostService : IApplicationService, IApplicationLifetimeAware
   2: {
   3:     public static HostService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Gets or sets the services.
   7:     /// </summary>
   8:     /// <value>The services.</value>
   9:     [ImportMany]
  10:     public IEnumerable<IApplicationService> Services { get; set; }
  11:  
  12:     /// <summary>
  13:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
  14:     /// </summary>
  15:     public HostService()
  16:     {
  17:         if (HostService.Current != null)
  18:             throw new InvalidOperationException("Service already exists");
  19:  
  20:         HostService.Current = this;
  21:         CompositionInitializer.SatisfyImports(this);
  22:     }
  23:  
  24:     /// <summary>
  25:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  26:     /// </summary>
  27:     public void Exited()
  28:     {
  29:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  30:             service.Exited();
  31:     }
  32:  
  33:     /// <summary>
  34:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  35:     /// </summary>
  36:     public void Exiting()
  37:     {
  38:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  39:             service.Exiting();
  40:     }
  41:  
  42:     /// <summary>
  43:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  44:     /// </summary>
  45:     public void Started()
  46:     {
  47:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  48:             service.Started();
  49:     }
  50:  
  51:     /// <summary>
  52:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  53:     /// </summary>
  54:     public void Starting()
  55:     {
  56:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  57:             service.Starting();
  58:     }
  59:  
  60:     /// <summary>
  61:     /// Called by an application in order to initialize the application extension service.
  62:     /// </summary>
  63:     /// <param name="context">Provides information about the application state.</param>
  64:     public void StartService(ApplicationServiceContext context)
  65:     {
  66:         foreach (IApplicationService service in this.Services)
  67:             service.StartService(context);
  68:     }
  69:  
  70:     /// <summary>
  71:     /// Called by an application in order to stop the application extension service.
  72:     /// </summary>
  73:     public void StopService()
  74:     {
  75:         foreach (IApplicationService service in this.Services)
  76:             service.StopService();
  77:     }
  78: }

As you can see in the code, for every method exposed by the interfaces I make a loop and call the corresponding method on each service. The services have to be exported ad IApplicationService, but if they implements also the other interface the host call the relative methods. To export a service you can simple decorate it with an Export attribute.

   1: [Export(typeof(IApplicationService))]
   2: public SampleService : IApplicationService
   3: {
   4:     // implement the service
   5: }

Once installed, this very simple service let you easily inject new services without the need of recompile the software. Simply add the assembly where it resides to the catalog and the services will be installed seamless.

Magic…

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Remove Transformation from shapes in Blend

2011-01-24T13:49:54+01:00 by Andrea Boschin

Recently I'm working hard with Expression Blend to create some interfaces starting from some Illustrator mockups, made by a designer and also creating the user interface of some Windows Phone 7 applications I developed. One thing I've found annoying is that once you create a shape and you rotate it in the designer, Blend uses a CompositeTransform to render the shape in the XAML markup. If you are creating lot of elements (in my case they were the ticks of a gauge) you can get lot of transformations that the plugin or the phone have to render. And to me (particularly from my developer side), this is no a good thing since many times a rotated rectangle might be represented as a Path instead of using the CompositeTransform.

After realizing that creating the shape by hand is not an option I searched hard the options in the menu items because in my mind it was obvious to have a "Make transform static" option. But it simply does not exists (or at least I was unable to find it). So for a long time I decided to close my eyes and accept the Transformations. This until I found a tricky way to convince Blend to perform this operation as a side effect of a sequence of common operations. Here is the process I found:

First of all create the Shape. In this example I will use a Rectangle:

Capture1

Then I apply the Transformation I need. For this example I simply rotate the shape with the mouse of an amout of degrees:

Capture2

Now select the shape and find the option in the context menu (or in the Objects Tree) to make the shape a Path (Path -> Convert To Path). Obviously, to remove the transformation, you have to make the rectangle a Path. Once you have done this you will have this in the Object tree:

Capture3

As now Blend have performed the conversion but the CompositeTrasform is still in place. To convince Blend we have another couple of steps. Now Draw another shape (a rectangle) over the shape to convert. See in figure the result:

Capture4

Then Convert also this Rectangle to a Path. After this step the Objects Tree will show two paths one on top of the other. In the figure I've named the upper Shape to "Mask":

Capture5

Finally, and it is the tricky thing you have to deselect both the Paths (e.g. selecting the LayoutRoot element) then select again them starting from "Mask" then (using CTRL) the shape to convert. It is important to be aware that in the conversion the order matter. If you select the shapes in the inverse order you will not reach the required result. Then finally from the context menu select Combine -> Intersect. The resulting shape is this:

Capture6

The type of selection makes evident that now the Shape ha no rotation but we can see at the markup and the result is reported here:

   1: <Grid x:Name="LayoutRoot" Background="White">
   2:     <Path x:Name="Mask" 
   3:         Data="M29.362633,1.5 L101.14476,29.362633 L73.282127,101.14476 L1.5,73.282127 z" 
   4:         Fill="#FFFFD200" 
   5:         Margin="228.678,148.678,308.678,228.678" 
   6:         Stretch="Fill" Stroke="Black" StrokeThickness="3" UseLayoutRounding="False"/>
   7: </Grid>

Now you are ready to put the shape wherever you need it. I've experimented the trick in many ways and until today I never found a case where it does not works. The sole things you have to keep in mind is the right order of the steps, and that you can convert only a single shape at a time. So for my Gauge I had to make the conversion many times.

As a suggestion I think it would be beautiful to have an additional menu item that does the trick automatically... If someone knows a better way please feel free to write me and I will be very grateful.

Technorati Tags: ,,
Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Simplify localization with a T4 template

2010-11-25T23:07:04+01:00 by Andrea Boschin

If you evere tryied to localize a Silverlight application, you have found for sure that it is not a straighforward task. Like all the other .NET tecnologies the Silverlight projects has resource files and you can create satellite assemblies to contains culture specific strings. But the problem is that it is not so easy to bring this string to the interface.

When you create a resx file Visual Studio generates a code file with a class ready to be used to read the strings. You can also choose to make this class public, so it is possible to use it across different projects. Unfortunately this class has an internal constructon so there is no way to add an instance to the App.xaml resources and use DataBinding to reference the strings in the user interface.

Someone suggest to manually change the ctor of the class but to me it is very frustrating to correct this simple bug every time I add a new resource. So it is required to create a proxy class, that wraps the static one. Every property will call the correspondent static property and then the proxy class will be used for the DataBinding. When you are developing a simple application this is a simple task, but what about when you have an huge number of strings?

With this in mind I've worked to a T4 template that it is able to read a resx file and automatically produces the proxy class. If you do not know it, the T4 template is a very powerful tool that exists since Visual Studio 2008. With it you can write code to generate code inside Visual Studio, and it is exactly what I'm searching for.

Here is the template:

   1: <#@ template debug="false" hostspecific="true" language="C#" #>
   2: <#@ output extension=".cs"  encoding="UTF8" #>
   3: <#@ assembly name="System.Core" #>
   4: <#@ assembly name="System.Xml" #>
   5: <#@ assembly name="System.Xml.Linq" #>
   6: <#@ import namespace="System.IO" #>
   7: <#@ import namespace="System.Text.RegularExpressions" #>
   8: <#@ import namespace="System.Linq" #>
   9: <#@ import namespace="System.Xml" #>
  10: <#@ import namespace="System.Xml.Linq" #>
  11: //------------------------------------------------------------------------------
  12: // <auto-generated>
  13: //     This code was generated by a tool.
  14: //
  15: //     Changes to this file may cause incorrect behavior and will be lost if
  16: //     the code is regenerated.
  17: // </auto-generated>
  18: //------------------------------------------------------------------------------
  19:  
  20: <#
  21: string appName = "ResXProxy Generator Template";
  22: string version = "1.0.3977.0";
  23: string ns = (string)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");
  24: string resxFileName = Path.ChangeExtension(Host.TemplateFile, ".resx");
  25: string resxClassName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
  26: string proxyClassName = string.Format("{0}ResourceProxy", resxClassName);
  27: XDocument document = XDocument.Parse(File.ReadAllText(resxFileName));
  28: #>
  29: namespace <#=ns#>
  30: {
  31:     using System.Globalization;
  32:     using System.Windows.Markup;
  33:  
  34:     /// <summary>
  35:     /// Represent a proxy class for "<#= resxClassName #>" resources
  36:     /// </summary>
  37:     [System.Diagnostics.DebuggerStepThroughAttribute()]
  38:     [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  39:     public class <#= proxyClassName #>
  40:     {
  41:         /// <summary>
  42:         /// Initializes the "<#= proxyClassName #>" class
  43:         /// </summary>
  44:         public <#= proxyClassName #>()
  45:         {}
  46:     
  47:         /// <summary>
  48:         /// Gets the current culture
  49:         /// </summary>
  50:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  51:         public CultureInfo CurrentCulture 
  52:         { 
  53:             get { return CultureInfo.CurrentCulture; } 
  54:         }
  55:  
  56:         /// <summary>
  57:         /// Gets the current UI Culture
  58:         /// </summary>
  59:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  60:         public CultureInfo CurrentUICulture 
  61:         {
  62:             get { return CultureInfo.CurrentUICulture; } 
  63:         }
  64:         
  65:         /// <summary>
  66:         /// Gets the current Xml Language property
  67:         /// </summary>
  68:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  69:         public XmlLanguage Language 
  70:         { 
  71:             get { return XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.Name); } 
  72:         }
  73: <# foreach(var item in document.Element("root").Elements("data")) 
  74:    { 
  75:         string name = EscapeName(item);
  76:     
  77:         if (item.Attributes("type").Count() == 0)
  78:         {
  79: #>
  80:  
  81:         /// <summary>
  82: <# if (item.Elements("comment").Count() == 1) { #>
  83:         /// <remarks><#= item.Element("comment").Value #></remarks>
  84: <# } #>
  85:         /// Gets the "<#= name #>" Property
  86:         /// </summary>
  87:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  88:         public string <#= name #> 
  89:         { 
  90:             get { return <#= resxClassName + "." + name #>; } 
  91:         }
  92: <# 
  93:         }
  94: }
  95: #>
  96:     }
  97: }<#+
  98: public string EscapeName(XElement item)
  99: {
 100:     string name = item.Attribute("name").Value;
 101:     return Regex.Replace(name, "[^a-zA-Z0-9_]{1,1}", "_");
 102: }
 103: #>

Using the template is really easy. First of all you have to configure the Silverlight application to be localizable ( you can find how to do)  and then create the required resx files. Finally you have to add the template using the same name of the main resource file but using the .tt extension. E.g. if your resx is called AppString.resx you have to call the template AppStrings.tt. Nothing else. Once you have added the template it automatically generates a class called AppStringResourceProxy you can easily reference from a XAML file and bind to the UI. Remember that every time you change the resx file you have to select "Run custom tool" from the context menu of the T4 template to generate the code again.

The generated class does not contains only the string properties but also expose a Language property. You can bind this property to the corresponding Language property of the page (the UserControl) and override the default culture that is usually set to "en-US". Doing this you will have also the Dates and numeric values converted to the right culture.

   1: <UserControl Language="{Binding Language, Source={StaticResource AppString}}" />

The tamplate works fine for Silverlight and Windows Phone. I've never tryied but I think it works also with WPF. Hope this helps.

Download Sample: Silverlightplayground.Localization.zip (96,3 KB)

WP7 Fast Tips: Load (decode) and Save (encode) Jpeg files

2010-10-15T11:40:05+01:00 by Andrea Boschin

Notice: I've added the new category "WP7 Fast Tips" to collect fast tips I found while developing my Windows Phone Applications. You Don't expect only great tips in this category but also simple things I need fo remember or I found with some difficulties :)


Working on an application I hope to publish soon on the marketplace, I have the need to save an image from a WriteableBitmap instance into a Jpeg file. It was not an easy search but finally I found a couple of methods, added as Extension Methods to the WriteableBitmap class itself. If you need to encode a jpeg to a byte array you can use this code:

   1: public byte[] EncodeToJpeg(WriteableBitmap wb)
   2: {
   3:     using(MemoryStream stream = new MemoryStream())
   4:     {
   5:         // this is Extensions.SaveJpeg() method
   6:         wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 85);
   7:         return stream.ToArray();
   8:     }
   9: }

The parameters of the SaveJpeg method require a stream to write to, the size of the bitmap to generate and the quality from 0 to 100. The fourth parameter, called orientation, currently is not used and you can set it to 0 (zero).

The counterpart of the SaveJpeg methos is LoadJpeg that you can use to take a Jpeg file and load it into a WriteableBitmap instance. A very useful feature. If you need to write the Jpeg to the MediaLibrary probably the better is to pass the generated stream directly to the Save method.

Tags:  
Categories:   TIPS | Windows Phone 7
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

A generic enum converter to map values...

2010-09-03T16:38:49+01:00 by Andrea Boschin

Many times I have to deal with enumerated values with databinding, and often this requires  the writing of a converter to map the enum to something else. As an example you can have a Status enum and you need to transform it to a color (or a brush) in the UI, or you have a series of values from the enumerator and need to change it to an image when it is presented to the user.

Finally these days I found a generic solution to these problems that let me create a mapping from a value (an enumerator, a series of integers, of also a boolean) to an instance of another object. To give you an example think at having this enumerator:

   1: public enum MessageType
   2: {
   3:     Normal,
   4:     Urgent,
   5:     Critical
   6: }

We want to map the values to a series of colors having White for Normal, Orange for Urgent and Red for Critical. Since usually you can override IValueConverter and write a specific converter that handles this enum I found useful to create a parametric value converter that lets me write the code below:

   1: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   2:              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   3:              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   4:              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   5:              xmlns:cv="clr-namespace:SilverlightPlayground.Converters;assembly=SilverlightPlayground"
   6:              mc:Ignorable="d" x:Class="SilverlightPlayground.MyApplication.Shell" d:DesignWidth="456">
   7:     <UserControl.Resources>
   8:         <cv:EnumConverter x:Key="mapTypeToBrush">
   9:             <cv:EnumConverter.Items>
  10:                 <SolidColorBrush Color="#00000000" />
  11:                 <SolidColorBrush Color="#FFFF9900" />
  12:                 <SolidColorBrush Color="#FFFF0000" />
  13:             </cv:EnumConverter.Items>
  14:         </cv:EnumConverter>
  15:     </UserControl.Resources>
  16:     <Grid x:Name="LayoutRoot">
  17:         <Rectangle Fill="{Binding ReveivedMessageType, Converter={StaticResource mapTypeToBrush}}" Width="100" Height="100" />
  18:     </Grid>
  19: </UserControl>

The beautiful of this converter is that with a single converter you can create a series of mappings in the App.xaml resources and share them across all the applications and centralize the conversion of application wide enums in a single position. The code of the converter is really simple and straightforward:

   1: public class EnumConverter : IValueConverter
   2: {
   3:     private List<object> items;
   4:  
   5:     public List<object> Items 
   6:     { 
   7:         get
   8:         {
   9:             if (items == null)
  10:                 items = new List<object>();
  11:  
  12:             return items;
  13:         }
  14:     }
  15:  
  16:     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  17:     {
  18:         if (value == null)
  19:             throw new ArgumentNullException("value");
  20:         else if (value is bool)
  21:             return this.Items.ElementAtOrDefault(System.Convert.ToByte(value));
  22:         else if (value is byte)
  23:             return this.Items.ElementAtOrDefault(System.Convert.ToByte(value));
  24:         else if (value is short)
  25:             return this.Items.ElementAtOrDefault(System.Convert.ToInt16(value));
  26:         else if (value is int)
  27:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  28:         else if (value is long)
  29:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  30:         else if (value is Enum)
  31:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  32:  
  33:         throw new InvalidOperationException(string.Format("Invalid input value of type '{0}'", value.GetType()));
  34:     }
  35:  
  36:     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  37:     {
  38:         if (value == null)
  39:             throw new ArgumentNullException("value");
  40:  
  41:         return this.Items.Where(b => b.Equals(value)).Select((a, b) => b);
  42:     }
  43: }

The converter has a list of items it use to make the conversion of integer values. It maps the integer to the index of the list and returns the n-th element found in the array. I didn't handled the out of bounds exception just because the errore returned to the user by the converter is already good to understand what it happen. The converter itself can handle both the directions if the Equals method is correctly overrided.

Finally, I  supported boolean values so it is possible to pass to the converter true/false and having it select the items from a list of two elements. This let me also map the Visibility to boolean values (that is a pretty frequent situation):

   1: <cv:EnumConverter x:Key="bool2Visibility">
   2:     <cv:EnumConverter.Items>
   3:         <Visibility>Collapsed</Visibility>
   4:         <Visibility>Visible</Visibility>
   5:     </cv:EnumConverter.Items>
   6: </cv:EnumConverter>

As you can see the trick is really simple but as usual simple things are the most beautiful and useful. I hope someone can judge this tip good to spare time. It has been very great for me.

FormsAuthentication and ClientHttpStack with mixed applications

2010-08-30T16:41:47+01:00 by Andrea Boschin

If you ever tried to have mixed applications, with some regular ASP.NET pages and a bunch of Silverlight applications, where the forms authentication is made with a traditional ASP.NET login page, you have for sure found that you are forced to use the BrowserHttp stack into the Silverlight applications because the FormsAuthentication cookie is not shared between the ASP.NET page and the new ClientHttp stack that is available since Silverlight 3.0.

Since the new ClientHttp stack for many reasons is a huge improvement, is would be a beautiful thing being able to use this stack every time there is the need to place HTTP calls. Finally I found a way to share this cookie across the two stacks, but I want to warn you that the sole way to accomplish this task require a little "dirty hack", that someone might consider unacceptable while it opens some minor security concerns I will detail at the end of the post.

Before continue reading this post please have in mind that my suggestion is to always use the BrowserHttp stack in these scenario, until you do not really need the new stack, e.g. if you have to use some special verb or for some other particular reason.

The dirty hack explained

The first thing you have to know is that when you are using the ClientHttp stack you are free to access the cookies issued by the server and it is also possible to add some cookies to attach to a call. The task we would have to accomplish is simply to read a cookie (often called .ASPXAUTH) from the BrowserHttp stack and add it to the ClientHttp stack and it may seems and easy task.

Unfortunately the BrowserHttp Stack prevent us from accessing the cookies. The cookies management is one of the features that has been added by the new ClientHttp stack and there is not any way to pump out them from the old one.

So my first try was to use the HtmlPage.Document.Cookies property, as I found in some examples on the Internet. Perhaps this may work when the authentication cookie is issued by a custom authentication system, but with FormsAuthentication it is another failed try because the cookies issued by the ASP.NET engine are HttpOnly. This attribute, added since IE6 SP1, prevents the browser scripts - and Silverlight is considered a client script - from accessing these cookies that remain hidden.

So, how can we share these cookies? The only thing you can do is to add the cookie to the InitParams collection when you initialize the plugin in the html page. You have to change this page to be an aspx page and then you can create two keys into the InitParams collection like the following:

   1: public string GetFormsAuthenticationCookie()
   2: {
   3:     return string.Format(
   4:         "FormsCookieName={0},FormsCookieValue={1}",
   5:         FormsAuthentication.FormsCookieName,
   6:         this.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
   7: }

Then in the plugin I would like to have a centralized class reponsible of creating the cookie and adding it to each server call I do. First of all, if you are using WCF you have to enable the CookieContainer from the ServiceReferences.ClientConfig file. You have to add an attribute that exists only in the Siverlight side of the configuration:

   1: <basicHttpBinding>
   2:     <binding name="BasicHttpBinding_IWCFService" 
   3:               maxBufferSize="2147483647"
   4:               enableHttpCookieContainer="True"
   5:               maxReceivedMessageSize="2147483647">
   6:         <security mode="None" />
   7:     </binding>
   8: </basicHttpBinding>

The enableCookieContainer attribute initializes an instance of an IHttpCookieContainerManager into the WCF client an this allow us to manage cookies using the CookieContainer collection. Now we can write a simple application service to put into the app.xaml that we can use to instantiate and initialize the client. The reason of creating an application service is to have a common place where initialize a shared cookie container that reads the InitParam and add the received cookies into it:

   1: public class ClientCreatorService : IApplicationService
   2: {
   3:     public static ClientCreatorService Current
   4:     {
   5:         get
   6:         {
   7:             return Application.Current.ApplicationLifetimeObjects.OfType<ClientCreatorService>().Single();
   8:         }
   9:     }
  10:  
  11:     private CookieContainer SharedCookies { get; set; }
  12:  
  13:     public void StartService(ApplicationServiceContext context)
  14:     {
  15:         this.SharedCookies = new CookieContainer();
  16:         string formsCookieName = context.ApplicationInitParams["FormsCookieName"];
  17:         string formsCookieValue = context.ApplicationInitParams["FormsCookieValue"];
  18:         this.SharedCookies.Add(new Uri(Application.Current.Host.Source, "../"), new Cookie(formsCookieName, formsCookieValue));
  19:     }
  20:  
  21:     public K CreateClient<T, K>()
  22:         where K : ClientBase<T>, new()
  23:         where T : class
  24:     {
  25:         K client = new K();
  26:         IHttpCookieContainerManager cookieManager = client.InnerChannel.GetProperty<IHttpCookieContainerManager>();
  27:         cookieManager.CookieContainer = this.SharedCookies;
  28:         return client;
  29:     }
  30:  
  31:     public void StopService()
  32:     {}
  33: }

During the initialization the ClientCreatorService initialize the SharedCookies with the FormsCookieName and FormsCookieValue. Then the generic CreateClient methid is able to instantiate the required client and add the SharedCookies to it. The usage of the Application Service is pretty simple. Here is how to instantiate the client and place a call:

   1: WCFServiceClient client = ClientCreatorService.Current.CreateClient<IWCFService, WCFServiceClient>();
   2: client.GetIdentityCompleted += new EventHandler<GetIdentityCompletedEventArgs>(client_GetIdentityCompleted);
   3: client.GetIdentityAsync();

If you try the code you will find that the GetIdentity method is able to retrive the membership identity from the HttpContext.Current.User property. This means your service runs in an authenticated context.

What is the drawback?

When you make a dirty thing you have to be aware that there is always almost a drawback. In this case the problem is that the injection of the authentication cookie in the InitParams opens a subtle security concern that is mitigated by the fact that the informations we are disclosing are already public even before my trick was up and running. The use of HttpOnly cookies for authentication is an important improvement that prevent the use of XSS attacks. Having the cookie injected in the page, re-open the window to some pre-IE6 SP1 scripts that could try to access the InitParams collection from javascript and simulate the authentication to the system like our Silverlight application actually does.

This problem must be always in your mind and you have to put on the scale both the advantage of having the ClientHttp working and the security concerns of this trick. My hope is that the Silverlight team will be able to solve this issue in next releases of the plugin, so we can rely on a secure implementation and avoid dirty tricks.

Categories:   Networking | TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Keeping an ObservableCollection sorted with a method override

2010-04-27T16:30:52+01:00 by Andrea Boschin

Usually the order of elements in a collection is something one does not feel to be important. We have plenty of methods in the Framework to order things and now that there is LINQ to Objects the need to sort something is really matter of seconds.

Since the introduction of LINQ the main problem is that you have many cases where a few lines of code are adding really complex iteration logic and often this hurts performances when the count of milliseconds is something important.

In these days I had this problem because I did need to make a lot of updates to a collection in the short time as possible, because I was working into the a CompositionTarget_Rendering event. A lot of hidden iterations through the collection, made by LINQ queries, has degraded the performances of the application and I had to put my nose on the iteration logic to optimize it.

In a case like this, having a collection that ensure a given order of the elements was key to solve my problem. So I decided to implement a SortedObservableCollection that helped me to minimize scans of the collection. Here is the simple code:

   1: public class SortedObservableCollection<T> : ObservableCollection<T>
   2:     where T : IComparable
   3: {
   4:     protected override void InsertItem(int index, T item)
   5:     {
   6:         for (int i = 0; i < this.Count; i++)
   7:         {
   8:             switch (Math.Sign(this[i].CompareTo(item)))
   9:             {
  10:                 case 0:
  11:                     throw new InvalidOperationException("Cannot insert duplicated items");
  12:                 case 1:
  13:                     base.InsertItem(i, item);
  14:                     return;
  15:                 case -1:
  16:                     break;
  17:             }
  18:         }
  19:  
  20:         base.InsertItem(this.Count, item);
  21:     }
  22: }

The trick has been done in a few minutes, just because the ObservableCollection exposes some overridable methods that enable the developer to change the Insert and Remove behavior. So I simply overriden the InsertItem method. I changed it trying to put elements in the right place, evaluating the position over the elements already in the collection. This helped me to move the sorting logic in another context, out of the Rendering event, and maximize the performances.

The code I wrote in this sample is really simple. I figure out you can improve again the performances of the InsertItem method implementing a binary search algorithm. But it is a little concern when you know where to act.

A generic ViewModel to publish sets of objects

2009-12-16T22:14:18+01:00 by Andrea Boschin

The programming with the MVVM pattern often requires a proliferation of types because there are more Views that need a ViewModel than you might think. You can think that you need to associate a ViewModel only to simple blocks of your user interface, but while you are writing code you often understand that there are other cases where a ViewModel is required.

As an example while populating a DataGrid binding simple entities to the grid is not  good idea. A single row of a grid not only contains some data to be displayed but often may contain some commands, a link to be clicked, a button to delete an item and so on.

All these are cases where you need a ViewModel to correcty handle the binding and the related commands. So you have to create a new type, add a bunch of properties and some commands and finally populate the grid with instances of the new type. Then you will find someway difficult to route the command from the child ViewModel to the one that has generated it.

Simple is better.

In my recent applications I found a reasonable workaround that correctly handle the situation but avoid a proliferation of classes and as usual in this cases the solution comes from a generic type. The trick comes from the understanding that often in this scenario you have some properties of an entity to be binded and one or two commands to be raised. And in the most cases the command have to be handled by a parent ViewModel. Imagine to have a collection which is shown in a DataGrid and a delete command on each row; To remove the row, the command has to be catched by the ViewModel which is exposing the collection. So I decided to create a simple class named RowViewModel this way:

   1: public class RowViewModel<T,K> : ViewModel
   2:     where T : ViewModel
   3: {
   4:     /// <summary>
   5:     /// Initializes a new instance of the RowViewModel class.
   6:     /// </summary>
   7:     /// <param name="parent">The parent.</param>
   8:     /// <param name="payload">The payload.</param>
   9:     public RowViewModel(T parent, K payload)
  10:     {
  11:         this.Parent = parent;
  12:         this.PayLoad = payload;
  13:     }
  14:  
  15:     /// <summary>
  16:     /// Gets or sets the parent.
  17:     /// </summary>
  18:     /// <value>The parent.</value>
  19:     public T Parent
  20:     {
  21:         get { return this.GetValue<T>("Parent"); }
  22:         set { this.SetValue<T>("Parent", value); }
  23:     }
  24:  
  25:     /// <summary>
  26:     /// Gets or sets the pay load.
  27:     /// </summary>
  28:     /// <value>The pay load.</value>
  29:     public K PayLoad
  30:     {
  31:         get { return this.GetValue<K>("PayLoad"); }
  32:         set { this.SetValue<K>("PayLoad", value); }
  33:     }
  34: }

To simplify the creation of the collection property I've created a class RowViewModelCollection that inherits from the ObservableCollection. This allow me to declare the observable properties with a less verbose syntax:

   1: public class RowViewModelCollection<T, K> : ObservableCollection<RowViewModel<T, K>>
   2:     where T : ViewModel
   3: { }

Now we are ready to publish a DataGrid using this class. The RowViewModel exposes T as the parent ViewModel and K as the payload. This is the entity we want to display in the grid. So we can write a XAML similar to this:

   1: <data:DataGrid ItemsSource="{Binding Tasks}">
   2:     <data:DataGrid.Columns>
   3:         <data:DataGridTemplateColumn>
   4:             <data:DataGridTemplateColumn.CellTemplate>
   5:                 <DataTemplate>
   6:                     <HyperlinkButton cmd:Click.Command="{Binding Parent.DeleteTaskCommand}"
   7:                                      cmd:Click.CommandParameter="{Binding PayLoad}"
   8:                                      HorizontalAlignment="Center" VerticalAlignment="Center" >
   9:                         <Image Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" Width="16" Source="/Elite.TimeTracker.Client;Component/Images/delete.png" Stretch="Fill"/>
  10:                     </HyperlinkButton>
  11:                 </DataTemplate>
  12:             </data:DataGridTemplateColumn.CellTemplate>
  13:         </data:DataGridTemplateColumn>
  14:         <data:DataGridTextColumn Header="Cliente" Binding="{Binding PayLoad.Customer.Description}" />
  15:         <data:DataGridTextColumn Header="Ora" Binding="{Binding PayLoad.StartTime}" />
  16:         <data:DataGridTextColumn Header="Descrizione" Binding="{Binding PayLoad.Description, Converter={StaticResource ellipsis}}" />
  17:         <data:DataGridCheckBoxColumn Header="Chiam." Binding="{Binding PayLoad.IsPhoneCall}" />
  18:         <data:DataGridCheckBoxColumn Header="Compl." Binding="{Binding PayLoad.IsCompleted}" />
  19:     </data:DataGrid.Columns>
  20: </data:DataGrid>

In a couple of words we have:

ItemsSource="{Binding Tasks}" - This connect the DataGrid to a RowViewModelCollection declared in the parent ViewModel.

Binding="{Binding PayLoad.StartTime}" - This take advantage of the dot syntax of the DataBinding that allow to access complex properties of the binded objects. Remember that PayLoad is the instance of the entity to bind to the DataGrid so PayLoad.StartTime binds the StartTime property to the cell.

cmd:Click.Command="{Binding Parent.DeleteTaskCommand}" - This binds the click event of the HyperlinkButton to a command of the parent ViewModel.

cmd:Click.CommandParameter="{Binding PayLoad}" - This binding let me pass the entire entity the delegate command.

This tecnique simplifies the code required to handle the DataGrid. When a command is raised the parent ViewModel handle it and this reduce the need to use an EventBroker, and it gives a more simple and maintainable code.

Is this good only for DataGrids?

There are a lot of cases where you have to bind a collection to a control, the ItemsControl, the ListBox, and so on. Every time you encounter one of these cases you can obviously apply this tecnique. Of course it is useful only if you need to handle some commands, otherwise you can simply bind the entity directly to the control.
In the code I've attached to a my previous post - the -you can find a working sample of the workaround. 

Categories:   Databinding | TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

How to configure an AuthenticationDomainService using Ria Services Class Library

2009-11-06T11:36:01+01:00 by Andrea Boschin

Using a Ria Services Class library to collect DomainServices and DomainContexts in a solution is a good practice because everyone know the value of having classes separated in projects instead of having them in the web project.

If you try to create this kind of project and then you add an Authentication Domain Service with the July 2009 CTP of Ria Services you will find two major drawbacks. First of all you will notice the generated code does not contains a RiaContext class. RiaContext is the class responsible to handle the User and roles and configure the type of Authentication to use in the application.

To workaround to this problem you may simply write by hand this class and put it in the client side part of the Ria Services library project. The code can be copied by an AuthenticationDomainContext generated in the web project but it is very simple and I show you the code here:

   1: public sealed partial class RiaContext : System.Windows.Ria.RiaContextBase
   2: {
   3:     #region Extensibility Method Definitions
   4:  
   5:     partial void OnCreated();
   6:  
   7:     #endregion
   8:  
   9:     public RiaContext()
  10:     {
  11:         this.OnCreated();
  12:     }
  13:  
  14:     public new static RiaContext Current
  15:     {
  16:         get
  17:         {
  18:             return ((RiaContext)(System.Windows.Ria.RiaContextBase.Current));
  19:         }
  20:     }
  21:  
  22:     public new User User
  23:     {
  24:         get
  25:         {
  26:             return ((User)(base.User));
  27:         }
  28:     }
  29: }

With this class you can configure the Authentication Domain Context as you did with the generated class. So you have to add it to the App.config, in the ApplicationLifetimeObjects tag and then add a kind of authentication like the WindowsAuthentication and FormsAuthentication.

The second drawback is that if you run the project this way and then you call the Login() method (or any other method) of the RiaContext you will get an error:

The DomainContextType is null or invalid and there are no contexts generated from AuthenticationBase<T>

This probably come from the fact that the RiaContext is not correctly initializated so when the class search for a valid domaincontext to handle the Login call it fails because it does not find it. The tip to correct this behavior is to specify the DomainContext property in the Authentication you have choosed. Here is how to change the App.xaml file:

   1: <Application.ApplicationLifetimeObjects>
   2:     <slpgria:RiaContext>
   3:         <slpgria:RiaContext.Authentication>
   4:             <appsvc:FormsAuthentication>
   5:                 <appsvc:FormsAuthentication.DomainContext>
   6:                     <slpgdc:MyAuthenticationDomainContext />
   7:                 </appsvc:FormsAuthentication.DomainContext>
   8:             </appsvc:FormsAuthentication>
   9:         </slpgria:RiaContext.Authentication>
  10:     </slpgria:RiaContext>
  11: </Application.ApplicationLifetimeObjects>

As you can see you have simply to give an instance of the generated Authentication Domain context class to the FormsAuthentication (or Windows Authentication as well). The RiaContext need to have this instance to make the calls to the BL methods in the Domain Service.

I really do not know if this behavior will be corrected in the next releases of RiaServices. The solution is pretty simple but take me some hours to understand what are going wrong. So I hope this may help you to spare some time making your experiments with the current release.

Categories:   TIPS | Networking
Actions:   E-mail | del.icio.us | Permalink | Comments (7) | Comment RSSRSS comment feed