Skip to main content

Posts

Automated update of AssemblyVersion and AssemblyFileVersion

Like so many others I wanted to update the Version of all of my assemblies with one version value but I could not find the solution I wanted, hence I've written my own, see the source code below. This implementation fits my own problem, a build script which gets all our codebase sourcecode from SourceSafe, builds all the projects and then labels all the sourecode with a single label passed in by the builder at command-line. This implementation takes either 1 or 2 arguments. If only 1 is supplied it will do a check for a valid Version value i.e. the version must be of the format 1.0.0.0 and the third value must be less than 65535 (This is a .NET requirement, the assembly would not build if the number is above 65535, see here for more ). If 2 arguments are supplied it will do all that step 1 does and also will set the version to AssemblyVersion and AssemblyFileVersion in all AssemblyInfo.cs files under the directory specified (and it's subdirectories). e.g. here's how I cal...

Configuration namespace and reading settings from the Configuration file,app.config,web.config

This one always catches me out. There are a few different ways to read settings in from a .config file. The confusion for me occurs when reading from certain sections and from config files other than ones for the currently running application. The easiest example is reading settings from the currently running application, from within it's own code. You can access the AppSettings Section <configuration><appSettings> <add key="hostname1" value="localhost"/><add key="hostname2" value="othername"/></appSettings></configuration> using: NameValueCollection appSettings = ConfigurationManager.AppSettings; foreach(string key in appSettings.AllKeys) Console.WriteLine(key + " " + appSettings[key]); So now you've all the keys in the AppSettings section i.e. "hostname1" and "hostname" and all their values accessed with appSettings[key] . Note: First you must add a reference to 'Sys...

ASP.NET HttpHandler how to

HttpHandlers are class which extend HttpHandler and which IIS recognises as the classes to use to handle errors etc. The HttpHandler class is usually stored inside a .ashx file. You can create your on .ashx and place it in you web app root dir, it will be entered whenever a request comes in from the client. If you do not want to show your HttpHandler file code in your web app then you can hide it in a dll see details below. How to hide the . ashx with a HttpHandler I'll describe here how to create a handler for a file with the extension .ashx, it's coincidental I require a HttpHandler in my web.config in order to alias my .ashx file which is too a HttpHandler. Note: Before you can compile a HttpHandler into a dll you must remove the directive at the start of the .ashx file e.g. delete this line <%@ WebHandler Language="C#" Class="MyWebHandler" %> If you fail to do this your .ashx code will not appear in your dll and you will not be able t...

Remoting, Remote Computing

Remote Computing http://del.icio.us/rss/learnerplates/remoting http://del.icio.us/search/?fr=del_icio_us&p=remoting&type=user There are 2 main ways in .NET to invoke a method and/or create an instance of an object on machine other than the one your 1. Webservices 2. .NET Remoting. 1. Webservices allow the client to communicate with the server be emitting an receiving XML, these XML messages are first wrapped on both the client and server side into SOAP envelopes..NET Remoting on the other hand allows a remote client to invoke a method through Proxy classes. 2. .NET Remoting You can access remote objects in 2 ways, by reference and by value. The first manipulates the actual original object on the server(the objects class must be MarshalByRefObject) , the second a copy of the original object is used which means any changes made will only effect the local copy, copy on the client, of the object (the objects class must be Serializable). The communication between Server and Client i...

MVC in ASP.NET

MVC - Model-View-Controller is a sort of design pattern used to develop applications in an uncoupled manner i.e. seperate the data (Model) from the User interface (View) and the business logic (Controller). I first came across this with MFC applications, it was well known design pattern when developing standalone applications now it appears to be the latest craze in ASP.NET 3.5 applications! In this post I'll try and find out why and what is the significance and benefits to us, developers. MVC (scott gu) Example Building a Simple Blog engine using MVC in ASP.NET (aspalliance.com)

WebServices

Webservices are exposed methods which can be accessed through a uri e.g.your code is contained in a .asmx file. The .asmx file contains webmethods (ordinary methods with the [WebMethod] attribute). You place your .asmx file inside your webserver somewhere and these can be consumed directly by browsing to the uri or by consuming the webservice by adding it as a reference to your application and then accessing that reference. Webservices can also be created with Codebehind meaning the code itself is in another file, this results in the .asmx file just containing a Directive to the class name containing the webmethod, to use this webservice the codebehind dll must be placed within the websites bin directory. The WebService works by the Client and Server sending SOAP XML (SOAP is just the root element, it's something that Internet Explorer knows how to parse) up and down over HTTP. Both sides need to know how to create this SOAP XML, on the Client side this is done by a Proxy class whi...

Active Directory

Active Directory is the windows method to access all distributed devices such as files, printers. In .NET the Active Directory is accessed from System.DirectoryServices namespace. .NET uses Windows Active Directory Service Interfaces (ADSI) to interact with the distributed devices, there are 5 of these ADSIs; Path Windows NT version 5.0, Windows 2000, or Windows XP WinNT://path, Lightweight Directory Access Protocol (LDAP) ldap://path/ , Novell NetWare Directory Service NDS://path, Novell Netware 3.x NWCOMPAT://path, Internet Information Services (IIS) IIS://. e.g. DirectoryEntry webentry = new DirectoryEntry(); String path = "IIS://localhost/W3SVC/1/ROOT"; DirectoryEntries webSiteChildren = webentry.Children; foreach (DirectoryEntry website in webSiteChildren) { //can access metadata of each website on iis here }

MMC Snapin creation

MMC Snapin Creation There are 2 main versions of MMC available on Windows, MMC 2.0 (Windows 2000 and XP) and MMC 3.0 (Windows Server 2003). I'll mainly explain how to develop a Snap-in with MMC 3.0. You can upgrade MMC 2.0 to MMC 3.0 using this install supplied by Microsoft MMC 2.0 snapin: Download the Platform SDK, Windows® Server 2003 R2 Platform SDK Web Install, from http://www.microsoft.com/downloads/details.aspx?FamilyID=0baf2b35-c656-4969-ace8-e4c0c0716adb&DisplayLang=en Build an example from the Platform SDK install: C:\Program Files\Microsoft Platform SDK\Samples\SysMgmt\MMC\mmc2.0\simple>nmake MMC 3.0 snapin: How to Develop and MMC 3.0 Snap-in (msdn) How to create a Snap-in using MMC 3.0 (msdn) MMC Snap-ins for MMC 3.0 can be developed with the Windows SDK , for information on the snapin classes see msdn Management Console There is also an opensource .NET library at http://sourceforge.net/projects/mmclibrary The MMC 3.0 application installation is at http://www.m...

Handling SoapExceptions

SoapExceptions are thrown from WebServices when a problem occurs. The SoapException will occur whenever any type of Exception occurs in the WebServices code e.g. if an IOException occurs it will bubble up to the caller as a SoapException, unlike other Exceptions the origninal Exception is not easily extracted, for instance by using the InnerException property. Because of this you must handle the SoapException in the sender and the caller. Yuo must stuf your Originals details into a new instance of a SoapException and then throw it. The original Exception's details are contained in the Details property of the SoapException but you still have to do some work to extract it. Also remember that WebServices return XML, the SoapException is also returned as XML, so our extraction code will need to know how to identify the inner exception and how to extract it from the correct XML Element. One thing you may wish to do with the SoapException is extract the original Exceptions details. Here...

DynamicMethod, IL Code, CreateDelegate

In a previous post, dotNET - Under the Hood, IL Assembly , I briefly described how to write some IL Code to an Assembly, but what if at a later occasion you'll want to invoke these methods from the dll. This could be achieved by loading the assembly in an AppDomain and then invoking the methods within ???? Or you could use what are known as DynamicMethod s to invoke the methods. DynamicMethod is a class that also resides in the System.Reflection.Emit namespace. Why would you want to use DynamicMethod? it's useful when you want to load methods into memory or write methods in memory and invoke later. We can write IL code in memory in the exact same was as we wrote IL code earlier using the MethodBuilder. In my experience DynamicMethod is a bit tricky to get right especially when you try to invoke your Dynamic method. The problem I found was with the constructor parameters, what do they mean; There are 6 constructor overloads in all; half of these are associated with an class in...

Enumerator(s)

You've all used the foreach loop in .NET to get the values in a container. The foreach actually uses that objects Enumerator. In order to allow the use of the foreach on an object the class must implement the IEnumerable.GetEnumerator method which also means you have to implement the IEnumerable interface. Within the GetEnumerator override you need to return an instance of IEnumerator, so you need to create a class that implements the IEnumerator interface. see the code. This is useful if you class contains a member variable which is an or even a container. Let's say it's an array. When an instance of your class is create the array is also instantiated. But you don't want the array to be visible but you do want clients to be able to use a foreach on your class instance, this can be achieved by implementing the IEnumerable interface and overriding the GetEnumerator method. using System; using System.Collections.Generic; using System.Text; using System.Collections; namesp...

Security in Assemblies

Code Security is all about allowing and preventing code from running. The . NET Security Model works by the assemblies each having their own Evidence embedded in the Assembly by the Assembly writer. When the CLR loads the assembly it then reads and applies this Evidence to a Security model and this in turn returns Permissions , depending on what Permissions are returned will determine is the assembly is permitted to execute or not. So it's Evidence in (on the assembly) -> Code Groups -> Permissions. Code Access Security (CAS) is the the mechanism used by .NET to manage all of this. It's function is to process assemblies and determine the runtime permissions they should have, e.g. should they code within a certain assembly be allowed to run or not. All assemblies have Evidence. As the CLR is loading the assembly it looks at this Evidence and processes it using the current machines .NET Code Group. Depending on where the assembly is being loaded from, the Evidence it has,...

dotNET - Under the Hood, IL Assembly, MSIL, ILGenerator, MethodBuilder

IL (Intermediate Language) is also referred to as MSIL (Microsoft Intermediate Language) or even ILAsm which is the same thing. IL is the code which your source code (C#, VB.NET etc) is compiled into. When you EXE or DLL is run these IL code is converted to a machine language particular to the present machine i.e. if the current machine being used to run the application is a Windows 2000 machine the IL will be converted into Windows instructions before running on the x86 processor (processor architecture used for Windows machines). When you're developing with .NET languages with a Development tool such as Visual Studio 2005 the compiler you use will generate the correct/valid IL for you, but what if you want to create your own IL for some reason! One reason could be that you've developed your own Language and would like it to run on .NET, in this case you'll need to write out some sort of Assembly, be it EXE or DLL, with IL code. To do this you can use some Classes availa...

Design Patterns - Visitor Pattern

The Visitor Pattern is a solution for a problem where you have many Objects, each Object is similar but you wish to extract information (such as a print statement) on each Object in a different way. E.g. You have a collection of Objects, each of your objects has different member variables and you want some way to call print this info. One way to do it would be to cycle through each object and Call some common method on each such as Print(), this ties the Object tightly to the printing or whatever you are doing, we want to loosly couple the printing or whatever to the object itself, to unbind the Printing from the actual Object we introduce 2 interfaces. The Visitor and the Visitable. The Visitor interface has a Visit(object) method (this is what would print the details). The Visitable interface has an Accept method, this takes the Visiting object as a parameter. The Object we want to print implements the Visitable interface, this has a callback to the implementor of the Visitor Interfa...

Installer (MSI) - Windows Installer general information

Aaron Stebner's Blog Common problems I've found while playing around with the creation of Windows installers is that if you break the uninstaller functionality it's very difficult to uninstall at all. There are a couple of ways to attempt to repair, this 1. Force overwrite of the installer with a known good installer and then uninstall. msiexec.exe /fvecmus 2. Repair the installer with a known good installer (Control Panel->Add/Remove Programs, locate you Applications installer and select "Click here for support Information", then select Repair from the resultant dialog, now point to a new location of a good installer) Then Remove. 3. Use a tool name MsiZip.exe, it's available with the MS Windows SDK , it cleans up the registry (the SDK installation takes quite a while). 4. A Windows Installer Cleanup Utility . The windows installer keeps a list in the OS of files installed by the installer, if you cannot uninstall because windows thinks one of these files ...