Thursday, August 07, 2008

Writing to Event log with ASP.Net and Enterprise Library

I recently had a problem writing to the event log from my application.  I had configured the application to use the Logging Application Block from the 3.1 version of the Microsoft Enterprise Library and followed the walk-through in the documentation to log exceptions to the Application Log, but nothing was appearing.

I had amended the code so that it always through an exception on clicking a test button, and stepping through the code, I could see that Log.Write was being called and no error was raised from this call.

A quick search on Google suggested that this was a permissions issue for the ASP.Net account, so I added the following key:

HKLM\System\CurrentControlSet\Services\EventLog\Application\<my app name>

I granted full control to the ASPNET account to this key and amended the source property of my trace listener to <my app name> using the Enterprise Library Configuration Manager.

I now get events recorded in the Application log and their source is set to <my app name> so it is easy to filter them for the events I want.

Tuesday, March 04, 2008

Code Access Security For Child AppDomain

I recently configured an existing application to use ClickOnce deployment. All seemed to go well until I tried to run the application, whereupon it threw an exception on a line that had previously been working perfectly:

AppDomain childDomain;

childDomain = AppDomain.CreateDomain(ChildAppDomainName);

//Following line throws exception

childDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

The exception was:

{"Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."}

The application was configured to run as FullTrust. To cut a long and frustrating story short, It turned out that the security settings were not being propagated to the child AppDomain. I changed the code to the following:

AppDomain childDomain;

Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;

Evidence childEvidence = new Evidence(baseEvidence);

childDomain = AppDomain.CreateDomain(ChildAppDomainName, childEvidence, AppDomain.CurrentDomain.SetupInformation);

This allowed the child AppDomain to inherit the settings from the parent domain and everything worked as expected once more.

Friday, February 29, 2008

Check for existence of DB objects

Some queries I find useful to add before the Create/Alter Statements in SQL Server.

--Constraints
IF EXISTS(
SELECT NULL FROM INFORMATION_SCHEMA.Constraint_column_usage
where table_name ='MyTable'
and constraint_name = 'FK_COl1')

--Check existence of stored_proc
IF EXISTS (SELECT NULL
FROM information_schema.routines r
WHERE r.specific_schema = 'dbo'
AND r.specific_name = 'MyProc')
--Check column exists
IF NOT EXISTS (
SELECT NULL
FROM information_schema.columns
where column_name='Col1'
AND table_name ='MyTable'
and table_schema='dbo')

--Check for description
IF not EXISTS(
SELECT NULL from sys.all_objects ao
INNER JOIN sys.extended_properties ep on ao.object_id = ep.major_id and ep.name ='MS_Description'
where ao.name ='FK_key_name')



Friday, January 25, 2008

Disable full screen video on Dual monitor setup

I was recently trying to debug an application that hosts a windows media player control and I wanted the application to run full screen on my primary display with my Visual Studio IDE on the secondary display. Annoyingly, although the application appeared on my primary display as expected, the secondary display had a full screen video output. There appears to be nothing in the media player API to prevent this. It turns out that you need to change your driver display settings to prevent this. These settings are driver specific, for mine I needed to do:
  1. Bring up display properties from control panel
  2. Click Settings
  3. Click Advanced
  4. Click Quadro NVS 120 M (Nvidia specific)
  5. Click Start NVidia control panel
  6. Click Video and Television
  7. Click Modify full screen options
  8. Set "When watching video content" to Only show it in my viewing application

Thursday, January 03, 2008

Soap Extensions in web forms client

On my current project, I need to consume a web service from a Windows Client, the web service is decorated with a custom attribute on the server side that causes the SOAP message to be compressed, this code was written by another developer but I think he got it from here. In order to uncompress it on the client, I needed to reference the assembly that contained the SoapExtension in my client and add the following section to the app.config:

<system.web>

<webServices>

<soapExtensionTypes>

<add type="myCompany.myProject.WebServices.SoapExtensions.CompressionExtension,myCompany.myProject.CompressionExtensionLib"

priority="3" group="High" />

soapExtensionTypes>

webServices>

system.web>


The first part of the type is the fully qualified class name, the second part is the namespace.