none
Is there a method to set Entry Assembly information in AppDomain RRS feed

  • Question

  • Our product using a third-part software, which will check the Entry Assembly information at runtime.

    And we wrappered the third part software as a DLL assembly(Agent.dll), and use this Agent.dll in our code. 

    But we found if the load Agent.dll in our code directly then it works as expect, but it would go abnormally if we create a AppDomain and then load the Agent.dll, finally we work out because the third part software will check the "Entry Assembly"

    at Runtime, and at that time the "Entry Assembly" is null after loaded by AppDomain.

    So my question is if there is any method to set the "Entry Assembly" information for AppDomain, in order to make the Third-part

    software can get right results.

    Thanks.

    Wednesday, March 7, 2012 1:46 AM

Answers

All replies

  • Hi Wozniak,

    Welcome to the MSDN Forum.

    Please take a look at the remark of this documentation: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly(v=vs.100).aspx 

    The GetEntryAssembly method can return Nothing when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to theGetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.

    If your third party software is not developed on .net framework, the null value should be the expected value.

    If I have misunderstood anything, please feel free to let me know.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 7, 2012 9:58 AM
    Moderator
  • Hi Mike,

    Our 3th-part software is develop on .net framework, and the assembly is loaded from managed application.here are some code :the winform application has a button, click the button function:

     private void button1_Click(object sender, EventArgs e)
            {
                AppDomainManager apm = new AppDomainManager();

                try
                {

                    AgentRuntimeClass sar = (AgentRuntimeClass)apm.CreateObject("AgentDll.dll", "AgentDll.AgentRuntimeClass");
                                  

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
                finally
                {
                    apm.Unload();
                }
            }

    AppDomainManager class has a method CreateObject to generate instance in new AppDomain:

    Get the EntryAssembly information , such as product name and company name.

    It is located in a dll assembly to get company name and product name.

    Then you will see the product name and company name are emtpy.

    Wednesday, March 7, 2012 10:42 AM
  • public class AppDomainManager { private AppDomain appDomain = null; public object CreateObject(string assemblyPartialName, string className) { try { string executable = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath; string baseDirectory = Path.GetDirectoryName(executable); string pathName = Path.Combine(baseDirectory, assemblyPartialName); AssemblyName an = AssemblyName.GetAssemblyName(pathName); Assembly asm = System.Reflection.Assembly.Load(an); if (asm == null) asm = Assembly.Load(assemblyPartialName); string exeAssembly = asm.FullName; System.Diagnostics.Debug.WriteLine("pathName=" + pathName); System.Diagnostics.Debug.WriteLine("exeAssembly=" + exeAssembly); // Construct and initialize settings for a second AppDomain. AppDomainSetup ads = new AppDomainSetup(); ads.ApplicationBase = "file:///" + baseDirectory; ads.DisallowBindingRedirects = false; ads.DisallowCodeDownload = true; ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; // Create the second AppDomain. appDomain = AppDomain.CreateDomain("New Created Domain", null, ads); System.Diagnostics.Debug.WriteLine("System.Environment.CurrentDirectory=" + System.Environment.CurrentDirectory); System.Diagnostics.Debug.WriteLine("appDomain.BaseDirectory" + appDomain.BaseDirectory); System.Diagnostics.Debug.Flush(); // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. object newObj = appDomain.CreateInstanceAndUnwrap( exeAssembly, className, true, BindingFlags.Default, null, null, null, null, null); return newObj; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.Flush(); throw; } } }


    Wednesday, March 7, 2012 10:51 AM
  • namespace AgentDll
    {
        [Serializable]
        public class AgentRuntimeClass
        {
            public AgentRuntimeClass()
            {
                string sCompany = GetRuntimeCompanyAttribute();
                string sProduct = GetRuntimeProductAttribute();
                string sDomain = GetCurrentDomain();
                MessageBox.Show(string.Format("The company name is '{0}', product name is '{1}', current domain is '{2}'", sCompany, sProduct, sDomain));
            }
            private string GetRuntimeCompanyAttribute()
            {
                string sRet = string.Empty;
                Assembly entryAss = Assembly.GetEntryAssembly();
                if (entryAss != null)
                {
                    object[] atrs = entryAss.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                    if (atrs.Length > 0)
                    {
                        AssemblyCompanyAttribute attCompany = atrs[0] as AssemblyCompanyAttribute;
                        if (attCompany != null)
                            sRet = attCompany.Company;
                    }
                }
                return sRet;
            }
            private string GetRuntimeProductAttribute()
            {
                string sRet = string.Empty;
                Assembly entryAss = Assembly.GetEntryAssembly();
                if (entryAss != null)
                {
                    object[] atrs = entryAss.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                    if (atrs.Length > 0)
                    {
                        AssemblyProductAttribute attProduct = atrs[0] as AssemblyProductAttribute;
                        if (attProduct != null)
                            sRet = attProduct.Product;
                    }
                }
                return sRet;
            }
            private string GetCurrentDomain()
            {
                string sDomain = AppDomain.CurrentDomain.FriendlyName;
                return sDomain;
            }
        }
    }

    Wednesday, March 7, 2012 10:53 AM
  • Hi Wozniak,

    Please change the GetEntryAssembly to GetExecutingAssembly and try again.

    For more, please refer to this description: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly.aspx 

    In other application domains, this is the first executable that was executed by AppDomain.ExecuteAssembly

    In your AgentDll assembly, there is no executable, at least, there is no such things in your above code. So you always get nothing. 

    If you add some executable in this dll later, and you have to call AppDomain.ExecuteAssembly first before you call GetEntryAssembly method.

    And the description of GetExecutingAssembly is: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx

    Gets the assembly that contains the code that is currently executing. 

    This returns the assembly you are executing.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Mike FengModerator Thursday, March 8, 2012 10:22 AM The previous is wrong.
    • Proposed as answer by ScottQ Friday, March 9, 2012 12:11 AM
    • Marked as answer by Wozniak_dl Friday, March 9, 2012 12:25 AM
    Thursday, March 8, 2012 9:43 AM
    Moderator