Posts Tagged ‘Coding’

More details on installing .NET 3.5 in Windows 2000

Wednesday, April 22nd, 2009

I’ve had a comment that my previous post explaining how to install .NET 3.5 in Windows 2000 wasn’t very clear, so I’ll try to clarify it a little…

First of all, let me explain how it works.  .NET is actualy 2 separate things.  There is the .NET runtime, which deals with interpreting the MSIL intermediate code and executing it.  There is also the .NET Framework, which are all the libraries containing the classes that you usually code with in .NET.

In theory, the .NET runtime can be used without the .NET framework (although you’d probably need a couple of DLLs to define things like the basic types).  You could use an entirely different framework which sits on top of the same runtime and is compiled to MSIL.  I’m not aware of any that exist though.

The really interesting bit is that there are actually only 3 versions of the .NET runtime – 1.0, 1.1 and 2.0.  1.0 and 1.1 are used for the corresponding framework versions, but 2.0 is used for running framework 2.0, 3.0 and 3.5.  

In fact, in terms of the framework, 3.0 is 2.0 plus some extra stuff, and 3.5 is 3.0 plus extra stuff.  The trick which I rely on is that you can install 2.0 in Windows 2000 and just add the extra stuff that you need. 

For my purposes, all I needed was support for Linq and Linq to SQL and a few other bits and bobs.  All this is available in .NET Assembly DLL files.  you just need to make sure that the assemblies are available to be loaded by the application you are running.  

You also need to make sure that you have any prerequisites for those assemblies, because a lot of them will access classes in other assemblies.  There are 2 ways of dealing with that.  You can either copy in the entire set of DLLs, or you can keep running your program and looking to see what assembly it can’t find when it throws an exception, copying that DLL in and re-running it until you don’t get any more exceptions.

There are 2 ways of making the assemblies available to your program.  You can either register them in the GAC so that they can be accessed from anywhere (I’ve not tried this and I probably wouldn’t recommend it – if you want to know how to do this, google installing an assembly in the GAC), or you can just copy them into the application folder that your EXE sits in (or bin folder on your ASP.NET website will probably work too).  This does make for a lot of files in that folder, but it works!

You should be able to find the .NET 3.5 assemblies in a folder on a PC that has .NET 3.5 installed under

Program Files\Reference Assemblies\Microsoft\Framework\v3.5 

nb. Make sure that you have Windows 2000 SP4 and .NET framework 2.0 SP1 installed on the Windows 2000 PC!  You might also need to install KB 835732 before .NET 2.0 SP1.

.NET 3.5 SP1 in Windows 2000

Monday, March 23rd, 2009

I’ve posted a couple of times earlier on .NET 3.5 in Windows 2000.  The .NET framework 3.5 sits on .NET 2.0 SP2, which is only available to install on Windows XP and onwards.  However, from experimentation, this does not mean that the same tricks don’t work.  As long as you have .NET 2.0 SP1 installed, you can get away with copying most of the DLLs in with no trouble.  It also seems to work with some of the newer ones.

Update on .NET 3.5 in Windows 2000

Monday, March 2nd, 2009

This has now been running fine for a couple of months and is being used on a day to day basis.  Not all functionality works perfectly out of the box, but most of the stuff I have tried can be worked around.  I’m using compilation on the fly using the VB.NET 3.5 compiler to make dynamic Linq to SQL queries, which I have needed to create a workaround using vbc.exe – the VB.NET compiler and passing in command line parameters.  I needed to copy in the required DLLs that it was complaining that it didn’t have, and a few extras as well that I threw in predicting that it might need them.  Let me know if you want more details – comment below.

More info on .NET 3.5 in Windows 2000

Thursday, September 18th, 2008

People have been requesting more information about whether my previously mentioned trick to get .NET 3.5 functionality in Windows 2000 works.  So far, it mostly seems to be a success although the software is still not being used on a large scale, but I have only found 1 real problem… When I made a WCF service client to access a web service, VS.NET created a section in the app.config file.  This caused an exception when I loaded the program on the Windows 2000 PCs.  However, I removed the section from the config file and hard coded the configuration info for the WCF client and it worked fine!

Microsoft .NET Framework 3.5 in Windows 2000 Test 1

Wednesday, March 26th, 2008

I mentioned earlier how to install .NET framework 3.5  in Windows 2000.  I have been testing this on 7 Windows 2000 PCs, and I have successfully managed to get them to do a simple Linq to SQL query with the method that I mentioned.  I can’t speak for WPF or anything, but I am currently only interested in Linq.  I will post more in future when I have the live system in place.

On Reflection Part 2: GetType

Monday, March 24th, 2008
This entry is part of a series, On Reflection»

In order to use reflection in .NET, you need to get an object of class Type.  I will explain how to use the Type object later, but first, you need to be able to get it.

There are 3 ways to get a Type in .NET.

  1. VB.NET has a GetType keyword which takes a class as a parameter.  eg. you can writeDim T as Type = GetType(String)

    This is not really a function, because you are passing in a class, not an actual value, and not even the name of a class!  C# has a similar keyword, which I think is called typeof.

  2. All .NET objects inherit from Object.  Object has a function defined on it called GetType, so all you need to do is eg.Dim p as New Person()
    Dim T as Type = p.GetType()

    Note that if you have an Interface reference you may need to cast it using the CType operator in order to access the GetType function.  You should be able to cast any .NET object to Object.

  3. You can get a Type by name from an Assembly.  I will post details on this in a later post on Assemblies.

Structured Exception Handling in .NET Part 4: Re-Throwing Exceptions

Wednesday, March 19th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

Sometimes when you have caught an Exception, you want to do a bit of processing and then throw the Exception on again, or perhaps check the Message property to decide if you can handle it, and if you can’t then you throw it again. This is actually quite easy – you just use Throw, with no parameters.

eg.

Try
   DoSomething()
Catch Ex as Exception
   If Ex.Message="Connection Failed"
      Conn.Reconnect()
   Else
      Throw
   End If
End Try

One thing that you really don’t want to do is to Throw Ex

.NET Compilation Part 1: Profiles

Tuesday, March 18th, 2008
This entry is part of a series, .NET Compilation»

You may have noticed that in Visual Studio, most projects start off with Debug and Release profiles. This is used to control the way that the code is compiled.  By default, Debug mode creates a pdb file and compiles the code to allow it to be used.  A pdb file contains information which will help you debug the program, such as information about how the source code maps to the compiled code.  This means that eg. when you get an Exception, the StackTrace property will give you information including line numbers.

On the other hand, a Release profile is much more optimised, both in terms of file size and code speed.  However, it is entirely up to you whether you ship a Debug application.  I wouldn’t recommend it for a large number of users, but for a constantly changing application in a small office, full stack traces with line numbers can be useful to find bugs that are difficult to trace.

Structured Exception Handling in .NET Part 3: The Exception Class

Tuesday, March 18th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

The base .NET Exception class has several useful properties to retrieve data about what went happened: -

Message
This is probably the most used one.  It is simply a textual message explaining some detail about the exception.

InnerException
Sometimes you’ll find that some piece of code lower down the line has caught an exception and wanted to add some information itself, so what it does is to create a new Exception, and put the one that it caught into the InnerException property.  Sometimes you really need to access this in order to find out what happened, because the actual exception that you caught is very non-specific about what happened.  The other thing to bear in mind is that the InnerException can also have an InnerException and so on.

StackTrace
This returns a string that contains a stack trace that you can use to see how the code got to this point.  It can be very useful for debugging.

Source
This returns a string that tells you the name of what caused the error.  I usually don’t tend to find this particularly useful

TargetSite
This returns a reflection object representing the method that the exception occurred within.  I’ve not actually found any use for this yet, but I can see how it could be useful.

Data
This is a Dictionary of any extra pieces of information you might need to know about the Exception.

Structured Exception Handling in .NET Part 2: Multiple Catch Clauses and Different Exception Types

Monday, March 17th, 2008
This entry is part of a series, Structured Exception Handling in .NET»

Not everyone realises that you can have multiple Catch clauses per Try clause. The point in this is that you can catch different types of Exception.

You may have a function which can throw several different types of Exception. You can catch specific types of Exception separately and deal with each of them differently.

For example, you may wish to do something like this (nb. these Exception classes are made up – they aren’t in the .NET framework)…

Try
   DBConn.ExecQuery(Query)
Catch ex as ForeignKeyException 'inherits from KeyException
   MsgBox("Sorry -  this query would cause a conflict with another table")
Catch ex as KeyException
   MsgBox("Sorry - this data is not valid")
Catch ex as ConnectionException
   MsgBox("The database connection has gone down.  Please reconnect and try again")
Catch ex as Exception
   MsgBox("Something else went wrong - " &  ex.Message)
End Try

Note that catch clauses will catch any object of inherited Exception classes as well as the class that you specify, so if you need to catch a more specific type of Exception, you need to put it before a less specific type, which is why ForeignKeyException comes before KeyException, and everything comes before Exception.