referencing a dll available in a different folder from a Windows Service without copying the dll
-
In the C# Windows Service we develop, we need to refer a dll file from a different folder, for an API Method call of a method in the dll. This we want to accomplish by directly accessing the folder where the dll exists. Also, we do not want the dll to be copied into the folder where the Executable file of the Windows Service is available. Example, we have a windows service called IdMAccountService,with it's Executable file IdMAccountService.exe available in D:\MetraTech\RMP\BIN folder . In that service we are referencing a dll file D:\MetraTech\RMP\Extensions\Account\Bin\MetraTech.Account.ClientProxies.dll for an API Method Call. Is it possible for us to do it without copying the above dll to D:\MetraTech\RMP\BIN folder? For this, I tried setting the ‘Copy Local’ as False in the reference to the dll and tried creating the Shortcut of the DLL file in the D:\MetraTech\RMP\BIN folder. On both the cases, Service while executing gives the error as 'Error Could not load file or assembly 'MetraTech.Account.ClientProxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0f5c26dbec45a75' or one of its dependencies. The system cannot find the file specified.'. I would like to know how I can make it work as expected? Please clarify.
Thanks & Regards, Govindarajan K.
-
In the C# Windows Service we develop, we need to refer a dll file from a different folder, for an API Method call of a method in the dll. This we want to accomplish by directly accessing the folder where the dll exists. Also, we do not want the dll to be copied into the folder where the Executable file of the Windows Service is available. Example, we have a windows service called IdMAccountService,with it's Executable file IdMAccountService.exe available in D:\MetraTech\RMP\BIN folder . In that service we are referencing a dll file D:\MetraTech\RMP\Extensions\Account\Bin\MetraTech.Account.ClientProxies.dll for an API Method Call. Is it possible for us to do it without copying the above dll to D:\MetraTech\RMP\BIN folder? For this, I tried setting the ‘Copy Local’ as False in the reference to the dll and tried creating the Shortcut of the DLL file in the D:\MetraTech\RMP\BIN folder. On both the cases, Service while executing gives the error as 'Error Could not load file or assembly 'MetraTech.Account.ClientProxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0f5c26dbec45a75' or one of its dependencies. The system cannot find the file specified.'. I would like to know how I can make it work as expected? Please clarify.
Thanks & Regards, Govindarajan K.
You could always use the AssemblyResolve[^] event which will be triggered when it can't find the assembly in the current path.
This space for rent
-
In the C# Windows Service we develop, we need to refer a dll file from a different folder, for an API Method call of a method in the dll. This we want to accomplish by directly accessing the folder where the dll exists. Also, we do not want the dll to be copied into the folder where the Executable file of the Windows Service is available. Example, we have a windows service called IdMAccountService,with it's Executable file IdMAccountService.exe available in D:\MetraTech\RMP\BIN folder . In that service we are referencing a dll file D:\MetraTech\RMP\Extensions\Account\Bin\MetraTech.Account.ClientProxies.dll for an API Method Call. Is it possible for us to do it without copying the above dll to D:\MetraTech\RMP\BIN folder? For this, I tried setting the ‘Copy Local’ as False in the reference to the dll and tried creating the Shortcut of the DLL file in the D:\MetraTech\RMP\BIN folder. On both the cases, Service while executing gives the error as 'Error Could not load file or assembly 'MetraTech.Account.ClientProxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0f5c26dbec45a75' or one of its dependencies. The system cannot find the file specified.'. I would like to know how I can make it work as expected? Please clarify.
Thanks & Regards, Govindarajan K.
You can specify the location of strongly named assemblies in an application configuration file. I think doing so creates a maintenance nightmare but if you want to try it here are the details. Add a application configuration file to the project and add the following
The location of the assembly is hard coded which is less than ideal and a slight improvement is possible by specifying a relative path in the codeBase href attribute. If application base directory is D:\MetraTech\RMP\BIN then this should be correct.
If the ClientProxies.dll relies upon other assemblies within ..\Extensions\Account\Bin then a dependentAssembly element must be added for each one otherwise the assembly loader will only look in the GAC or the application base directory. Good luck and remember that all the xml is case sensitive. If you enter PublicKeyToken instead of publicKeyToken then the loader won't complain and will just quietly ignore the whole dependentAssembly element. Alan.
-
You can specify the location of strongly named assemblies in an application configuration file. I think doing so creates a maintenance nightmare but if you want to try it here are the details. Add a application configuration file to the project and add the following
The location of the assembly is hard coded which is less than ideal and a slight improvement is possible by specifying a relative path in the codeBase href attribute. If application base directory is D:\MetraTech\RMP\BIN then this should be correct.
If the ClientProxies.dll relies upon other assemblies within ..\Extensions\Account\Bin then a dependentAssembly element must be added for each one otherwise the assembly loader will only look in the GAC or the application base directory. Good luck and remember that all the xml is case sensitive. If you enter PublicKeyToken instead of publicKeyToken then the loader won't complain and will just quietly ignore the whole dependentAssembly element. Alan.
Thanks Alan for the inputs provided. It worked fine for me.
Govind.