2

I moved few dialog forms from one class library to another (drag and drop) in the same solution (both are c# class libraries). Then at run time I started getting error inside the InitializeComponent method of myform.Designer.cs of moved and previously existed forms in that target dll at line similar to

this.pictureBox1.Image = global::mydll.Properties.Resources.Webster;

The exception is:

String cannot have zero length.

Sometimes the form will load correctly the first time but not after that.

Do you ever have issues moving forms from one project to another?

I did updated all namespaces to use the target dll namespace.

-- From Event viewer

Message:    String cannot have zero length.
Source: mscorlib
TraceStack:    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
   at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
   at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
   at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
   at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
   at Common.Properties.Resources.get_License() in E:\WORK\ProjectOne\Common\Properties\Resources.Designer.cs:line 146
   at Project.ONE.Common.ProgressDialog.InitializeComponent() in E:\WORK\ProjectOne\Common\ProgressDialog.Designer.cs:line 100
   at Project.ONE.Common.ProgressDialog..ctor(String caption) in E:\WORK\ProjectOne\Common\ProgressDialog.cs:line 60
   at Start.CSCom.start() in E:\WORK\ProjectOne\Addin\CSCom.cs:line 326
   at Start.Connect.ButtonStartClicked(IRibbonControl control) in E:\WORK\ProjectOne\Addin\Connect.cs:line 464.

-- SOLVED

As directed by Avi, I enabled the "First Chance Exception" and found the problem in the Assembly Resolve code below (Apparently this trying to load the assembly and failing to do so) :

currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);

.. .. ..

    Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args){
        //This handler is called only when the common language runtime tries to bind to the assembly and fails.

        //Retrieve the list of referenced assemblies in an array of AssemblyName.
        Assembly MyAssembly, objExecutingAssemblies;
        string strTempAssmbPath = "";

        objExecutingAssemblies = Assembly.GetExecutingAssembly();
        AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();

        //Loop through the array of referenced assembly names.
        foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
        {
            //Check for the assembly names that have raised the "AssemblyResolve" event.
            if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
            {
                //Build the path of the assembly from where it has to be loaded.
                //The following line is probably the only line of code in this method you may need to modify:
                RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"Software\ProjectONE\addin");
                strTempAssmbPath = regkey.GetValue("DllLocation").ToString();
                if (strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\";
                strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
                break;
            }

        }
        //Load the assembly from the specified path.
        MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

        //Return the loaded assembly.
        return MyAssembly;
    }

I just removed the "Assembly Resolve" code completely since my target of moving the forms from one class library to another is to reduce the number of my solution dlls.

I believe this problem is still unique to my case but someone may find this useful.

Thanks.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Alobidat
  • 462
  • 7
  • 16
  • Did you check if the resources moved along with it? – Prix Sep 15 '13 at 04:57
  • yes, the issue is corrupted resource or something similar. the exception is thrown on already exists forms as well as moved ones and only when loading images (and to confuse me more some forms will load the first time only). – Alobidat Sep 15 '13 at 05:11
  • And removing the images and adding them again to the forms does not solve the issue. – Alobidat Sep 15 '13 at 05:14
  • are your images set to the correct compiler option `content` so that the build process moves them correctly into the project's bin or release directories? – Claies Sep 15 '13 at 05:29
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Sep 15 '13 at 05:31
  • John, I think I updated the title again before seeing you comment, I will check the post and update the title again. – Alobidat Sep 15 '13 at 05:39
  • Andrew, I tried to set my images as embedded resources and as content and still the same issue. As I said, previously existed Forms in the target class library is generating the exception as well as moved ones. Apparently something is corrupted in the new class library but I cannot figure it out. – Alobidat Sep 15 '13 at 05:49

2 Answers2

1

I remember having the same issue, And it also has something to do with a class that Inherited from Image.

I do not recall what was the origin of the issue, but I do remember that it was due to an internal exception that was not handled.
The original exception was not related to String cannot have zero length. so this message might be misleading.

Try the following:

  1. Enable catching all first chance exceptions (you can find instructions in my answer here)
  2. Debug you application.
  3. Make sure that there are no first chance exceptions (If this is not possible, make sure to catch and handle them)
  4. After there are no unhandled exceptions debug you application again.
  5. Do you still get the error?

I'll try to look around in order to remember what was the cause of this issue. If The above helps you, please share your insight about the cause.

Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75
1

Since i´ve encountered the same problem,

i found the problem (if anybody wonders about this):

your last line in your resolve event is:

MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

but if the correct assembly was not found strTempAssmbPath="" <- empty string.

thus the exception

this will also happen if you added the reference, but did never use a type (it will not be loaded into objExecutingAssemblies.GetReferencedAssemblies();)

Markus
  • 195
  • 1
  • 11