.NET 4.0 // C# // EF

Entity Framework 4.0 - Running Ad hoc Queries or Commands

A nice addition to the latest version of ADO.NET Entity Framework is the ability to run ad hoc commands.  Two methods of the Entity Context have been introduced to accomplish that: ExecuteStoreQuery and ExecuteStoreCommand. The tricky part is passing the parameters. They are index based so the example below shows you how. string sql = "SELECT Col1, Col2 FROM MyUdf({0},{1});"; IEnumerable<MyComplexType> list = dbclient.ExecuteStoreQuery<MyComplexType>(sql, parameter1, parameter2);

WPF Themes

WPF Style themes by Microsoft, combined. [More]
.NET 4.0 // C# // WF

Running Workflows in .NET 4.0

Workflows can be run synchronously and asynchronously. Below is an example how you run a workflow instance synchronously: Dictionary<string, object> inputs = new Dictionary(); inputs.Add("InArgument", "SomeValue"); inputs.Add("InOutArgument", "SomeOtherValue"); WorkflowElement activity = workflow; IDictionary<string, object> outputs = WorkflowInvoker.Invoke(new MyWorkflow(), inputs); MyObject obj = (MyObject)outputs["OutArgument"]; MyOtherObject obj = (MyOtherObject)outputs["InOutArgument"]; Note that now workflows have arguments as well as parameters. Arguments are for passing data to and from the Workflow where parameters are for internal use. Remember: Workflow is an activity so the same goes for any custom activity you create. Any In or In/Out arguments have to be passed to the workflow for it to execute. Now here is a very simplified example of how to run a workflow instance asynchronously. The OnCompleted event is raised when the workflow has finished running and the IDictionary<string, object> outputs can be read off of the WorkflowCompletedEventArgs. ManualResetEvent resetEvent = new ManualResetEvent(false); WorkflowInstance instance = new WorkflowInstance(new MyWorkflow()); instance.OnCompleted = delegate(WorkflowCompletedEventArgs e) { Console.WriteLine("workflow instance completed, Id = " + instance.Id); resetEvent.Set(); }; instance.Run(); resetEvent.WaitOne(); I haven't done it yet but I presume running a workflow in IIS and interacting with it before it finishes would not be as staighforward as the examples above.

In .NET 4.0 SecurityManager.IsGranted is obsolete

In .NET 4.0 SecurityManager.IsGranted has been made obsolete.  This is what it was and if you compile in .NET 4.0 compatibility it will complain. bool isGranted = SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.Infrastructure)) In order to fix this now you have to do this instead. var permissionSet = new PermissionSet(PermissionState.None); permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode)); bool isGranted = permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);