Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C#
Tip/Trick

Who Are You Running From?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 May 2013CPOL1 min read 6.6K  
Determine the nature of the entry assembly

Introduction

Once again, I have a tip that came directly from my work-a-day development efforts. We have a plugin-based windows service, and when developing the associated plugin DLLs, we create a test harness app in the solution so we can debug the plugins on our local box before we deploy them.

All of the plugins have an output folder, and when deployed, the output folder is located on a remote machine. To avoid interfering with a previous version of a given plugin that might be running (when debugging), we need to specify a local folder to receive the output that the plugin generates.

Prior to me taking over the coding for this particular task, the process of debug/deployment involved changing the DLL's config file to change the output folder, depending on whether we were debugging or deploying the plugin. As you might guess, there have been several times where someone forgot to restore the deployed version of the path, which led me to the solution presented below.

All I needed to do was determine whether the DLL was loaded by an application, or if it was loaded by a service. Thanks to reflection, we can easily make this determination. The code below could easily be reduced to a single line, but I kept it separated out so it was easier to see what was happening. 

The end result is that now I can include BOTH the deployed and local version in the config file, and don't have to worry about making sure the correct key entry is commented out in the config file. Given the nature of my swiss-cheese-like memory, that can only be a good thing.

The Code

C#
using System.Reflection;

private bool RunningInService()
{
    bool isService = false;

    // Get the assembly that's actually running our plugin DLL
    Assembly entry = Assembly.GetEntryAssembly();

    // Get the entry point method that was called to enter the host assembly.
    System.Reflection.MethodInfo entryPoint = entry.EntryPoint;

    // see if it's a service
    isService = entryPoint.ReflectedType.BaseType.FullName == "System.ServiceProcess.ServiceBase";

    return isService;
}

History

  • 10 May 2013:Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
-- There are no messages in this forum --