Showing posts with label Windows Service. Show all posts
Showing posts with label Windows Service. Show all posts

Friday, August 15, 2014

System.BadImageFormatException: Could not load file or assembly(Windows service)

Recently I have created a windows service and I wanted to to install that service to the my QA server.for that I used InstallUtil command.So for that below command in my command prompt.

image

But that command failed with below message

System.BadImageFormatException: Could not load file or assembly '{WriteOffService.exe}' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Then after my check dependencies those were placed fine.After I search I found the reason for this. Smile

I have complied my application according to the 32 bit configuration and I used 64 Bit installUtil to the install application. after I installed it using 32 bit installUtil and its worked as expected.

After you install .Net framework(in my case it is version 4.0) its created the two separate folders for 32 bit and 64 bit.In side those folders for according to the Framework version

Intallation root:\Windows\Microsoft.NET\

 image

(here you can see two different folders Framework(32) and Framwork64(64)).That each folder contains two InstallUtil versions according to the .Net Framework version.

image

64 bit version

image

32 bit version

Friday, August 10, 2012

In C#, how to check if a TCP port is available?

When hosting wcf service which is use net tcp binding and without port sharing in a windows service , we should make sure the availability of our wcf port number. Other wise service doesn't start.

There fore as a best practice its is better to have code which will check the availability of the port when starting the service.This code should included to the service startup event. 

   1: protected override void OnStart(string[] args)
   2:        {
   3:            try
   4:            {
   5:                if (serviceHost != null)
   6:                {
   7:                    serviceHost.Close();
   8:                }
   9:  
  10:                PortChecker pc = new PortChecker();
  11:  
  12:                if (pc.IsPortAvailable())
  13:                {
  14:                    serviceHost = new ServiceHost(typeof(M3ISPlusService));
  15:                    serviceHost.Open();
  16:                }
  17:            }


ANd this is the port checker class



   1: public bool IsPortAvailable()
   2:         {
   3:             bool isAvailable = false;
   4:             TcpListener tcpListner = new TcpListener(GetServerIp(), GetPort());
   5:             Logger logger = Logger.GetLogger();
   6:  
   7:             try
   8:             {
   9:                 tcpListner.Start();
  10:                 isAvailable = true;
  11:             }
  12:             catch (Exception ex)
  13:             {
  14:                 logger.Log(ex.Message);
  15:             }
  16:  
  17:             finally
  18:             {
  19:                 try
  20:                 {
  21:                     tcpListner.Stop();
  22:                     tcpListner = null;
  23:                 }
  24:                 catch (Exception)
  25:                 {
  26:                     tcpListner = null;
  27:                 }
  28:             }
  29:  
  30:             return isAvailable;
  31:         }

To get port in code please refer my previous post :- Read End point address programmatically


Happy Coding :)