Category Archives: Uncategorized

How to install CSLA 4.5 and run ProjectTracker sample

This article shows you how to download and install CSLA and compile and run the ProjectTracker sample.

Download and Install

Go to http://www.cslanet.com/Download.html and download the latest installer.

Run the installer with default settings. Take note of the install folder and open this folder in Windows Explorer.

In this folder you will find CSLA.zip.

Right click on file and select “Extract all” and extract to f.ex C:\Projects\CSLA .NET\<versionnumber>\Csla.
I downloaded version 4.5.491 so my path is C:\Projects\CSLA .NET\4.5.491\Csla)

ProjectTracker

Open the Samples\Net\cs\ProjectTracker\ProjectTracker.sln solution file in Visual Studio 2013. (you may get upgrade warnings).

Build the solution. This status line should now “Build Succeeded”.

Then open the solution property page and set startup projects:

image

You must set multiple startup projects and always select WcfAppServer plus the UI project(s) of your choice.
The WcfAppServer hosts the data access (N-Tier) deployment is always required for all UI projects.

I selected the WfUI (Windows Forms) and then hit run.
In the login dialog use manager/manager  or admin/admin and you are logged in to ProjectTracker.

Next change the startup project to WpfUI + WcfAppServer and you will be running the WPF sample.

So that’s all, folks. Enjoy and explore CSLA.

How to: C# Connect to dynamic event

In our WCF services we have used the DbProviderFactories and the default interface so that our WCF services has no direct reference to the actual database provider or version. Recently we needed to connect to the InfoMessage event on the TeraData database provider without knowing the actual type. We wanted to still remain on the path with no reference to the driver and this was not straight forward. With the inspiration fro0m http://stackoverflow.com/questions/45779/c-sharp-dynamic-event-subscription we adapted a solution with Expression trees and in case you need to dynamically hook into an event, here is how we did it:

 1: namespace DynamicEvent
 2: {
 3:   internal class Program
 4:   {
 5:     private static void Main(string[] args)
 6:     {
 7:       var connectionString = "Your connection string here!";
 8:       var factory = DbProviderFactories.GetFactory("Teradata.Client.Provider");
 9:       var conn = factory.CreateConnection();
 10:       conn.ConnectionString = connectionString;
 11:       conn.Open();
 12:  
 13:       EventInfo ev = conn.GetType().GetEvent("InfoMessage");
 14:       var handler = EventProxy.Create(ev, HandleInfoMessage);
 15:       // add event handler 
 16:       ev.AddEventHandler(conn, handler);
 17:       try
 18:       {
 19:         // run sql call
 20:  
 21:  
 22:       }
 23:       finally
 24:       {
 25:         // unhook event handler 
 26:         ev.RemoveEventHandler(conn, handler);
 27:       }
 28:     }
 29:  
 30:     static void HandleInfoMessage(object obj, EventArgs eventArgs)
 31:     {
 32:       // Use Fasterflect, http://fasterflect.codeplex.com to dynamically access properties on the EventArgs object.
 33:       Console.WriteLine("InfoMessage {0}:{1}", obj.GetType().ToString(), eventArgs.GetType().ToString());
 34:     }
 35:   }
 36:  
 37:   public class EventProxy
 38:   {
 39:     public static Delegate Create(EventInfo evt, Action<object, EventArgs> d)
 40:     {
 41:       var handlerType = evt.EventHandlerType;
 42:       var eventParams = handlerType.GetMethod("Invoke").GetParameters();
 43:  
 44:       var parameters = eventParams.Select(p => Expression.Parameter(p.ParameterType, "x")).ToArray();
 45:       var body = Expression.Call(Expression.Constant(d), d.GetType().GetMethod("Invoke"), parameters[0], parameters[1]);
 46:       var lambda = Expression.Lambda(body, parameters.ToArray());
 47:       return Delegate.CreateDelegate(handlerType, lambda.Compile(), "Invoke", false);
 48:     }
 49:   }
 50: }

You could use this EventProxy and technique to hook into any event where the signature is (object, EventArgs) or any other class that descends from EventArgs.