Coding Horrors

I found me a new blog to read (like I need more, I have 179 feeds in Bloglines currently).

Coding Horror

Its similar in essence to what I kinda aim to do with my blog, although he’s being a bit more successful (i.e. A coding blog with a bit of other stuff, whereas mine is more a other stuff blog with a bit of coding, and not updated as often!)

Anyway, I thought I’d quickly mention two of his recent posts that caught my eye.

Coding Horror: C# Snippet Parity

Microsoft have apparently released a new set of code snippets for C#. Now I’ve never been a big user of code snippets in general, although I have started to use the property ones in Visual Studio 2005 quite a lot. But having installed these code snippets I have even learnt a thing or two. Examples include capturing the output from a console app, pinging a web site. There are many other useful code snippets that could save so much time and effort, and also result in better code (such as some of the database snippets that wrap execute statements in a try finally block to make sure connections are always closed).

Coding Horror: Darwinia

I looked at this entry and thought – looks interesting, wonder if there is a demo. Quick jump to the web site and minutes larger I’m blasting little red pixels to protect my little green pixels (it really is that basic on some levels). After completing the demo level I almost immediately paid for the full game (only $20 if downloaded via Steam), minutes later I’m playing the game and many hours later (late into the night) I emerge bleary eyed, it has proved an absolute winner. I may have to check out their previous game Uplink which is also meant to be very good. I also think the future of games is the download distribution model. (They have a new game due soon as well, Defcon).

Darwinia

Stealing things like lists.

I’m not one for lists most of the time, but this one had some really cool questions in it that caught my imagination:

Stolen from here

1. When you looked at yourself in the mirror today, what was the first thing you thought?
How good I’m lookin’ (for the record, today my weight dropped to below 13stone, so I’m in happy mood).

2. How much cash do you have on you?
£19.06

3. What’s a word that rhymes with “TEST”
West

4. Favourite planet?
Alpha Centuri (ok, so this is a star – Pluto then, cause of the dog in Mickey Mouse)

5. Who is the 4th person on your missed call list on your mobile?
Work

6. What is your favourite ring on your phone?
Whats a ringtone?

7. What shirt are you wearing?
Next work shirt – one of my favourites

8. Do you label yourself?
Geek – but other than that I’m comfortable without labels.

9. Name the brand of your shoes you’re currently wearing.
Clarks

10. Bright or Dark Room?
Natural bright light or dark room.

11. What were you doing at midnight last night?
Darwinia – hence why I’m so tired :)

12. What did your last text message you received on your cell say?
Advanced warning: big lamp gig on friday (from Camo) (which is now last friday) – I obviously don’t get that many texts.

13. Where is your nearest 7-11?
Spar? Pelaw, Gateshead probably.

14. What’s a saying that you say a lot?
Tappa Tappa Tappa

15.Who told you they loved you last?
Caroline, last night.

16. Last furry thing you touched?
My wife’s hot water bottle :)

17. How Many Drugs Have You Done In The Past three Days?
None

18. How many rolls of film do you need to get developed?
1 to be developed, 2 in cameras.

19. Favourite age you have been so far?
31 has been quite good, although 32 is looking interesting.

20. Your enemy?
Evil kittens (there aren’t any, all kittens are cute). Oh, and Time.

21. What is your current desktop picture?
Brendadada’s ‘towering’

22. What was the last thing you said to someone?
I’ll get that application working on a mobile device emulator then shall I?

23. If you had to choose between a million bucks or to be able to fly, which would you choose?
With a million bucks you could by a jet pack!

24. Do you like someone?
Almost everyone – a fault?

25. The last song you listened to?
Kerrang TV last night… Green Day?

Flickr.Net API and Medium Trust

I’ve had a few requests to get the FlickrNet API Library working in Medium Trust mode under ASP.Net, so I thought I’d look at it. Here lies the tales and tribulations that such a journey took.

What is ‘Medium Trust’?

Good darn question. I wasn’t actually sure until I looked it up. Basically its a more secure environment for ASP.Net, often in which you might find yourself if using ASP.Net hosting on a shared server. It means you can’t iterate through all the directories on the server and read a load of files, and it also has other restrictions, which I’ll get to in a bit. (Read this for more details on the permission levels for each type of built in trust level.)

Where To Start

First thing is first, put a test web site into ‘Medium Trust’ mode.

Add the line to your system.web of either your applications web.config (for testing) or your machine.config.

<trust level=”Medium” />

Easy so far.

The only thing is this just gave me a generic “SystemException: Security error” which was no help whatsoever.

What Next?

Turns out that if I took OUT the [assembly:AssemblyKeyFile()] attribute from the AssemblyInfo.cs file it got rid of the above error, and gave me another one. But at least this new one has a line number associated with it! I still haven’t managed to find a reason for this, but for now the FlickrNet assembly isn’t signed. I shall probably have to start distributing a signed and a none signed version if I can’t find an answer to this one.

(Update: Found out what the problem was here. When in Medium Trust mode the web application is considered a ‘partial trust’ application and by default code libraries do not allow partial trust applications to call them. I’ve added the AllowPartialTrustCallersAttribute() to the assembly and this is now working correctly. Hurray. See here for blog post)

File IO Errors

The main effect that Medium Trust has is on file access. You are not allowed to browse directory or create files outside of your own virtual web directory. Therefore I was obviously going to have to look in further detail at the caching infrastructure, specifically where it created these files.

I made all these changes (eventually), as well as adding the ability to specify the cache location from within the configuration file (and then added the ability to specify it at runtime as well, which took a bit more work).

Unmanaged Code

I didn’t have any unmanaged code in the API, or so I thought. Then I came across a bit of code which called the Marshal.GetHRForException method. This converts a .Net Exception into a HRESULT integer, i.e. an old school Win32 error code. The code in question was used to distinguish a file locked error from other types of IOExceptions. And hopefully unsuprisingly (from the description I’ve just given you) you should be able to tell that this method is effectively a call out to unmanaged code. I’ve yet to work out a replacement for this code so at the moment its commented out.

Miscellaneous others

Other various things I had to work around was access to System.Net.WebProxy.GetDefaultProxy() static method, which throws an exception if called in Medium Trust. So I wrapped a try catch around this to catch that error. Then it turned out you cannot assign “null” to a HttpWebRequest.Proxy property if you are running in Medium Trust as well. Strange I though, but thats fine too (who wants to assign null to it anytime!)

The Final Stumbling Block – Shot in the Head?

Finally, everything compiled and I could create an actual Flickr class instance successfully. Then came the time to perform a quick test to Flickr.

Bam – turns out that any WebRequest to any web site (apart from the local web server) is blocked under Medium Trust. What? How is anyone ever going to use the FlickrNet API library under these scenarioes.

There is a Code Access Security document that is included with DotnetNuke that says the following code in your web.config will fix this particular problems:

<trust level=” ” originUrl=”http://www.flickr.com/*” />

The document also details ways to create a custom medium trust level. So if you are working in a modified medium trust envorinment you probably wont have this problem. You’ll either have to ask your hosting provider or just play around to see what works.

As an aside, if working in the above Medium environment then picture downloading will not work, as pictures are hosted at http://static.flickr.com and not http://www.flickr.com. But then it you’re working on a web app you probably just want to output the URL to the web page for the user’s browser to download anyway.

Conclusion

It has been a fun run around the .Net Framework for this one little feature, so I hope you all appreciate the work :)

[tags]microsoft, security, asp.net, .net, web, flickrapi[/tags]