The Technical Adventures of Adam Weigert

  • dynamic? I'll never use that ... or then again, maybe it could ...

    So, I don't know about you, but I was highly skeptical of the dynamic keywork when it was announced. I thought to myself, oh great, just another move towards VB compliance.

    Well after seeing it being used in things like DynamicXml (which I use for this example) I then was working with a MVC controller and wanted to move some things like operation timeout of an action to a configuration file. Thinking big picture, it'd be really nice to have configuration for all my controllers like that. Ugh, I don't want to have to create all those ConfigurationElement objects...

    So, I started thinking self, use what you know and do something cool ...

    Well after a bit of zoning out, self came up with use a dynamic object duh! I was thinking of a config like this ...

    <controllers>
      <add type="MyApp.Web.Areas.ComputerManagement.Controllers.MyController, MyApp.Web">
        <detail timeout="00:00:30" />
      </add>
    </controllers>

    So, I ended up with a couple configuration classes like this ...

    blic abstract class DynamicConfigurationElement : ConfigurationElement
    {
        protected DynamicConfigurationElement()
        {
            this.DynamicObject = new DynamicConfiguration();
        }
    
        public DynamicConfiguration DynamicObject
        {
            get;
            private set;
        }
    
        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            this.DynamicObject.Add(name, value);
            return true;
        }
    
        protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
        {
            this.DynamicObject.Add(elementName, new DynamicXml((XElement)XElement.ReadFrom(reader)));
            return true;
        }
    }
    
    public class ControllerConfigurationElement : DynamicConfigurationElement
    {
        [ConfigurationProperty("type", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
        public string TypeName
        {
            get { return (string)this["type"]; }
        }
    
        public Type Type
        {
            get { return Type.GetType(this.TypeName, true); }
        }
    }
    
    public class ControllerConfigurationElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ControllerConfigurationElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ControllerConfigurationElement)element).Type;
        }
    }

    And then had to create the meat of the DynamicConfiguration class which looks like this ...

    public class DynamicConfiguration : DynamicObject
    {
        private Dictionary<string, object> properties = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);
    
        internal void Add<T>(string name, T value)
        {
            this.properties.Add(name, value);
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var propertyName = binder.Name;
    
            result = null;
    
            if (this.properties.ContainsKey(propertyName))
            {
                result = this.properties[propertyName];
            }
    
            return true;
        }
    }

    So all being said, I made a base controller class like a good little MVC-itizen ...

    public abstract class BaseController : Controller
    {
        protected BaseController()
            : base()
        {
            var configuration = ManagementConfigurationSection.GetInstance();
                
            var controllerConfiguration = configuration.Controllers.ForType(this.GetType());
    
            if (controllerConfiguration != null)
            {
                this.Configuration = controllerConfiguration.DynamicObject;
            }
        }
    
        public dynamic Configuration
        {
            get;
            private set;
        }
    }

    And used it like this ...

    public class MyController : BaseController
    {
        static readonly string DefaultDetailTimeout = TimeSpan.MaxValue.ToString();
    
        public MyController()
        {
            this.DetailTimeout = TimeSpan.Parse(this.Configuration.Detail.Timeout ?? DefaultDetailTimeout);
        }
    
        public TimeSpan DetailTimeout
        {
            get;
            private set;
        }
    }

    And there I have an actual use for the dynamic keyword ... never thoguht I'd see the day when I first heard of it as I don't do much COM work ... oh dont' forget this little helper extension methods to find the controller configuration by the controller type.

    public static ControllerConfigurationElement ForType<T>(this ControllerConfigurationElementCollection collection)
    {
        Contract.Requires(collection != null);
        return ForType(collection, typeof(T));
    }
    
    public static ControllerConfigurationElement ForType(this ControllerConfigurationElementCollection collection, Type type)
    {
        Contract.Requires(collection != null);
        Contract.Requires(type != null);
        return collection.Cast<ControllerConfigurationElement>().Where(element => element.Type == type).SingleOrDefault();
    }

    Sure, it isn't perfect and I'm sure I can tweak it over time, but I thought it was a pretty cool way to take advantage of the dynamic keyword functionality. Just remember, it only validates you did it right at runtime, which isn't that bad ... is it? And yes, I did make it case-insensitive so my code didn't have to look like my XML objects, tweak it to your liking if you dare to use this creation.

    Read more...

  • The WaitForAll Roadshow

    OK, so I took for granted some imaginative uses of WaitForAll but lacking that, here is how I am using. First, I have a nice little class called Parallel that allows me to spin together a list of tasks (actions) and then use WaitForAll, so here it is, WaitForAll's 15 minutes of fame ...

    First Parallel that allows me to spin together several Action delegates to execute, well in parallel.

     

    public static class Parallel
    {
        public static ParallelQuery<Action> Task(Action action)
        {
            return new Action[] { action }.AsParallel();
        }
    
        public static ParallelQuery<Action<T>> Task<T>(Action<T> action)
        {
            return new Action<T>[] { action }.AsParallel();
        }
    
        public static ParallelQuery<Action> Task(this ParallelQuery<Action> actions, Action action)
        {
            var list = new List<Action>(actions);
            list.Add(action);
            return list.AsParallel();
        }
    
        public static ParallelQuery<Action<T>> Task<T>(this ParallelQuery<Action<T>> actions, Action<T> action)
        {
            var list = new List<Action<T>>(actions);
            list.Add(action);
            return list.AsParallel();
        }
    }
    

     

    Next, this is an example usage from an app I'm working on that just is rendering some basic computer information via WMI and performance counters. The WMI calls can be expensive given the distance and link speed of some of the computers it will be trying to communicate with. This is the actual MVC action from my controller to return the data for an individual computer.

     

    public PartialViewResult Detail(string computerName)
    {
        var computer = this.Computers.Get(computerName);
        var perf = Factory.GetInstance<IPerformanceCounterProvider>();
    
        var detail = new ComputerDetailViewModel()
            {
                Computer = computer
            };
    
        try
        {
            var work = Parallel
                .Task(delegate 
                {
                    // Win32_ComputerSystem
                    var key = computer.Name + "_Win32_ComputerSystem";
                    var system = this.Cache.Get<Win32_ComputerSystem>(key);
    
                    if (system == null)
                    {
                        using (var impersonation = computer.ImpersonateElevatedIdentity())
                        {
                            system = computer.GetWmiContext().GetInstances<Win32_ComputerSystem>().Single();
                        }
    
                        this.Cache.Set(key, system);
                    }
    
                    detail.TotalMemory = system.TotalPhysicalMemory;
                    detail.Manufacturer = system.Manufacturer;
                    detail.Model = system.Model;
                    detail.NumberOfProcessors = system.NumberOfProcessors;
                })
                .Task(delegate 
                {
                    // Win32_OperatingSystem
                    var key = computer.Name + "_Win32_OperatingSystem";
                    var os = this.Cache.Get<Win32_OperatingSystem>(key);
    
                    if (os == null)
                    {
                        using (var impersonation = computer.ImpersonateElevatedIdentity())
                        {
                            os = computer.GetWmiContext().GetInstances<Win32_OperatingSystem>().Single();
                        }
    
                        this.Cache.Set(key, os);
                    }
    
                    detail.OperatingSystem = os.Caption;
                    detail.OSVersion = os.Version;
                })
                // Performance Counters
                .Task(delegate 
                {
                    using (var impersonation = computer.ImpersonateElevatedIdentity())
                    {
                        detail.AvailableBytes = perf.GetSample(computer, "Memory", "Available Bytes");
                    }
                })
                .Task(delegate 
                {
                    using (var impersonation = computer.ImpersonateElevatedIdentity())
                    {
                        detail.TotalProcessorUtilization = perf.GetValue(computer, "Processor", "% Processor Time", "_Total");
                    }
                }).WithExecutionMode(ParallelExecutionMode.ForceParallelism);
    
            if (!work.WaitForAll(TimeSpan.FromSeconds(15), task => task()))
            {
                return PartialView("Timeout");
            }
        }
        catch (Exception ex)
        {
            this.LogException(ex);
            return PartialView("Error.ascx");
        }
    
        return PartialView(detail);
    }
    

    Read more...

  • Reinventing the Paged IEnumerable, Weigert Style!

    I am pretty sure someone else has done this, I've seen variations as PagedList<T>, but this is my style of a paged IEnumerable collection. I just store a reference to the collection and generate the paged data when the enumerator is needed, so you could technically add to a list that I'm referencing and the properties and results would be adjusted accordingly.

    I don't mind reinventing the wheel when I can add some of my own personal flare ...

    // Extension method for easy use
    public static PagedEnumerable AsPaged(this IEnumerable collection, int currentPage = 1, int pageSize = 0)
    {
        Contract.Requires(collection != null);
        Contract.Assume(currentPage >= 1);
        Contract.Assume(pageSize >= 0);
    
        return new PagedEnumerable(collection, currentPage, pageSize);
    }
    
    public class PagedEnumerable : IEnumerable
    {
        public PagedEnumerable(IEnumerable collection, int currentPage = 1, int pageSize = 0)
        {
            Contract.Requires(collection != null);
            Contract.Assume(currentPage >= 1);
            Contract.Assume(pageSize >= 0);
    
            this.collection = collection;
            this.PageSize = pageSize;
            this.CurrentPage = currentPage;
        }
    
        IEnumerable collection;
    
        int currentPage;
        public int CurrentPage
        {
            get 
            {
                if (this.currentPage > this.TotalPages)
                {
                    return this.TotalPages;
                }
    
                return this.currentPage; 
            }
            set
            {
                if (value < 1)
                {
                    this.currentPage = 1;
                }
                else if (value > this.TotalPages)
                {
                    this.currentPage = this.TotalPages;
                }
                else
                {
                    this.currentPage = value;
                }
            }
        }
    
        int pageSize;
        public int PageSize
        {
            get 
            {
                if (this.pageSize == 0)
                {
                    return this.collection.Count();
                }
    
                return this.pageSize;
            }
            set
            {
                this.pageSize = (value < 0) ? 0 : value;
            }
        }
    
        public int TotalPages
        {
            get
            {
                return (int)Math.Ceiling(this.collection.Count() / (double)this.PageSize);
            }
        }
    
        public IEnumerator GetEnumerator()
        {
            var pageSize = this.PageSize;
            var currentPage = this.CurrentPage;
            var startCount = (currentPage - 1) * pageSize;
                
            return this.collection.Skip(startCount).Take(pageSize).GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
    

    Read more...

  • PLINQ Adventure Land - WaitForAll

    PLINQ is awesome for getting a lot of work done fast, but one thing I haven't figured out yet is how to start work with PLINQ but only let it execute for a maximum amount of time and react if it is taking too long. So, as I must admit I am still learning PLINQ, I created this extension in that ignorance. It behaves similar to ForAll<> but takes a timeout and returns false if the threads don't complete in the specified amount of time. Hope this helps someone else take PLINQ further, it definitely has helped for me ...

     

    public static bool WaitForAll<T>(this ParallelQuery<T> query, TimeSpan timeout, Action<T> action)
    {
        Contract.Requires(query != null);
        Contract.Requires(action != null);
    
        var exception = (Exception)null;
        var cts = new CancellationTokenSource();
                
        var forAllWithCancellation = new Action(delegate
        {
            try
            {
                query.WithCancellation(cts.Token).ForAll(action);
            }
            catch (OperationCanceledException)
            {
                // NOOP
            }
            catch (AggregateException ex)
            {
                exception = ex;
            }
        });
                
        var mrs = new ManualResetEvent(false);
        var callback = new AsyncCallback(delegate { mrs.Set(); });
        var result = forAllWithCancellation.BeginInvoke(callback, null);
                
        if (mrs.WaitOne(timeout))
        {
            forAllWithCancellation.EndInvoke(result);
    
            if (exception != null)
            {
                throw exception;
            }
    
            return true;
        }
        else
        {
            cts.Cancel();
            return false;
        }
    }

    Read more...

  • Overriding Inheritance for IIS 6.0 Virtual Directories

    If you have an IIS site that has a mixture of ASP.NET 1.1 and 2.0 virtual directories below it, changing the root site ASP.NET settings could affect the virtual directory ASP.NET settings as well since IIS inherits except for explicit overrides. This little PowerShell script will take each setting and persist it to the virtual directory so you are able to freely change the root site settings without affecting the applications below it.

    Please remember to backup your IIS settings before you do this, just incase it has adverse affects on your IIS server.

    function Persist-IIsSettings ( $obj ) {
        Write-Host $obj.Path
       
        $obj.Properties.GetEnumerator() |% {
            $propertyName = $_.PropertyName
            $value        = $obj.Get($propertyName)
           
            if ( -not ($value -is [System.__ComObject]) ) {
                $obj.Put($propertyName, $value)
            }
        }
       
        $obj.SetInfo()

        $obj.Children |% { Persist-IIsSettings $_ }
    }

    Persist-IIsSettings ([ADSI]"IIS://localhost/W3SVC/1/ROOT")

    Read more...

  • ASP.NET PowerShell Data Source Control

    Extending on the small proof-of-concept I mentioned yesterday I created this simple data source control that lets you bind a Repeater or DataGrid to it like an ObjectDataSource control but it executes a PowerShell script to retrieve the results.

    <asp:PowerShellDataSource ID="EmployeesDataSource" runat="server">
        [PSObject]"" | Add-Member NoteProperty "Title" "Software Engineer" -passThru | Add-Member NoteProperty "Name" "Mike Wolford" -passThru
        [PSObject]"" | Add-Member NoteProperty "Title" "Software Evangelist" -passThru | Add-Member NoteProperty "Name" "Scott Root" -passThru
    </asp:PowerShellDataSource>



    <asp:Repeater runat="server" DataSourceID="EmployeesDataSource">
        <ItemTemplate>
            <h4><%# DataBinder.Eval(Container.DataItem, "Title") %>: <%# DataBinder.Eval(Container.DataItem, "Name") %></h4>
        </ItemTemplate>
    </asp:Repeater>

    If this is something that interests you, take a look over  at http://aspower.codeplex.com/

    Read more...

  • ASP.NET PowerShell Runspace Control

    This is just a little concept project I am working on so I can run PowerShell scripts within my ASP.NET applications. As I am a server administrator, I love PowerShell + WMI, and bringing this power into ASP.NET my automation capabilities are being greatly simplified. I'll build on this solution as I play around with actually implementing this as a primary source of automation from support a Windows Server environment.

    I am aware of other projects/products that allow you to do similar things, I just needed something simple, and I don't want to code an entire page in PowerShell, just specific tasks.

     http://aspower.codeplex.com/

    Read more...

  • LINQ: Expressive Html Tag Building

    I hate building HTML tags in code, but it needs to be done. I just wanted to make it a little cleaner. So I came up with this method that utilizes LINQ expressions to generate the attributes for the tag. This is really only clean with simple tags, but I use it with my other tag building methods to keep them clean as well. I've seen others look for something like this and thought it'd be helpful posting it here. I haven't deeply tested this code but it shows the general concept and I'm sure it needs cleaned up a little. The following is an example calling the Tag method:

       1:  Tag( "a", "The Technical Adventures of Adam Weigert", href => "http://weblogs.asp.net/adweigert/" );
       2:  Tag( "div", "LINQ Expressions Rock", style => "font-size: 250%; font-weight: bold;", id => "title" );

    This is the actual method, I love how simple the LINQ expression makes building the attributes.

       1:  public string Tag( string tagName, string innerHtml, params Expression<Func<string, string>>[] attributes )
       2:  {
       3:      XElement tag = new XElement( XName.Get( tagName, string.Empty ) );
       4:      
       5:      if ( attributes != null )
       6:      {
       7:          foreach ( var attribute in attributes )
       8:          {
       9:              string attributeName = attribute.Parameters[ 0 ].Name;
      10:              string attributeValue = attribute.Compile()(string.Empty);
      11:   
      12:              tag.SetAttributeValue( XName.Get( attributeName, string.Empty ), attributeValue );
      13:          }
      14:      }
      15:   
      16:      if ( !string.IsNullOrEmpty( innerHtml ) )
      17:      {
      18:          tag.Add( XElement.Parse( "<xml>" + innerHtml + "</xml>" ).Nodes() );
      19:      }
      20:   
      21:      return tag.ToString();
      22:  }

    Update: Used XElement instead, and better innerHtml handling. Thanks to everyone that helped improve this method.

    Read more...

  • PowerShell: Install-Gac (GACUTIL for PowerShell)

    So, I don't know about anyone else, but it is a pain to use GACUTIL on systems without the .NET SDK. So, I found out there is a .NET API for GACUTIL, so now I just wrapped it in PowerShell to make it easier to use. :)

       1:  BEGIN {
       2:      $ErrorActionPreference = "Stop"
       3:      
       4:      if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) ) {
       5:          [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null
       6:      }
       7:      
       8:      $publish = New-Object System.EnterpriseServices.Internal.Publish
       9:  }
      10:  PROCESS {
      11:      $assembly = $null
      12:      
      13:      if ( $_ -is [string] ) {
      14:          $assembly = $_
      15:      } elseif ( $_ -is [System.IO.FileInfo] ) {
      16:          $assembly = $_.FullName
      17:      } else {
      18:          throw ("The object type '{0}' is not supported." -f $_.GetType().FullName)
      19:      }
      20:      
      21:      if ( -not (Test-Path $assembly -type Leaf) ) {
      22:          throw "The assembly '$assembly' does not exist."
      23:      }
      24:      
      25:      if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0 ) {
      26:          throw "The assembly '$assembly' must be strongly signed."
      27:      }
      28:      
      29:      Write-Output "Installing: $assembly"
      30:      
      31:      $publish.GacInstall( $assembly )
      32:  }

    Read more...