Saturday, April 16, 2011

Installing F# on OS X

Download and install the latest mono package (2.10.1 at time of writing)

http://www.go-mono.com/mono-downloads/download.html

If you don't already have it, you'll also need Git

http://code.google.com/p/git-osx-installer/downloads/list?can=3

Then do the following from terminal:
git clone git://github.com/fsharp/fsharp
cd fsharp
autoreconf
./configure 
make
sudo make install

Sunday, March 27, 2011

django setup on osx

Quick steps to having a django site on OS X, here are the terminal commands:
Assumptions -
  • You have git installed
  • Current python release is 2.6
  1. cd ~/Downloads
  2. git clone https://github.com/django/django.git
  3. mv ~/Downloads/django/django /Library/Python/2.6/site-packages/
    (at this point you can test the installation by opening a python terminal and attempting to import django)
  4. sudo ln -s /Library/Python/2.6/site-packages/django/bin/django-admin.py /usr/bin/django-admin.py 
You can now make a first project:
  1. cd ~/
  2. mkdir Django
  3. cd Django
  4. django-admin.py startproject djangoFirst
Project and Web Applications are different things, you can add a web application to a project with the following command(from within your ~/Django/djangoFirst:
  • python manage.py startapp poll 
That's all it takes for the basics.  You can test the python project with the built in server (for dev/test purposes only, not production) with this command:
  • python manage.py runserver
If you're just getting started with django I'd recommend reading up on the design patterns behind it a bit.  The purpose of this post was just to get it running in OS X, anything beyond that will wait until I spend a bit more time with the environment.

Monday, February 21, 2011

Get the most recently written file out of a network share directory with PowerShell

Today’s task is to copy a database backup off of a network share and restore it locally.  So to start we’re copying from a network share to the local host.
$dbName = “MyDBName”
$RemotePath = "\\MyServer\Backup$\" + $dbName + "\"
That puts $RemotePath at a point where I have a variable with the folder that I want. Now test that it works and I have connectivity with a Get-ChildItem
Get-ChildItem $RemotePath
I received results back, so lets filter that down to 7zipped files.
Get-ChildItem $RemotePath –filter “*.7z”
This is good, but I should probably account for if someone decides they want to hide the backup files or something silly like that.
Get-ChildItem $RemotePath –force –filter “*.7z”
That all works, now to sort those files by LastWriteTime
(Thanks StackOverflow for this handy sort)
Get-ChildItem $RemotePath –force –filter “*.7z” | sort @{expression={$_.LastWriteTime}; Descending=$true}
That sorted the list fine, now to select just that last one and only its name.
Get-ChildItem $RemotePath –force –filter “*.7z” | sort @{expression={$_.LastWriteTime}; Descending=$true} | select Name -first 1
And wrapped into a function
function GetLastItem()
{
    param([string]$RemotePath)
    $returnString = Get-ChildItem $RemotePath -force -filter "*.7z" | sort @{expression={$_.LastWriteTime}; Descending=$true} | select Name -first 1
    return $returnString.Name
}
So to use this
$filename = GetLastItem $RemotePath
$RemoteFile = $RemotePath + $filename

Wednesday, February 9, 2011

EPiServer Event Cache Invalidation in a Multi-Server Environment

When multiple servers are setup in EPiServer CMS for load balancing that share a SQL Server backend one common issue is making certain that change from the admin on any server will invalidate the correct cache objects on all servers.

This is all setup very easily within web.config and can be tested quite easily.

I’m not going to go through and explain the full scope of what is going on to accomplish this, but you should know it uses the UDP protocol, multicasting, and Microsoft WCF.  For more information you can visit:

The EPiServer Tech-Note on Configuration

Relevant web.config Sections:

   1: <system.servicemodel>
   2:     <client>
   3:         <endpoint bindingconfiguration="RemoteEventsBinding"
   4:             name="RemoteEventServiceClientEndPoint" 
   5:             address="soap.udp://239.255.255.19:5000/RemoteEventService" 
   6:             binding="customBinding"
   7:             contract="EPiServer.Events.ServiceModel.IEventReplication" />
   8:     </client>
   9:     <bindings>
  10:         <custombinding>
  11:             <binding name="RemoteEventsBinding">
  12:                 <binarymessageencoding />
  13:                 <udptransport multicast="true" />
  14:             </binding>
  15:         </custombinding>
  16:     </bindings>
  17:     <extensions>
  18:         <bindingelementextensions>
  19:             <add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, EPiServer.Events" />
  20:         </bindingelementextensions>
  21:     </extensions>
  22:     <services>
  23:         <service name="EPiServer.Events.Remote.EventReplication">
  24:             <endpoint bindingconfiguration="RemoteEventsBinding" name="RemoteEventServiceEndPoint" address="soap.udp://239.255.255.19:5000/RemoteEventService" binding="customBinding" contract="EPiServer.Events.ServiceModel.IEventReplication" />
  25:         </service>
  26:     </services>
  27: </system.servicemodel>
  28: <system.webserver>
  29:     <modules runallmanagedmodulesforallrequests="true">
  30:         <add name="EventSubscriberHostModule" type="EPiServer.EventSubscriberHostModule, EPiServer" />
  31:     </modules>
  32: </system.webserver>

Monday, January 17, 2011

PowerShell scripts not running locally?

For security reasons the operating system might not run PowerShell scripts by default unless they are signed.  To make your local scripts run open a PowerShell prompt as administrator and run the following command:


Set-ExecutionPolicy RemoteSigned

Saturday, October 16, 2010

IDisposable and the Using statement

 

.Net has been around for about a decade now and I still run across code fairly regularly that completely skips out on closing connections or disposing of unmanaged resources.  Here is a basic overview that covers 95+% of when to implement IDisposable and when to wrap something in a using statement.

You'll occasionally run across objects that have a method called Dispose()

show_in_intellisense

The Dispose method exists to ensure that unmanaged resources are released.  It should only be used when you're using parts of the .Net framework that use unmanaged code (all database connections, GDI+, and most file related tasks).  The benefit of the method is that it gives you a guaranteed way to make sure that connections are closed.

When creating a class that owns unmanaged resources you can ensure that their dispose methods are called by implementing IDisposable on your own class:

class MyDisposableClass : IDisposable
    {
        private SqlConnection sqlConn;
        private SqlCommand sqlCmd; 
 
        public MyDisposableClass()
        {
            this.sqlConn = new SqlConnection();
            this.sqlConn.Open();
            this.sqlCmd.Connection = new SqlConnection();
        } 
 
        public void Dispose()
        {
            sqlConn.Dispose();
            sqlCmd.Dispose();
        }
    } 

To make things easier .Net added the using keyword.  Using allows you to create code blocks which call Dispose() automagically when their scope ends.

static void Main(string[] args)
{
using (var DisposableClass = new MyDisposableClass())
{
// Do stuff that needs DisposableClass here
}
}

This is functionally equivalent to doing the following Try/Finally code:

static void Main(string[] args)
{
   var DisposableClass = new MyDisposableClass())
    try
   {
       //Do stuff here
   }
   finally
   {
       DisposableClass.Dispose();
   }
} 

You can of course chain using's together when using unmanaged objects like this:

static void Main(string[] args)
{
    using (var sqlConnection = new SqlConnection())
    {
        using (var sqlCommand = new SqlCommand())
        {
            sqlConnection.Open();
            sqlCommand.Connection = sqlConnection;
        } // sqlCommand.Dispose() is called here
    } // sqlConnection.Dispose() is called here
}

References:


http://msdn.microsoft.com/en-us/library/yh598w02.aspx
http://msdn.microsoft.com/en-us/library/aa664736(VS.71).aspx
http://www.codeproject.com/KB/cs/tinguusingstatement.aspx  (most in depth)