Class library loses references (dlls) when being used

Sorry for the title. I don't know how to describe this problem shortly. My problem is that I have a class-library which has references to other (third party) DLLs. I need to use this class-library in another project, so I obviously added the .dll of my class-library to my main-project.

When I start my main-project, there's alway an error which says, that a reference (dll) in my class-library cannot be found.

If I add the whole class-library as a project to my projectmap in visual studio and then reference the whole project, this error doesn't occur.

I really don't want to add the whole class-library as a project to every "host"-project I make.

Has anyone an idea why this error occurs when the .dll of the class-library is added, but not when the whole project of the class-library is added as reference?

There must be a solution to get this working even if I don't add the whole library-project as reference. Otherwise it wouldn't make any sense to make a class library, right?

By the way: My class-library contains third-party dlls and the local copy property of the third-party dll is set to true.

Thanks in advance!

Edit: My goal is to really make the class-library portable, even though it contains third-party libraries. I want to give only the .dll to another pc and use it without adding the whole class-library project every time.

Answers


The error is because you're not copying the dll's on the second project, you added a reference to your dll so it get's copied, but not the dll's referenced by your dll, so there are missing libraries.

Or you redistribute the dependencys with your dll or you can embedd the dll's inside your dll as resources and then intercept the assembly load and provide it through a resource: http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

EDIT: IN order to do it inside a dll you need to use an static class and call an static initializer BEFORE using any of the classes which are dependant on other libraries.

Here is an example setup:

-A library called LibraryB which supplies a simple class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryB
{
    public class ReferencedClass
    {
        public int GetIt()
        {

            return 5;

        }
    }
}

-A library called LibraryA which references LibraryB and supplies two classes, the initializer and the real class:

Initializer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LibraryA
{
    public static class Initializer
    {
        public static void Init()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {

                if (!args.Name.StartsWith("LibraryB"))
                    return null;

                return Assembly.Load(LibraryA.Properties.Resources.LibraryB);

            }; 
        }
    }
}

Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryA
{
    public class RealClass
    {
        public int DoIt()
        {

            LibraryB.ReferencedClass cl = new LibraryB.ReferencedClass();
            return cl.GetIt();

        }
    }
}

The LibraryA also has the LibraryB.dll compiled library embedded as a resource.

-A project called Test which only references LibraryA:

using LibraryA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Initializer.Init();
            RealClass c = new RealClass();
            Console.WriteLine("From LibraryA: " + c.DoIt());
            Console.ReadKey();
        }
    }
}

If you set-up everithing right and you execute it it will work, remember that if you are doing through visual studio, vs will copy the dll's so to do a real test after compiling all copy the exe and LibraryA and execute, it will work without LibraryB and LibraryB is being used from LibraryA.


Need Your Help

How to get inputs with append / prepend to have matching widths with twitter bootstrap

css twitter-bootstrap

Is there a way to have inputs that have append and/or prepend to all have matching widths?