General

Transform System.Drawing.ContentAlignment property to System.Drawing. StringFormat Alignment property

Recently we had the need to transform System.Drawing.ContentAlignment property to System.Drawing. Stringformat alignment property for creating a Graphic object with text drawn on it inside a given rectangle.  Assuming that you want the text printed from left to right, you can use the ContentAlignment Enum values to obtain the desired StringAlignment Enum value.  For example:
   1: public StringFormat TransformProperty(ContentAlignment alignment)

   2:         {

   3:             StringFormat myStringFromat  = new StringFormat();

   4:             switch(alignment)

   5:             {

   6:                 case ContentAlignment.MiddleLeft:

   7:                     myStringFromat.Alignment = StringAlignment.Near;

   8:                     break;

   9:                 case ContentAlignment.MiddleRight:

  10:                     myStringFromat.Alignment = StringAlignment.Far;

  11:                     break;

  12:                 default:

  13:                     myStringFromat.Alignment = StringAlignment.Center;

  14:                     break;

  15:             }

  16:             return myStringFromat;

  17:         }

Craig

General

Comments (0)

Permalink

Adding SQLite to a Windows Mobile Application

Using SQLite in a Windows Mobile Application provides a simple way to add pre-populated data for consumption by the application.  There are a couple of configuration considerations when adding the database to the project.

1. Adding reference to SQLite.dll. After installing SQLite there will be a Compact Framework folder in the following path :C:\ProgramFiles\SQLite.NET\bin.  This folder contains the System.Data.SQLite.dll that needs to be referenced.

2 The same path will also contain the dll which Windows Mobile will need to invoke the methods contained in the SQLite.dll.  The following file needs to be added to the project which contains the main executable,  SQLite.Interop.065.DLL.

Once the interop dll is in the project you can start using SQLite in the Mobile Application just as you would for any other application.

Craig

General

Comments (0)

Permalink

Could not load file or assembly ‘AjaxControlToolkit’ or one of its dependencies. Access is denied.

Problem: Recently i came across this problem while switching to a new Windows 7 64 bit m/c. The web site at hand was working fine on my previous m/c Windows 7 64 bit RC1. The error and the event log were basically not very helpful either.

Resolution:

So here is what I did to fix the issue (to get it to work using IIS):

1. Make sure that the dll in question is either set to “Copy to: Local). Go to References, right click on AjaxControlToolkit and select properties. In my case it was already set to Copy Local. So that was not the issue

2. Open IIS Manager, browse to the AppPool (in my case it was the DefaultAppPool… it is just a Dev box, ok!). Go to Advanced Settings, and set Enable 32-bit Applications to true. Now that did fix the problem I was having.

For Cassini:

1. I had to give modify rights on the asp.net temp folder  to the Apppool identity (in my case IIS users group).

Discussion

  • On further research (i.e. “google”), I came to the conclusion that it was a permissions error related to the Temporary ASP.NET Files folder (C:\Windows\Microsoft.NET\Framework64\v2.0.50727). Some of the other fixes for this problem can be found on other blogs. They relate to the access permissions on the ASP.NET folder, Trust level Settings on the Website, and Anti-virus settings. Here is a link to some of them:
  • What I found was that the main focus while resolving this error should be on the later part of the error that says ‘Access is denied’ or “Invalid Argument’ etc.. that will help you in attacking the issue. In my case it was the fact that I was running a 32 bit app on a 64 bit mc and a permissions issue as well on the Temporary ASP.NET folder.
  • If you get an ‘Access Denied’..try checking the permissions on the Temp ASP.NET folder.
  • If you get ‘Invalid Argument’ try cleaning the temp asp.net folder and restarting the web site.

http://lichao.net/eblog/fix-access-is-denied-exception-from-hresult-0×80070005-e_accessdenied-on-local-machines-200905307.html

http://weblogs.asp.net/joshuajohnson/archive/2008/12/22/could-not-load-file-or-assembly-ajaxcontroltoolkit-or-one-of-its-dependencies-the-parameter-is-incorrect-exception-from-hresult-0-215-80070057-e-invalidarg.aspx

http://msforums.ph/forums/t/46678.aspx

.NET Framework
General

Comments (1)

Permalink

WCF Exception Shielding Error

While using the WCF Exception Shielding policy in Enterprise Library’s Exception Handler Block to shield exceptions and return Fault Contracts I came across an error. I named the shielding policy “WCF Shielding Policy” and implemented a FaultContract for each type of desired or expected exception.

While testing the policy, we were unable to return a proper FaultContract from the WCF Service.  The only error that we received was the generic Communication Error:

System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to (path of service).
 This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).
See server logs for more details. --->  System.Net.WebException: The underlying connection was closed:
An unexpected error occurred on a receive.
  System.IO.IOException: Unable to read data from the transport connection: An existing connection was
forcibly closed by the remote host.
 System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.

This error is incredibly deceiving and led to many dead ends while trying to track down the root cause of the error. I re-wrote the app.config file and removed and re-added the service references, all to no avail.

The cause of the problem was simply in the name of the policy. If you don’t name your shielding policy “WCF Exception Shielding” verbatim then the policy will not be found and the error above will be thrown.  If you do name your policy something different or you implement different policies for different types of exceptions then you have to explicitly state in the ExceptionShielding attribute the name of the policy as follows:

C#:[ExceptionShielding("MyPolicy")]
VB: <ExceptionShielding ("MyPolicy")>

I renamed the policy to the default name and started receiving the Fault Contracts as expected.

Hope this helps eliminate some headaches for someone.

-Craig

General

Comments (0)

Permalink

Write iPhone Applications in C# using MonoTouch

The mono project has released a new Mono edition for Apple’s iPhone and iPod Touch.  This allows you to write full featured applications for the iPod touch using C#. 

This is not a JIT Compiler, but is a static compiler that turns .NET executables into native applications. 

For more information, check out the project at : http://www.mono-project.com/MonoTouch

General

Comments (0)

Permalink

Spring and Functional Tests

Recently on our project we created a “functional” unit test for some services. The test was designed to check the entire call stack from the service layer to the DAO and return some expected results. We are using Spring for dependency injection through our entire application. This functional test initially only referenced the service DLL’s as this is all we thought that had to be referenced.  Upon execution, the test was failing due to “null or empty Context”. We knew that we had referenced the context in the configuration file correctly, but were perplexed at how the spring context were not loading.

We finally realized that it was an issue with Resharper and how the tests were executed differently then when run is Visual Studio using default run (ctrl + r, t)

Once this was solved, we were getting reference exceptions.

To solve this error, it is necessary to reference in your test project all of the DLL’s that are referenced in your Spring xml file. It will not work unless you do so.

-Craig

General

Comments (0)

Permalink

Convert Multi-page image to Collection of Bitmaps

Recently on a project, we had the need to convert a saved multi-page tiff to a collection of bitmaps for viewing in a UI using standard GDI+ methods. These bitmaps also needed to be printed in a high quality way for submission to a government agency.  This method ensures that no quality will be lost from the creation of the new bitmap objects.

/////////////////////////////////////////
// by Craig Vallee
// Consultant
// Tallan, Inc.
/////////////////////////////////////////

private static List<Bitmap> ImageToBitmap()
{
    //Create and Image object from file path and name
    Image originalImage = Image.FromFile(@"C:\Temp\Your_File.tif");

    //Create a collection of Bitmap objects 
    List<Bitmap> bitmapList = new List<Bitmap>();

    //Place holders for setting resolution of new Bitmap objects
    var xResolution = originalImage.HorizontalResolution;
    var yResolution = originalImage.VerticalResolution;

    //Create FrameDimesion for iteration through file frames
    FrameDimension frameDimension = new FrameDimension(originalImage.FrameDimensionsList[0]);

    //Framecount of image for iteration through file frames
    int frameCount = originalImage.GetFrameCount(FrameDimension.Page);

    //Simple iteration through file frames
    for (int i = 0; i < frameCount; ++i)
    {
        //Create bitmap to hold individual frame
        Bitmap bmp;

        //Moves active frame pointer to next frame in iteration
        originalImage.SelectActiveFrame(frameDimension, i);

        //Cast image frame to Bitamp holder
        bmp = (Bitmap)originalImage;

        //Create new Bitmap from placeholder
        Bitmap temp = new Bitmap(bmp);

        //Set bitmap resolution based on original resolution
        temp.SetResolution(xResolution, yResolution);

        //Add Bitmap to Bitmap collection
        bitmapList.Add(temp);
    }
    return bitmapList;

}

-Craig

.NET Framework
General

Comments (0)

Permalink

Windows Worfklow: Error 1342 Activity X validation failed: Can not find the condition Y

This is just a small tip for anyone doing any WWF work:  The workflow’s name property MUST MATCH file workflow filename or rules and conditions will not be found, and the workflow will not be instantiated.

Example:

Create a sequential workflow.  Call it WorkflowA.cs.

Click the workflow in the designer mode, and change the name of the workflow in the property window to WkflwA.  Add any activity that takes conditions (i.e., if/then/else). 

Add a valid condition to this activity.

Attempt to start the workflow.

 

You will get a WorkflowValidationFailedException saying that the Workflow failed validation.  To correct this issue, rename the workflow back to “WorkflowA” and restart.  The workflow will instantiate without issue.

.NET Framework
General

Comments (0)

Permalink

Bitmap Default resolution

Recently I encountered a problem regarding default resolution for Images. An image that had been stored in a database as a multi-page tiff needed to be split into individual bitmaps for display and print on a winform using standard GDI+. While spliting the tiffs and creating the new bitmaps, the resolution was lost and was set to the default value of Bitmaps. While the display on the winform was not a problem as the viewer handled the image sizing, the printing of the image resulted in a very distorted printed image.

Upon examination of the image, the resolution was 300 x 300. Upon creation of the bitmap it defaulted to 72 x 72.

This problem was quickly resolved by setting the resolution of all of the new  bitmaps  to the original resolution contained in the image object.

Seems like a simple fix, but it created a huge problem on the project.

-Craig

General

Comments (0)

Permalink

Creating a Project Installer for a Windows Service

I recently had to create a Windows Service for a project I’m currently working on.  Rather than installing/uninstalling using InstallUtil, I decided to create a project installer to do the work for me.  It took a little bit of digging to gather all of the necessary steps, so here is what I did to get up and running.

  1. Create your Windows Service project and Windows Service.
  2. Navigate to the designer for your Windows Service. Under the properties for your service you should see (Name) and ServiceName. Fill in both of these properties with your desired names. You will need to reference these later.
  3. In the designer for your Windows Service, right-click and click Add Installer. You should now have a ProjectInstaller.cs file, along with a designer file for the installer.
  4. Navigate to the designer for your new project installer. You should see two components, a Service Process Installer and a Service Installer. Under the Service Installer properties, you will need to add a DisplayName, and provide the ServiceName of the service you will be installing. The ServiceName must match the (Name) property of the service.
  5. Build your Windows Service project. Now in the same solution, add a new Setup Project. This will provide the Wizard interface for the installation process.
  6. After adding the project, you need to add the primary output of your Windows Service to the Setup Project. Do this by right-clicking on the Setup Project -> Add -> Project Output. Select your Windows Service project from the drop down, highlight Primary Output and click OK.
  7. Up to this point the installer will install the correct files, but it doesn’t know what to do with the files. We can add custom actions to do the service installation work. Right-click on the Setup Project -> View -> Custom Actions. For a Windows Service we need to add actions for Install, Rollback, and Uninstall. Right-click each of these and click Add Action. Double click Application Folder and select the Primary Output from your Windows Service project.
  8. You can now build your Setup Project. The output of this build will be two setup files which are used to install your new Windows Service.

-Jon

.NET Framework
General

Comments (0)

Permalink