Tuesday, December 16, 2008

Application Architecture Guide 2.0 from Patterns & Practices

The Microsoft's Patterns and Practices group continue their great work on the Application Architecture Guide. You should definitely check out the newly released Beta 2 version.

http://www.codeplex.com/AppArchGuide

Wednesday, November 26, 2008

log4net and appenders for different levels

The log4net uses different logging levels, ALL, DEBUG, INFO, WARN, ERROR and FATAL.

What I wanted to have is to log debugging, information and warnings into an appender and errors and fatal errors to another appender.

Here's how to define two appenders for different levels:

   1: <?xml version="1.0" standalone="yes"?>
   2: <log4net>
   3:     <appender name="RSLogFileAppenderInfo" type="log4net.Appender.RollingFileAppender">
   4:         <file value="PATHTOFILE\info.log" />
   5:         <appendToFile value="true" />
   6:         <rollingStyle value="Size" />
   7:         <filter type="log4net.Filter.LevelRangeFilter">
   8:             <acceptOnMatch value="true" />
   9:             <levelMin value="DEBUG" />
  10:             <levelMax value="WARN" />
  11:         </filter>
  12:         <maxSizeRollBackups value="10" />
  13:         <maximumFileSize value="10MB" />
  14:         <staticLogFileName value="true" />
  15:         <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  16:         <layout type="log4net.Layout.PatternLayout">
  17:             <conversionPattern value="%newline%date [%thread] %-5level - %message" />
  18:         </layout>
  19:     </appender>
  20:     <appender name="RSLogFileAppenderFatal" type="log4net.Appender.RollingFileAppender">
  21:         <file value="PATHTOFILE\error.log" />
  22:         <appendToFile value="true" />
  23:         <rollingStyle value="Size" />
  24:         <filter type="log4net.Filter.LevelRangeFilter">
  25:             <acceptOnMatch value="true" />
  26:             <levelMin value="ERROR" />
  27:             <levelMax value="FATAL" />
  28:         </filter>
  29:         <maxSizeRollBackups value="10" />
  30:         <maximumFileSize value="10MB" />
  31:         <staticLogFileName value="true" />
  32:         <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  33:         <layout type="log4net.Layout.PatternLayout">
  34:             <conversionPattern value="%newline%date [%thread] %-5level - %message" />
  35:         </layout>
  36:     </appender>
  37:     <root>
  38:         <level value="ALL" />
  39:         <appender-ref ref="RSLogFileAppenderInfo" />
  40:         <appender-ref ref="RSLogFileAppenderFatal" />
  41:     </root>
  42: </log4net>

Note that there are two appenders with different filters, the former raning from DEBUG to WARN and the latter ranging from ERROR to FATAL.


The simple helper class I've found somewhere and use to simplify the logging:



   1: public class Log
   2: {
   3:     public static ILog For( object LoggedObject )
   4:     {
   5:         if ( LoggedObject != null )
   6:             return For( LoggedObject.GetType() );
   7:         else
   8:             return For( null );
   9:     }
  10:  
  11:     public static ILog For( Type ObjectType )
  12:     {
  13:         if ( ObjectType != null )
  14:             return LogManager.GetLogger( ObjectType );
  15:         else
  16:             return LogManager.GetLogger( string.Empty );
  17:     }
  18: }

To test different levels defined for these two appenders just call:



   1: class LogTest
   2: {
   3:   public void Test()
   4:   {
   5:       Log.For( this ).Fatal( "fatal" );
   6:       Log.For( this ).Error( "error" );
   7:       Log.For( this ).Warn ( "warn" );
   8:       Log.For( this ).Info ( "info" );
   9:       Log.For( this ).Debug( "debug" );
  10:   }
  11: }
  12: ...
  13: LogTest test = new LogTest();
  14: test.Test();

Note that logs with different levels are correctly directed to appropriate appenders.

Thursday, September 18, 2008

ASPNET user account folder and files

Did you ever thought about the folder and files created by Windows XP for the ASPNET user account?

"The ASPNET user account is an internal account and no one is able to log using this identity"

This is the most common answer for my question. And yes, the answer is true. At least it touches the most important part. But there's more.

You see, since Windows XP invokes ASP.NET processes using ASPNET account, it also automatically creates a user folder with typical structure (Application Data, Cookies, My Documents etc.) for this account.

And here comes the strange part: the folder structure for the ASPNET user account is created under "Documents and Settings" but instead of username as a folder name, the machinename/username is used.

Suppose now that your machine's name is XYZ and you use a user account named XYZ. Guess what - you'll see ASPNET user local folder inside your own user folder.

Something tells me that this is not quite right. I should not be able to peek into other user's files but since the folder sits inside my own local folder, I am able to browse it with no restrictions.

I would say that this is rather insecure, no matter if it can be easily misused or not.

Tuesday, September 9, 2008

The "cookie" property of mshtml.IHtmlDocument2 does not work anymore

In one of our applications we host the Internet Explorer ActiveX control in a window and manually create parameters for navigation (including the body and http headers). To correctly inject the ASP.NET session id cookie into such requests (the page we navigate to comes from the ASP.NET server) we have to be able to somehow inject the ASP.NET_SessionId cookie into the navigation context.

The application was written two (or three) years ago, and we've been injecting the cookie using the cookie property of the mshtml.IHtmlDocument2 interface.

/* we've retrieving the reference to the document */
mshtml.IHtmlDocument2 doc = ...; 
 
/* and we've been setting the aspnet session id cookie */
doc.cookie = ...;

This worked like a charm, up to this year - the application is heavily used but only in september (it's responsible for gathering and processing some data available in september and then the data is passed to another appliaction) and this year we've been surprised to see that the cookie property does not work (at least in Internet Explorer 7). Both accessors (set and get) seem to have no effect and you always get the null value from the getter.


Pretty annoying. I guess it's somehow related to security issues but surely it broke the backward compatibility, at least for us.


The solution is to use another method which can inject cookies into the http processing chain:



/* injects cookies into the http processing for
   current process */
[DllImport("wininet.dll")]
public static extern bool InternetSetCookie( 
    [MarshalAs(UnmanagedType.LPStr)]
    string Url,
    [MarshalAs(UnmanagedType.LPStr)]
    string CookieName,
    [MarshalAs(UnmanagedType.LPStr)]
    string CookieData
    );
 
...
    /* first inject the cookie */
    InternetSetCookie(
        "url",
        "ASP.NET_SessionId",
        "theaspnetcookievalue_nomatterhowyougetit" );
 
    /* then navigate */
    webBrowser1.Navigate( "url" );
  
    /* the cookie is correctly set for the navigation.
       although you STILL cannot see it using mshtml.IHtmlDocument2.cookie,
       the http debugger reveals that it's really there 
    */

Wednesday, September 3, 2008

SmtpClient exception - An invalid character was found in the mail header

This hit me today as I tried to move a configuration section to another configuration file. It seems that the SmtpClient class does not like non-english letters in email header.

MailMessage mail = new MailMessage();
 
mail.From = new MailAddress( 
    "noreply@noreply.com", 
    "ąęłóżśńć", // <- the cause of the exception
    Encoding.UTF8 ); 

What's really scary is that the code above actually works - the exception occured only when the subject name was read from the configuration file!


After an intense struggle, I've found the solution. Just add



mail.SubjectEncoding = Encoding.UTF8;
somewhere around. It works, at least for me.

Wednesday, August 27, 2008

Remote desktop to another OS and back again

As you surely know, XP is a single-user OS which means that there can be only a single user logged-in at one time. If the administrator logs in from a remote machine using the remote desktop tool, the current user is logged off and a message shows up saying "this machine is currently in use [...]"

If you have a spare 3 minutes, try to reproduce following steps:

  1. Log into a non-virtual Windows XP
  2. Use Virtual PC to boot a virtual Windows XP and log into it
  3. Use remote desktop in the virtual Windows XP to log back to the non-virtual Windows XP (which hosts the virtual one)

What happens to your non-virtual Windows session after you log into it from the virtual machine? Will the "this machine is currently in use" message show up?

Well, if you do it using two XPs, the host screen goes black and the machine stops responding. The non-virtual XP is controlled "from the inside".

What's great is that the non-virtual machine still runs - you can access all it's services, the application server, the database server and others run perfectly. You just cannot control the machine anymore using the keyboard.

I belive that there are few other interesting options to continue this experiment.

For example, can you regain the control of the OS by accessing it with the remote desktop from yet another machine? What happens when the Windows Server 2003 is used instead Windows XP?

Thursday, August 14, 2008

Lightweight Service Bus frameworks

The application environment I work on needs an architectural fundament capable of a loose-coupled integration. There's a reference handbook on integration patterns, the "Enterprise Integration Patterns" by Hohpe/Woolf (also take a look at the "Integration Patterns" book from Microsoft Patterns & Practices group).

 

Anyway, there are four major integration patterns:

  • File Sharing - where application share data by exchaning files
  • Shared Database - where applications are build around a single database
  • Remote Procedure Calls - where applications expose their data through remote interfaces
  • Messaging - where applications exchange messages with the help of a centralized component (called a "Service Broker" when the messages are processed synchronously or the "Enterprise Service Bus" when a messaging component is used to store and handle messages) which acts as a backbone of the application environment


[http://msdn.microsoft.com/en-us/library/aa475433.aspx]

One of the most interesting aspects of an ESB implementation is handling various MEPs (Message Exchange Patterns: synchronous, asynchronous, send-and-forget, publish-subscribe). The latter, publish/subscribe seems the most attractive option to integrate an environment consisting of different components.

There are plenty of fully fledged ESB implementations but the core of a service bus can be built using much a simple environment based solely on a messaging component like MSMQ or IBM Webshpere MQ. And since the "low level" integration with a messaging component could be painful, we need lightweight service bus frameworks just to wrap unnecessary calls into friendly APIs.

After some research, I've found three such lightweight service bus frameworks for the .NET platform:

  • nServiceBus - uses MSMQ as messaging component and Spring.NET as the IoC container. Supports so called "sagas" which are just long-running workflows as well as the scalability over a large enterprise system.
  • Simple Service Bus - built on top of the nServiceBus. At first look seems like the porting of few nServiceBus features is not completed at the moment. Uses the Castle.Microkernel as the IoC container.
  • MassTransit  - seems to take a more universal approach, supports MSMQ / ActiveMQ as messaging components and  different IoC containers. There's not much to evaluate since the useful versions cannot be downloaded at the moment.

I hope to write more on the topic soon since this is not only the top-priority task I work on at the moment but also, in the same time, it's just just quite fun to play with.