nickdu
11/18/2008 2:39:00 PM
I'm starting to look into this again. Unfortunately it appears it's not
possible if you derive from System.ServiceProcess.ServiceBase because its
ServiceMain method removes the first command line argument which happens to
be the service name.
public unsafe void ServiceMainCallback(int argCount, IntPtr argPointer)
{
fixed (NativeMethods.SERVICE_STATUS* service_statusRef = &this.status)
{
string[] state = null;
if (argCount > 0)
{
char** chPtr = (char**) argPointer.ToPointer();
state = new string[argCount - 1];
for (int i = 0; i < state.Length; i++)
{
chPtr++;
state[i] = Marshal.PtrToStringUni(*((IntPtr*) chPtr));
}
}
.
.
.
This is unfortunate. If I want to provide this functionality I'll have to
write my own ServiceBase which does all the interop stuff, ouch! Why would
someone remove such an important argument?! Even if they want to remove it
from the args[] passed to OnStart() they should have at least captured it
here and set the ServiceName property on the Service object.
--
Thanks,
Nick
nicknospamdu@community.nospam
remove "nospam" change community. to msn.com
""Hongye Sun [MSFT]"" wrote:
> Thanks for your reply, Nick.
>
> In my opinion, there is no easy solution for your situation. However, it is
> possible for us to work out a relatively simple AppDomain solution.
>
> In order to save your effort to write the code, I wrote a sample code to
> illustrate how to use AppDomain to change the configuration file name. The
> key concepts of the code are:
> 1. Create a new AppDomain with same application base and specified
> configuration path.
> 2. Create a MarshalByRefObject object: internal worker. Call its work
> method to run across different AppDomains.
> 3. Inside the internal worker, it will load and run the components based on
> configuration file.
> 4. Since components are run in new AppDomain, they are also using specified
> configuration file.
> 5. After service stop, unload the worker AppDomain.
>
> Here is the Worker and InternalWorker classes:
> //////////////////////////////////
> public class Worker
> {
> private AppDomain mAD;
>
> public void Work(string serviceName)
> {
> // Get the full name of the EXE assembly.
> string exeAssembly = Assembly.GetEntryAssembly().FullName;
>
> // Construct and initialize settings for a second AppDomain.
> AppDomainSetup ads = new AppDomainSetup();
> ads.ApplicationBase =
> System.Environment.CurrentDirectory;
> ads.DisallowBindingRedirects = false;
> ads.DisallowCodeDownload = true;
> ads.ConfigurationFile =
> Path.Combine(Assembly.GetEntryAssembly().Location,
> serviceName + ".exe.config");
>
> // Create the second AppDomain.
> mAD = AppDomain.CreateDomain("Worker AD " + new
> Guid().ToString(), null, ads);
>
> // Create internal worker in second domain.
> InternalWorker innerWorker =
> (InternalWorker)mAD.CreateInstanceAndUnwrap(
> exeAssembly,
> typeof(InternalWorker).FullName
> );
>
> // Run the internal worker to do real work.
> innerWorker.Work();
> }
>
> public void Unload()
> {
> if (mAD != null)
> {
> AppDomain.Unload(mAD);
> }
> }
>
> internal class InternalWorker : MarshalByRefObject
> {
> public void Work()
> {
> Console.WriteLine(
>
> ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FileP
> ath
> );
> // TODO: Load configuration data
>
> // TODO: Load components
>
> // TODO: Run components
> }
> }
> }
> //////////////////////////////////
>
> Please use the Worker class as following:
> When service starts:
> m_worker = new Worker();
> m_worker.Work(this.ServiceName);
>
> When service stops:
> m_worker.Unload();
>
> The code was only tested by me in simple situation. In order to use the
> code in your product, you still need to well debug and test on it. Please
> let me know if you have any question or difficulty on it. I will try my
> best to help.
>
> Regards,
> Hongye Sun (hongyes@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>