none
Use AppDomainManager subclass on WPF Console App RRS feed

  • Question

  • Recently I have run trought coding a custom AppDomainManager to get control over AppDomain creation and security. I went into a simple implementation -only overrides an some fields, with no real functionallity.

    I need some control over my default AppDomain, and I manage to do the job done this way, thanks to someone post when googled this issue:

     
    AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager;
    
    if (domainManager == null || domainManager.GetType() != typeof(MyNamespace.MyDomainManager))
    {
         //MY APP CODE...
    }
    else
    {
         ProcessStartInfo info = new ProcessStartInfo(System.Reflection.Assembly.GetExecutingAssembly().Location, System.Environment.CommandLine);
         info.UseShellExecute = false;
         info.EnvironmentVariables["APPDOMAIN_MANAGER_ASM"] = "MyNamespace.MyAssemblyWithDomainManagerInside,Version=0.1.0.1,Culture=neutral,PublicKeyToken=XXXXXXXXXXXXXXXX";
         info.EnvironmentVariables["APPDOMAIN_MANAGER_TYPE"] = "Mynamespace.MyDomainManager";
         Process process = Process.Start(info);
    }
    
    
    

    That works, but it has two problems that i really want to solve, and i am really stuck:

    1. That code "restarts" the process, and it's an undesired behavior for my App.
    2. Using App.config does not work, I included all the code in file:
     
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <runtime>
        <appDomainManagerType value="MyDomain.MyDomainManager" />
        <appDomainManagerAssembly value="MyDomain.MyAssemblyWithMyDomainManager, Version=0.1.0.1, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXX" />
        </runtime>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_MyService1" />
            <binding name="BasicHttpBinding_MyService2" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://MyService1.mydomain.gov/UpdateService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyService1"
            contract="UpdateServiceReference.MyService1" name="BasicHttpBinding_IUpdateService" />
          <endpoint address="http://MyService2.mydomain.gov/LicenseService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_MyService2"
            contract="LicenseServiceReference.MyService2" name="BasicHttpBinding_ILicenseService" />
        </client>
      </system.serviceModel>
    </configuration>
    

     Any advices are welcome, thanks!

    Wednesday, January 30, 2013 3:00 PM

Answers

  • Hi Eduardo,

    I would like to quote a paragraph of this blog: http://blogs.msdn.com/b/shawnfa/archive/2005/07/21/441588.aspx 

    Incidentally, one question that comes up relatively often is why didn't we enable an application to specify its AppDomainManager in the .exe.config file?  The reason is security -- every managed .exe can supply an .exe.config file.  Since AppDomainManagers are so powerful, we didn't want to expose the ability to specify one to untrusted code.  Instead, the only ways to set one up involve running managed code, having write access to the environment, or having write access to the registry.  Since these are all trusted operations, fencing off the AppDomainManager to only applications that can perform them reduces the surface area for attack on this feature.

    So I am afraid that you need to try your first option.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 4, 2013 10:12 AM
    Moderator
  • I created a new WPF Application Project, and runtime section in app.config Works perfectly, no need to create a new process to get custom AppDomainManager working correctly.

    Maybe it was caused by modifications in the project file as I converted a console application project to WPF console application project.

    Thanks for all help.

    • Marked as answer by Eduardo Ridao Thursday, February 7, 2013 8:54 PM
    Thursday, February 7, 2013 8:54 PM

All replies

  • Hi Eduardo,

    Welcome to the MSDN Forum.

    What do you exactly mean about your two questions:

    >>That code "restarts" the process, and it's an undesired behavior for my App.

    Which process has been restarted?

    >>Using App.config does not work, I included all the code in file:

    If you just want to change the environment variables, please take a look at this Class:

    http://msdn.microsoft.com/en-us/library/yat6s6yb.aspx 

    http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx 

    http://msdn.microsoft.com/en-us/library/z46c489x.aspx

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 31, 2013 10:17 AM
    Moderator
  • 1 - That code "restarts" the process, and it's an undesired behavior for my App:

    As shown above, the code creates a Process with identical ProcessStartInfo and getting the commandline as parameters to Process Start...
    This is in fact "restarting" the main application process, with environment variables setted up.

    If you read carefully the code you can see that environment variables are setted up correctly, and of course, the code Works fine.

    But i don't want to restart my application's main process to get that variables working...


    2 - Using App.config does not work, I included all the code in file:

    Somewhere in msdn is assumed that you can use:
    <runtime>
        <appDomainManagerType value="MyDomain.MyDomainManager" />
        <appDomainManagerAssembly value="MyDomain.MyAssemblyWithMyDomainManager, Version=0.1.0.1, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXX" />
    </runtime>
    

    to set up these environment variables, but it isn't working and i must to hardcode it in Program's static constructor and restart the application main process...
    Friday, February 1, 2013 6:53 PM
  • Hi Eduardo,

    I would like to quote a paragraph of this blog: http://blogs.msdn.com/b/shawnfa/archive/2005/07/21/441588.aspx 

    Incidentally, one question that comes up relatively often is why didn't we enable an application to specify its AppDomainManager in the .exe.config file?  The reason is security -- every managed .exe can supply an .exe.config file.  Since AppDomainManagers are so powerful, we didn't want to expose the ability to specify one to untrusted code.  Instead, the only ways to set one up involve running managed code, having write access to the environment, or having write access to the registry.  Since these are all trusted operations, fencing off the AppDomainManager to only applications that can perform them reduces the surface area for attack on this feature.

    So I am afraid that you need to try your first option.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 4, 2013 10:12 AM
    Moderator
  • I created a new WPF Application Project, and runtime section in app.config Works perfectly, no need to create a new process to get custom AppDomainManager working correctly.

    Maybe it was caused by modifications in the project file as I converted a console application project to WPF console application project.

    Thanks for all help.

    • Marked as answer by Eduardo Ridao Thursday, February 7, 2013 8:54 PM
    Thursday, February 7, 2013 8:54 PM