Monthly Archives: May 2005

Don’t forget the mexican spices.

A few of the folks from Readify were staying at the Corus Hotel in Sydney last night so we decided to take the opportunity to have a bit of a meeting. While we were in the bar waiting for everyone to arrive a troop of singers walked in and started singing a song – I took a photo to record the event.

CorusSingers

At the time I thought that it was maybe a pitch to an advertising executive but I didn’t hear a single product placement in there. For those who are interested – this is the song that they were singing, although some lines were repeated quite a few times – it went for about 5–10 minutes.

Chili con carne
säv. ja san. Anders Adenroth, suomennos Matti Pulkkinen

Take some ripe and fresh tomatoes,
put them in a bowl with oil.
Add the onions, strong and tasty,
on the stove you let it boil.
Choose the finest meat you get from cow or pig,
it doesn’t matter.
Beans and jalopeño peppers, necessary is to this.
But don’t forget the mexican spices,
the heart of the art of the cooking. Oh no!
Don’t forget the mexican spices,
without them you won’t get the flavor of sunshine.
Chili con carne aha
let’s make chili con carne aha that’s good!
Eat your chili nice and slowly
to prevent a stomachache,
crisp tortillas on your plate
will sound so nicely when they brake.
When your mouth gets full of fire
you might need something to drink,
one or two or three or four or seven beers
will be enough.
But don’t forget the mexican spices,
the heart of the art of the cooking. Oh no!
Don’t forget the mexican spices,
without them you won’t get the flavor of sunshine.
Chili con carne aha
let’s make chili con carne let’s eat!

 

How to implement an Indigo proxy from scratch.

So most people would have already figured this out, but I thought I would post up about it anyway. The proxies generated by svcutil.exe in Indigo derive from ProxyBase<T> where T is an interface with a ServiceContract attributed.

One of the things that I like to do is figure out how to build up a proxy from scratch to see what all the critical pieces of the service model are from a client perspective (since normally the generated code includes more than it has to). So in the example below I contstruct an instance of MathProxy which derives from ProxyBase<T>, passing in IMath (service contract/interface) as the type parameter.

The type parameter is used to build an InnerProxy which implements the IMath interface. Then all you have to do is create wrapper methods which call that interface. Also note that I pass the contruction call to the protected ProxyBase(Binding) constructor. You need to specify the binding you are going to use up from to correctly configure the proxy.

IndigoProxyFromScratch

I’m looking forward to posting up more about Indigo (and Avalon) as I start understanding the models that support the abstraction.

Who is John Galt?

I just finshed reading Ayn Rand’s Atlas Shrugged. This book has been recommended to me several times over the past eight or so years but I tend not to take much time to read many books other than technical references, although I have taken the time to read things like the Harry Potter and Artemis Fowl series, and even re-read some of Issac Asimov’s works. But I should have read Atlas Shrugged earlier.

The book is extremely powerful but very difficult to penetrate so you really have to WANT to read it. I read quite slowly so this 1000+ page volume has taken me a few months to get through with casual reading. The book really has layers, one layer is pure story with characters, plot, love and war – the other layer is a manifesto for life.

The funny thing is that I don’t think anyone would enjoy both layers at the same time (I could be wrong but thats what I felt). I was most interested in the philosophical aspects of the book because it is helping me deal with some issues that I am trying deal with at the moment. It certainly helps put things into perspective.

A Class A Day: System.Activator

So I’ve done the math, if I do just one class a day I am going to be at this for decades given the size and scope of the .NET type-system. In fact its worse than that because between .NET 1.x and 2.0 the size of the out-of-the-box class library effectively doubled and if I included all thats coming in WinFX in my TODO list then I’ll never get done. The great thing about my original goal was that I wasn’t going to talk about every class on this blog.

Anyway – I did want to do a pretty quick post about the System.Activator class. The Activator class is one of those useful types which would be used in pretty much every enterprise scale .NET application around, both inside the Framework itself but also directly by developers like you and me. Its role in life is basically to support the instansiation of objects whether they be PONO (Plain Old .NET Objects), COM objects are even remote objects exposed by the .NET Remoting plumbing. To support that there are three main methods – CreateInstance(…), CreateComInstanceFrom(…) and GetObject(…), each with their own set of overloads.

Now – in .NET 2.0 there has been a couple of modifications (from a public API point of view) to the Activator class so that now there is actually a generic CreateInstance method. Now the curious thing is that this won’t necessarily help people who are using the CreateInstance method to do things like plug-in loading because you actually have to have the type information you are want to create an instance available at compile time. So effectively this:

SomeType instance = Activator.CreateInstance<SomeType>();

Is the same as this:

SomeType instance = new SomeType();

So you are probably asking the why question now right? A not-so-old wise man once said to me – “I don’t answer why questions”. Unfortunately I’m not going to take his advice and instead I’m going to try and speculate where this method might be useful – I’m thinking generic factory classes which attach background behaviours to the types they create. Here is a trivial example.

GenericCreateInstanceUseCase

Now I don’t necessarily recommend spinning up a thread for every object you create in everyday code but I wanted to illustrate a concept. The use of anonymous methods was pretty cute though don’t you think?

P.S. I knew it – CreateProcess<T> is used inside System.ServiceModel in Indigo. I guessed, checked and was right. It just strikes me as the kind of place you would want to do the kind of interception that this little fella allows.

A Class A Day: System.ActivationContext

Astute readers would have noticed that when I started on my “class a day” journey that I missed out on the System._AppDomain interface and skipped straight to System.AccessViolationException. Thats because I am going to look at it when I look at the System.AppDomain class (notice the subtle difference there?) a little way down the track.

While I am at it I am going to excuse myself from talking about the Generic Action delegate which I have already given some coverage to. So what’s next? Answer – System.ActivationContext.

The ActivationContext class is a new addition to the .NET Framework in the 2.0 release and we have a quick look at the Callee Graph in Reflector we can see that it has something to do with ClickOnce deployment.

CalleeGraphActivationContext

Basically, the ActivationContext is available to applications that have been deployed by ClickOnce and give those applications access to important information such as where the application came from. This information is loaded out of the application manifest which is part of the set of files that are deployed to a server in order to support ClickOnce.

Developers don’t actually get a whole heap of access to the application manifest information from the ActivationContext class because the really interesting methods are marked as internal as the picture below generated from Reflector shows.

OverviewActivationContext

One thing that I thought was pretty amusing in the above listing was the internal inner enum called ApplicationStateDisposition. Way back when I used to do technical support I used to tell people that computers are just a machine, and they don’t hate their users, no matter how much you think that file corruption is a personal slight against them. Well – it looks like I was wrong.

Anyway – I think that the ActivationContext class like many of the classes that deal with deployment in the .NET Framework will provide useful diagnostic information for support staff. So what I would do is display something like the ApplicationIdentity attached as a property to the ActivationContext in some part of an About box – although ClickOnce kinda does that for free.

ApplicationIdentityForActivationContext

A Class A Day: System.AccessViolationException

I get to take time away from consulting work and teach our .NET ESSENTIALS class from time to time. On the first day most students will hear me utter the words “its not possible to know every class in the .NET framework”, and I pretty much think that it is true, except for perhaps Brad Abrams who did excellent work with the SLAR.

It is however just a theory, and theories need to be tested to see if they are true – but how do I test it? Well I’ve decided to walk the framework, from top to bottom to see how much information I can retain. Back when I was teaching myself about .NET in 2000 I would pick a namespace each night and understand as much about it as I could.

My hope is that by going much deeper I’ll not only learn a lot about the classes in the Framework but also get insights into the runtime itself. Logically features in the runtime eventually have to manifest themselves in the Framework so there should be lots to learn.

On my journey I’ll post up anything interesting I find. First stop? System.AccessViolationException.

So why is an exception class interesting? Well for starters AccessViolationException is new in the .NET Framework 2.0 and its designed to replace the use of a NullReferenceException in some instances.

If you’ve ever written any interop code before (especially P/Invoke where you are dealing with pointers) you may have gotten strange NullReferenceException errors – looking at the code you can’t see where but when you debug you notice that its coming out of a member that ultimately does an interop call.

Well in .NET 1.x this could be an indication that the memory that the pointer refers to never was or is no longer allocated to the program and the runtim doesn’t know how to deal. Unfortunately NullReferenceException doesn’t really describe the problem – presumably thats what this new exception is all about, making the problem easier to spot.

Now when you get this error its a bug in your code, pure and simple so you should really crash the application if possible but if for the sake of argument you had a .NET 1.x application which happened to detect this condition and work around it, the change to AccessViolationException would mean that .NET 2.0 has introduced a breaking change.

Fortunately the guys at Microsoft have thought about this and have introduced a configuration setting which you can slap in the <runtime /> section of the application configuration file.

<LegacyAccessViolationPolicy enabled=”1” />

Now – what else have I noticed? Well AccessViolationException ultimately derives from System.Exception and if you look closely you can see that in .NET 2.0 it has been given a new property called Data which is an IDictionary. I guess the idea here is that you can provide additional information when you throw exceptions without having to create a new type of exception.

Where this might be useful is when you have an exception defined for your particular component but when you throw it you want to pass back all the interesting variables that are in scope at the time. This really makes exceptions a bit more useful for fault finding without having to write a lot of exception classes for each condition – it guess thats a good thing . . .