Link to home
Start Free TrialLog in
Avatar of ditkis
ditkis

asked on

Using DLL's on client machine

Hello!

My project consists of several DLL's. Among other I have xDLL.dll which has a reference to xObjects.dll. When compiling the project, xDll.dll and all dependensies are copying to bin folder of the project. It is known that on every machine (user machine) that this application will be used, all those files are located in c:\\Program Files\X\x.Objects.dll

My question: how can I use files located on users machines instead of those which are copying while compiling?

I've tried:

Copy Local = false for xObjects.dll

And have created the file:
<?xml version ="1.0"?>
<configuration>
<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly>
                        <assemblyIdentity name="xObjects" culture="" publicKeyToken=""/>
                        <codeBase version="" href="file:///C:/Program Files/X/xObjects.dll"/>
                </dependentAssembly>
        </assemblyBinding>
</runtime>
</configuration>

---------------------------

Please help!
Thanks!

Avatar of dstanley9
dstanley9

From MSDN:

You can use the <codeBase> element only in machine configuration or publisher policy files that also redirect the assembly version.

...

Note   If you are supplying a code base hint for an assembly that is not strong-named, the hint must point to the application base or a subdirectory of the application base directory.


If you want to use the assembly from a location other than the app's location, I believe you have to use the GAC.  This also means you need a strong name (using a public key) for the assembly.  You can use GACUTIL to install the DLL into the GAC, and even create setup projects that will do this for you.

Avatar of ditkis

ASKER

assembly i wish to use is strongly named
Then I would install it into the GAC on the users' machines.  

Use the command:

gacutil /i C:/Program Files/X/xObjects.dll
Avatar of ditkis

ASKER

I can not register assembly in GAC. I know there is another way to do it.
Try to supply the public key token in the assemblyIdentity node.
Avatar of aponcealbuerne
I think that if you set the version in your AssemblyInfo.cs should be enogh

[assembly: AssemblyVersion("1.0.0.7")]

instead of

[assembly: AssemblyVersion("1.0.*")]

hope helps...
Also set the culture if you set the culture in the assemblyinfo of the xObjects DLL
Avatar of ditkis

ASKER

I've tried to "Try to supply the public key token in the assemblyIdentity node." Didn't work.

The reason I want to use DLL's on user machine is because they are updating them regulary. I want to use any version they have, so I can not set the version. Is there anything else I can do to make it work?
try adding culture="neutral" if there is no culture specified in the assemblyinfo of the source dll
If you're still having trouble, try adding an event handler in your app to perhaps shed some more light on the cause:


{copied from http://support.microsoft.com/default.aspx?scid=kb;EN-US;837908}

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);



private Assembly MyResolveEventHandler(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.
                  strTempAssmbPath="C:\\Myassemblies\\"+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;
}
Avatar of ditkis

ASKER

I'm trying to use example from msdn but getting wierd error.
Studio underlines "+=" and "ResolveEventHandler" in "currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);" line. Does anybody know what's happening there?
Where are you adding the code?  It should be in the entry point of your application (Main(), for example).

What compiler errors do you get?
Avatar of ditkis

ASKER

TYpe or namespace name 'Assembly' could not be found...
Avatar of ditkis

ASKER

private Assembly MyResolveEventHandler(object sender,ResolveEventArgs args)
add

using System.Reflection;

to the top of your code file.
Avatar of ditkis

ASKER

currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

It still does not work, I got following error:

an object refence required for non static field, method or property .......... myresolveeventhadler....
Are you defining the MyResolveEVentHandler as a method of the startup class as well?
Avatar of ditkis

ASKER

Yes, that's part of a code:

      [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
                  AppDomain currentDomain = AppDomain.CurrentDomain;
                  currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
            }


            private Assembly MyResolveEventHandler(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="";
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial