Archive for April, 2007

Calgary Code Camp 2007

Apr 30

I went to Calgary Code Camp this weekend and thought I would write up a quick review. Justice has a series of live blogs for the sessions he attended. I figure with that much content I don’t need to write that much on code camp fashion so I will skip to the technical stuff.

Session #1: Building Reliable and Transacted Distributed Applications with WCF – Daniel Carbajal
This was a good session on showing how to take WCF past the basics of a contract & service. He showed how to do reliable messaging, queuing, transactions, and more. I think the talk was sullied by the fact that many people had no experience with WCF and were asking basic / off topic questions which really slowed down the presentation. I think the talk would be better with more time or when more people have worked with WCF. I found the talk useful to see especially as I am working on my first WCF app.

Session #2: .NET and Mono: Meet the Monkey – Tom Opgenorth
This was one of the most fun sessions I attended. The format was very open with lots of audience (and by audience I mean Bristowe) participation. Tom showed that Mono was a viable choice to develop apps for other platforms (Tom also mentioned that Mono can run on windows if for some reason you do not want to install the .NET framework). He also showed some of the IDE and Tool choices out there. It was a great and fun intro to Mono and makes me realize I now have to recode my php server apps in C#!

Session #3: Making Reflection Do Bad Things (So You Don’t Have To) – David Woods
It’s unfair to write a review of this as it I was presenting, but I am going to review myself anyways. This was my first time presenting and I think it went fairly well. I showed a lot of the core reflection things as well as showing how to generate a type at runtime.

I was worried that it would not be well received as generating a type at runtime is not something we have to do in our normal day. Most people I talked to liked seeing how you could do this but probably have no use for it. To that I answer: “generating IL at runtime is a sure fired way to impress the ladies”. Technically though there are a few good uses like generating a proxy at runtime for a type, being able to create DTOs from a file or other source (but remember you get no source code this way so they are hard to extend), or to generate re-useable machine / domain specific classes.

Overall I was happy with the talk even though my laptop seemed to be dying. I hope everyone else enjoyed it!

Session #4 & #5: Introducing XNA – John Bristowe & Bil Simser
This was a great two part session on showing how to create games for both windows and the xbox 360 using XNA. This talk covered things like pushing games to the xbox, debugging, sound, graphics, inputs, and more! I was amazed at how powerfully this mostly free tool was (you need a subscription to be able to push your games to the xbox 360 but win development is free).

I really liked the two projector presentation style. One was hooked up to the xbox and the other to the laptop so you could easily see the code and compiler pushing resources to the xbox and then the xbox running it. It was also really cool to see a game running on an xbox, setting a breakpoint in code and then being able to step through the code, change values via the immediate window, and see that reflected in the game. The whole development / debugging experience seems to be really great via XNA.

The talk was quite comprehensive I found. Almost every question asked either had an answer or else we tried it out and found the answer in the presentation. The presenters talked about so many  things and pointed out some of the great improvements over other platforms (i.e. the ease of using sound now, be able to use strings in games (in direct X you could only do images of strings before apparently), and to use any font installed on the development machine (it gets turned to a graphic so consumers will not need the font installed).

This was another good talk and a great closing presentation. I would have loved to have delved into more like 3D, collision detection, texturing models, and more but there is just not enough time to delve into these topics. I wish I had time to start developing games for my 360 as if there is anything that impresses the ladies more than generating types at runtime it is creating your own (possibly goat based) xbox game.

General
In general I thought this was a great code camp with a few exceptions. The venue was a bit hard to find as the univeristy campus is not very well marked. I was also supprised at the lack of power outlets in the rooms. Seems strange for a university to me. Other than that the venue was great, lots of great prizes (I think edmonton got a pretty good haul again this year but there were just too many prizes to clean house). It was well organized and quite well done in my opinion. The only thing that I noticed was not there was  review sheets for the different talks which I would have liked to see (but that is minor). All in all a big congrats to the organizers of this event!

Filed Under: Uncategorized

Code Camp Presentation Source Code & Resources

Apr 29

Thank to everyone who came out to see my presentation at Calgary Code Camp. No heads exploded so I think it was a success. Here is the source code and a few references.

Source Code
JoyOfReflection.zip (47.6 KB)

Resources
Reflector
MSDN OpCodes List
MSDN Reflection.Emit Reference

Happy Reflecting!

Filed Under: General

Social Distortion Review

Apr 27

 
On Tuesday night Social Distortion rocked one of the crappiest venues in Edmonton. Yes Edmonton Event Center, I am talking about you. Seriously how weak is it that instead of changing your name you just take the ‘R’ off of Reds and call yourself Eds? I don’t like the venue for acoustics, layout, and the “too good to be here” attitude of the staff.

Social Distortion is one of my favorite bands so it was great to see them for the first time. Formed in 1978 (seriously) they have gone through the death of bandmates, heroin addiction, law troubles (leading to the albulm Prison Bound), and countless band member changes. They have such a unique Johny Cash inspired sound (even doing a great cover of ring of fire). I deffinately recommend to check them out if you have not already in the last 30 years.

They put on a great show and they played every song I wanted to hear. The pit was full of energy and I have the bruises to prove that. For a rockabilly / cowpunk band the pit was quite agressive but you always get some assholes in a crowd I guess. The band did such a good encour that I did not want to leave the pit but finally had to at there last song due to a bit of heat exhaustion. Although I would not have been embarassed to faint at a Social D concert… it was just that good.

Filed Under: Personal

Impersonating An Identity Temporarily

Apr 24

I have always had problems with my current app as it has to impersonate a user to consume various different components and databases. Testing components in isolation is tricky with this setup. I did find a handy way to have a test impersonate a user for a test run:

Imports System.Runtime.InteropServices

Imports System.Security.Principal

Public Class
Impersonation

    Implements
IDisposable

    Dim
impersonationContext As
WindowsImpersonationContext

    <DllImport(“advapi32.dll”,
SetLastError:=True)> _
    Shared Function LogonUser(ByVal
principal As String,
ByVal authority As
String, ByVal
password As String,
ByVal logonType As
LogonTypes, ByVal logonProvider As LogonProviders, ByRef
token As IntPtr) As
Boolean
   
End Function

    <DllImport(“kernel32.dll”,
SetLastError:=True)> _
    Shared Function CloseHandle(ByVal
handle As IntPtr) As
Boolean
   
End Function

    Public Enum LogonTypes
        Interactive = 2
        Network
        Batch
        Service
        NetworkCleartext = 8
        NewCredentials
    End Enum

    Public Enum LogonProviders
        [Default] = 0
        WinNT35
        WinNT40
        WinNT50
    End Enum

    Private
user As String
   
Private
domain As String
   
Private
password As String

    Public Sub New(ByVal user As String, ByVal domain As String, ByVal password As String)
        Me.user
= user
        Me.domain
= domain
        Me.password
= password
        impersonationContext = Impersonate()
    End Sub

    Private Function GetUserToken() As
IntPtr
        Dim
token As IntPtr
        Dim result
As Boolean =
LogonUser(user, domain, password, LogonTypes.Interactive,
LogonProviders.Default, token)
        If (Not result) Then Throw New Exception(String.Format(“LogonUser failed: {0}. User:
{1}”, Marshal.GetLastWin32Error(), user))
       
Return
token
    End Function

    Private Function Impersonate() As
WindowsImpersonationContext
        Dim
token As IntPtr = GetUserToken()
        Dim
identity As WindowsIdentity = New WindowsIdentity(token)
        CloseHandle(token)
        System.Threading.Thread.CurrentPrincipal
= New WindowsPrincipal(identity)
        Return
identity.Impersonate()
    End Function

    Private Sub UndoImpersonation()
        If (Not impersonationContext Is
Nothing) Then
impersonationContext.Undo()
       
System.Threading.Thread.CurrentPrincipal = New
WindowsPrincipal(WindowsIdentity.GetCurrent())
    End Sub

    Public Sub Dispose() Implements
IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(True)
    End Sub

    Protected Sub Dispose(ByVal
disposing As Boolean)
        If (Not disposing) Then
           
Exit
Sub
       
Else
           
UndoImpersonation()
        End If
   
End Sub

End
Class

From there its easy for a test to use it:

    Private
identity As impersonation

    <SetUp()> _
    Public Sub Setup()
        identity = New
Impersonation(“User”, “Domain”, “Password”)
    End Sub

    <TearDown()> _
    Public Sub Teardown()
        identity.Dispose()
    End Sub

The dispose call will undo the impersonation and return it to be the current user.

Filed Under: General

My biggest COM+ issue solved

Apr 19

I have a com+ component that I have been involved with for years. It lacks tests but I wrote functional tests at the interface level which surprisingly has fairly good coverage of the code.

The issue with this is that the component HAS to impersonate an identity for the tests to work properly. So I have a pre/post build condition that installs / uninstall’s it from the gac/com+ catalog. My only manual intervention was to set the identity (every freaking time I wanted to run my tests).

To help with this issue I installed autohotkey which is a great keyboard & mouse record and replay tool (and its free). I then recorded a script to set the identity of the component. This works fairly well but will break when something changes on the com+ snap in screen (things are not where it expects it). Or if the CPU is taxed things might try to be clicked while a screen is still loading.

Searching the web I could not find a way to programmatically configure the identity of the app but finally found this little gem; Com+ Admin.

I created a new app and added a reference to the comAdmin type library

The code to create an application is relatively simple:

catalog = New COMAdmin.COMAdminCatalog

applications = catalog.GetCollection(“Applications”)

applications.Populate()

Dim application As COMAdmin.COMAdminCatalogObject

application = applications.Add()

application.Value(“IsEnabled”) = True ‘there are about 30-40 different properties for here

application.Value(“Identity”) = “DOMAIN\User.Name”

application.Password=”MyPasswordHere”

applications.SaveChanges()

catalog.InstallComponent(application.value(“ID”), “c:\build\mycomapp.dll, “”, “”) ‘add assemblies to the component

Pretty freakin easy! What I hate is that you have to remember the strings and proper types for the Value collection. What I did was create a wrapper around the application object that exposes all the properties in an easy fashion

i.e.

Public Class ComApplicationWrapper

Private application As COMAdmin.COMAdminCatalogObject

Public Sub New(ByVal application As COMAdmin.COMAdminCatalogObject)

Me.application = application

End Sub

Private Property Config(ByVal key As String) As Object

Get

Return application.Value(key)

End Get

Set(ByVal Value As Object)

application.Value(key) = Value

End Set

End Property

Public Property _3GigSupportEnabled() As Boolean

Get

Return Config(“3GigSupportEnabled”)

End Get

Set(ByVal Value As Boolean)

Config(“3GigSupportEnabled”) = Value

End Set

End Property

Public Property AccessChecksLevel() As COMAdmin.COMAdminAccessChecksLevelOptions

Get

Return Config(“AccessChecksLevel”)

End Get

Set(ByVal Value As COMAdmin.COMAdminAccessChecksLevelOptions)

Config(“AccessChecksLevel”) = Value

End Set

End Property

Now at least my com+ application can be easily installed so my tests will run. Next step is to create more granular unit tests that can be run in isolation instead of having to be run as an identity.

 You can download the beta code here of my com installer app.

Filed Under: General

The Testing Vs. Isolation Debate

Apr 19

At the eamug meeting last night it was briefly mentioned about having all methods public for testing (which I totally agree with) but can make consuming a class/api harder to use as everything is public. This is not usually the desired case, especially when building an API.

A lot of my applications are re-useable libraries. The way I keep it simple for my consumer is to have all the methods they will need in one namespace or assembly and then all the supporting classes and methods in other namespaces. This keeps things easy to consume and easy to test.

Filed Under: Uncategorized

Dear PHP

Apr 14

Dear PHP,

We have known each other for about seven years now. We had a lot of good times together. You were my introduction into web based programming and rescued me from that c++ monster I was seeing before. You taught me the ways of the web and your great website documentation and comment system helped me through some tricky times.

It is sad that we have grown apart recently, but unfortunately; you just aren’t right for me. Lets face it, you lack direction in your life. Your naming conventions are a strange mix of underscores and non-underscores which lack any constancy. Your object support is close to nonexistent. You lack consistency in your methods signatures as well like with strpos ( string $haystack, mixed $needle [, int $offset] ) and in_array ( mixed $needle, array $haystack [, bool $strict] ); should they not have the same order of parameters for constancy sake?

I am sick of fighting with you. Yesterday, when we tried to create a webservice it just ended in a terrible fight with me deciding that upgrading my OS and installing mono would probably be simpler. Adding new language features is always a fight and I usually have to be the one to solve it by recompiling you entirely which is consuming my life.

As you know .NET and I have been hanging out a lot recently and a relationship has formed. I hate to leave you but .NET does so much more for me. Its builtin input validation helps keeps me secure from the xss attacks you are plagued with. The builtin controls save me time from those mundane tasks. The great objectification of the language(s) saves me time and increases the quality of my code. Being able to unit test everything simply and easily is something that your community does not even know about. The best thing about .NET is that it has a great IDE… something you just don’t have.

We had a good run and some good times together but it is time for us to go separate ways. I am sure we will run into each other in the future and I hope we can still be friends. I am sure you will find another programmer who is meant for you as you are a good language. You are just not right for me and my needs.

Best wishes,
Dave

Filed Under: Uncategorized

Presenting At Calgary Code Camp

Apr 12

I have the great honor of presenting at Calgary Code Camp this April 28, 2007. Here is the old synopsis: 

Making Reflection Do Bad Things (So You Don’t Have To). 12:45 PM – 02:00 PM

Reflection is an immensely powerful tool that most developers shy away from. This talk will show how to get started using reflection in your applications. From reading and writing private/protected data in an object to creating a dynamic type at runtime this talk shows some of the great uses for reflection.

I see that the organizers placed Don and I in the same time spot so that there is at least one less person heckling Don.

Filed Under: Uncategorized

Pulitzer Winning Interview

Apr 12

I recently had the <sarcasm>pleasure</sarcasm> of being interviewed by Mr. Justice Gray. It actually was a lot of fun with some very…. unique questions. The interview can be found here.

Filed Under: Uncategorized

Silly Rogers, Traffic Shaping Is For Suckers

Apr 5

Michael Geist posted a great article today on what Rogers is doing to “fight” peer-to-peer sharing.

To sum it all up Rogers is analyzing traffic and throttling the bandwidth of what it suspects is p2p traffic. P2P clients have turned around and started encrypting packets to fight this tactic. So Rogers turns around and now throttles all encrypted traffic. The obvious side effect of this is that encrypted email, SSL, vpn, and all sorts of other encrypted traffic is now going to run slower on their network.

I am just amazed at this tactic. I don’t see why the networks care about p2p traffic. P2P is legal in Canada for one so no reason to block there. Secondly it could be a bandwidth issue but when a person buys an account they purchase so much bandwidth per month (so technically Rogers is selling something and then making it impossible to use all of it by doing something perfectly legal on your account). Thirdly, what are they thinking by throttling encrypted traffic (as if encryption does not already add its own overhead).

I am just shocked at this and am glad I am not on Rogers network (If we even can be here in Alberta).

Filed Under: Uncategorized