#henrik

development and technology stuff

ASP.NET and log4net

My favorite way to configure log4net for ASP.NET applications was via the Global.asax way. Just use the single line:

log4net.Config.XmlConfigurator.Configure();

This worked for the VS2005 style webpage projects. Since the change in VS2008 to web-application projects instead of webpage projects the Global.asax trick does not work any more. For web-application projects one has to configure log4net via an assembly directive in AssemblyInfo.cs. When put there everything works as expected:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Thanks for the config hints to haacked!

Filed under  //   Development   c#   log4net  
Posted July 9, 2009

SPListItem - SPFieldLookupValue

If I use the SharePoint object-model to set items my "normal" approach is:

SPListItem item;
item["fieldName"] = "value";

This works as expected until the field in question is a lookup-field. Using the approach mentioned above I always got an exception. The reason for this is, that a lookup values always contain a key and value ==> 1#;Value So the correct code would look like this:

int lookupId;
string lookupValue;
item["fieldName"] = new SPFieldLookupValue(lookupId, lookupValue);

Thanks to Sridhar's Blog.

Filed under  //   Development   SharePoint   Sharepoint Development   c#   object-model  
Posted May 21, 2009

NHibernate Transactions

For my ASP.NET project I'm using the nice NHibernate template from Billy McCafferty. Work's exactly the way I want. But I noticed one thing which isn't that good after all. A transaction rollback does not work. One ends up with all the changes persisted to the database. The problem was, that the NHibernate example uses a session.flush() even after a rollback - which is not the recommended by the NHibernate documentation. The comments on the CodeProject page were quite helpful, as well as a discussion at stackoverflow. The solutiion for this problem: Just don't flush after rollback.

public void RollbackTransaction() {
    ITransaction transaction = ContextTransaction;
    try {
        if (HasOpenTransaction()) {
            transaction.Rollback();
        }
        ContextTransaction = null;
    }
    finally {
        CloseSession(false);
    }
}

public void CloseSession(bool flush)
{
    ISession session = ContextSession;
    if (session != null && session.IsOpen)
    {
        if(flush)
            session.Flush();
        session.Close();
    }
    ContextSession = null;
}

Filed under  //   Development   NHibernate   c#  

NHibernate Transformers

NHibernate is awesome. I'm using it for the first time in a ASP.NET project. Today I found a nice feature in combination with native SQL queries. One can return unmanaged entities for a native SQL query - with automatic mapping - excellent! See the documentation on using Transformers: (NHibernate native SQL queries)

sess.CreateSQLQuery("SELECT name, birthdate FROM CATS")
        .SetResultTransformer(Transformers.AliasToBean(typeof(CatDTO)))

public class CatDTO {
    public string Name {
        get {}
        set {}
    }
    public DateTime Birthdate {
        get {}
        set {}
    }
}

Just create Properties using the same name as the columns of the query in the POCO and you're done. And the extra nice thing. Exactly the same approach in the original Java version (Hibernate native SQL queries).

Filed under  //   Development   NHibernate   Other   c#  

MOSS: SAP incoming Email

For a MOSS/SAP project we needed a simple way to exchange SAP documents (order-confirmation, invoices, ...) with MOSS. The incoming email feature came in handy. Just define an email-address for the desired doclib and tell SAP to send emails including the documents as PDF attachments - sounds easy. The doclib was configured just to store the attachments and remove the original emails. After activation of this simple interface nothing happend. No attachments could be found in the doclib. First I tought that the emails were miss-routed, but the mail-server said everything was ok. I enabled to keep the original emails on the doclib. Now a number of emails were saved, but no attachment anyway. Strange! Saving the .eml messages and opening them with Outlook, Thunderbird, ... did work - the attachments (PDFs) were included - or using vim - showed a plain MIME-message with base64 encoded attachment. But there seemed to be something wrong with the MIME-message because MOSS did not recognise the attachment. I decided to parse the message myself - using a MIME library for .NET. Google found a codeproject site with a rather good example including a complete MIME library. The Lumisoft.Net library is really easy to use. I created a small test-program to find out what's wrong with the SAP MIME-message. Soon I found the problem. It seems that the SAP MIME-message includes a number of NULL-bytes which causes the Base64 decoder to throw an Exception.

Mime message = Mime.Parse(@"..\..\SAP_MIME.eml");
byte[] bytes = message.MainEntity.DataEncoded;

// without trim decode fails with exception
Encoding enc = Encoding.Default;
string myString = enc.GetString(bytes);
myString = myString.Trim('');
byte[] decoded = Base64DecodeString(myString);

After removing the trailing Null-bytes the bas64-encoded attachment could be extracted. To process all the files in the doclib I needed a timer-job anyway so I added the simple MIME-parsing/cleanup code to the timer-job. Everyhting works like a charm know. SAP and Microsoft - some times I think those two companies don't want things to be easy.

Filed under  //   SAP   Sharepoint Development   c#   moss