Category: C#

Url Redirect for moved blog

namespace Director { public class HttpModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(this.context_BeginRequest); } private void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; if (context.Request.FilePath.TrimEnd(‘/’).Length > 0 && !File.Exists(context.Request.PhysicalPath)) { context.Response.Redirect( $”{context.Request.FilePath}”, true); } } public void Dispose() { } } } <system.webServer> <modules runAllManagedModulesForAllRequests=”true”> <add name=”HttpModule” type=”Director.HttpModule” />… Read more →

Holistic Abstractions (Take 2)

In this post I will outline an updated perspective on Commands and Queries with respect to applying cross-cutting concerns (aka Aspects or Decorators). If you are unfamiliar with these two patterns then please see these posts here and here for a great introduction. Bertrand Meyer defines CQS as: every method should either be a command that performs an action, or… Read more →

dynamic code generation in c#

Copied directly from Given a Dictionary<string, string>, convert that dictionary into a type in as performant a manner as possible. The conversion will happen many time, and first time costs are acceptable. public static Func<Dictionary<string, string>, T> Generate<T>() where T : new() { var dic = Expression.Parameter(typeof (Dictionary<string, string>), “dic”); var tmpVal = Expression.Parameter(typeof (string), “tmp”); var args =… Read more →

LINQ: Group a list of strings into chunks

LINQ: Group a list of strings into chunks, dividing the chunks by a whole-line marker (in the example “<Custom>”) public static partial class Queries { public static IEnumerable<string[]> Execute( this IQueryHandler<DataChunker, IEnumerable<string[]>> handler, IEnumerable<string> data) { return handler.Execute(new DataChunker(data)); } public class DataChunker : IQuery<IEnumerable<string[]>> { public DataChunker(IEnumerable<string> data) { this.Data = data; } public IEnumerable<string> Data { get; set;… Read more →

Give background threads time to complete in IIS

Thanks to this post public sealed class OutOfProcessMediatorDecorator<TCommand> : IMediatingHandler<TCommand>, IRegisteredObject where TCommand : IMediator { private readonly IMediatingHandler<TCommand> decorated; private readonly Container container; public OutOfProcessMediatorDecorator( IMediatingHandler<TCommand> decorated, Container container) { this.decorated = decorated; this.container = container; } public void Execute(TCommand command) { HostingEnvironment.RegisterObject(this); var task = Task.Factory.StartNew(() => { using(container.BeginLifetimeScope()) { try { this.decorated.Execute(command); } finally { HostingEnvironment.UnregisterObject(this); }… Read more →


interaction from javascript in the Document to external C# code [ComVisible(true)] public class ScriptProxy { // This method can be called from JavaScript. public void AMethod(string message) { MessageBox.Show(message); } } this.webBrowser1.ObjectForScripting = new ScriptProxy(); <input type=”button” value=”Go Again!” onclick=”window.external.AMethod(‘Hello’);” /> subscriber to browser events thanks to this answer [ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDispatch)] public class EventListener { [DispId(0)] public void NameDoesNotMatter(object data)… Read more →