Skip to main content

Posts

Showing posts from 2008

How to access the IIS Metabase programmatically, IIS metabase

The IIS Metabase is XML representation of the IIS instance, it's actually an XML file stored in IIS. It can be access using apps like  MetaEdit   or programmatically using the Active Directory through a class  System.DirectoryServices.DirectoryEntry. using System.Collections.Generic; using System.Text; using System; using System.Data; using System.Configuration; using System.DirectoryServices; using System.Web.Configuration; namespace IISMetabase { class IISMetabase { private string _machineName = "localhost"; private System.Collections.IDictionary _applications;//collection of websites private Configuration _config; public IISMetabase() { _applications = new System.Collections.Hashtable(); } public System.Collections.IDictionary GetApplications() { DirectoryEntry webentry = new DirectoryEntry(); String path = "IIS://" + _machineName + "/W3SVC/1/ROOT"; webentry.Path = path; Boolean exists = false; try { exists = DirectoryEntry.Exists(path); } catch (Sy...

ActiveX Stopwatch Timer Control

Here's an example of a StopWatch control implemented in Csharp and used as a control on a web client. The control needs to be register on the clients machine then added to the clients web application (in html) and then called from Javascript. <OBJECT id="activeXTimerControl" name="activeXTimerControl"  classid="clsid:4F3B57CE-D9CA-41c3-ACBD-EBB2380990E7"  style="width: 504px; height: 182px"  codebase="Bin/ActiveXTimerControl.dll" class="ActiveXTimerControl.TimerControl"> </OBJECT> To start the Stopwatch call Start() and to end call End() then call Time() to return the result as a string or use the AddTimeToGrid to add the time to a DataGrid Control (supplied by the StopWatch control) or call AddTimeToLabel which adds the time to a textbox. activeXTimerControl.Start(); ///do someting activeXTimerControl.Stop(); var time = activeXTimerControl....

AcitveX Controls - how to create one and call with Javascript

One way to get a .NET object to run in a browser is to create a User Control in .NET. You can then call this User Control from Javascript in your client. There's nothing magical about this except, it only works on IE and there are a few small bits that you must implement in order for your User Control to be picked up, the errors you get aren't very helpful so I recommend following these steps (Some of these I'm not sure if they're required but they've worked for me). Create a Class Project in Visual Studio 2005. Add a User Control to the project. In the User Control Designer add a TextBox Control Add a Property to your class to set the Text of the TextBox you've just added. public void AddText(string time) { if (_timerTextBox == null) { _timerTextBox = new System.Windows.Forms.TextBox(); _timerTextBox.Text = " Time (msec)"; } _timerTextBox.Text += ("\r\n" ...

Localization and Globalization

Using Localization from Code ASP.NET provides all the Localization you need by just adding .RESX files to your Web App in the right location but sometimes you want to invoke the Localization yourself. To do this you'll use the System.Resources.ResourceManager class. This allows you to get resources by simply supplying the name of the resource and the Culture but to setup the resource itself is a bit trickier (in Visual Studio 2005) as some of the files required have been removed from Visual Studio's Templates. Because of this you'll need to use some tools to create the Resource (and optionally the Assembly to store it know as Resource Assemblies or Satellite Assemblies). There are 3 steps to creating resources for use with ResourceManager: 1. Write a text file or .RESX file to hold your resources. 2. Compile the resources in step 1 into a .RESOURCES file. 3. Compile the .RESOURCES file in step 2 into a Satellite Assembly. 4. Locate the Satellite Assembly from step 3 ...

VSPackage - moving the assembly out of the GAC

Some tips when moving your VS Package assembly out of the GAC. First of all in order for Visual Studio to find the Package in it's new location you need to tell it where to find it, this is done in the Registry with the CodeBase key. This is done in the Registry at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Packages\{1237c0fa-4f6e-43c5-9ee4-a5938847c54a}] "Codebase"="C:\\Program Files\\MyCompany\\MyProduct\\MyPackageAssembly.dll" This is also the case for other classes which have GUIDs in your package. Use regasm with the /codebase switch to tell you which classes may need an additional registry entry.

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 }