C# Cannot find dll if it's installed with the program running - c#

I'm running a program in C# .Net, WPF, and having this problem: if I try to load a dll dynamically from the GAC and it's not presente, the system show an error, then I install the dll in the GAC and system still isn't able to find it. But, if just restart the system, whitout changing anything, it find's the DLL.
For more details, in an async thread, I call one dll dynamically from the GAC, and this dll calls another DLLs dynamically, and are those "second" dlls that are causing this problem.
The dlls are being called using the following:
var classType = Type.GetType(fullyQualifiedName, true);
T assemblyClass = (T)Activator.CreateInstance(classType);
Also, the "fullyQualifiedName" follows the format:
NameSpace.Class, AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=number of the token, processorArchitecture=x86
And the "assemblyClass" is cast to an interface.
Thanks in advance.

Related

Enforce a specific version of a third-party assembly

I have an application that relies on a set of third-party DLLs, version v1.1. In order to use them, I reference the main one, which is found somewhere under the Program Files folder. This library uses the other ones, installed in the GAC.
A new version of the third-party DLLs is released, v1.2. The DLL in the Program Files folder is replaced by the new version; in the GAC, both versions co-exist.
The problem is to get the application (compiled with DLLs v1.1) to work when v1.2 is installed, without re-compiling it, and without changing the app.exe.config file.
I am the maintainer of the DLLs; additional constraints are that only the latest version of the DLL is installed in Program Files, and other DLLs are all installed in the GAC (all versions are kept).
My problem is that the application starts, but eventually I get an error because of incompatibilities between types:
Unhandled Exception: System.InvalidCastException:
[A]ThirdParty.User cannot be cast to [B]ThirdParty.User.
Type A originates from 'ThirdParty, Version=1.2.0.0, Culture=neutral, PublicKeyToken=XXXX'
in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\ThirdParty\v4.0_1.2.0.0__XXXX\ThirdParty.dll'.
Type B originates from 'ThirdParty, Version=1.1.0.0, Culture=neutral, PublicKeyToken=XXXX'
in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\ThirdPart\v4.0_1.1.0.0__XXXX\ThirdParty.dll'.
I am not sure to understand how the v1.2 DLL is chosen, I am guessing that the path to the DLL is stored in the application, which in turn decides to use the DLLs in the GAC for this version? I am testing on a system different from the one used for compiling, but the DLLs are located in the same location. My understanding from the documentation is that the v1.1 version in the GAC should be chosen, not v1.2.
Where is my mistake? How can I fix it?
Thanks,
I found a solution in my specific case, and re-reading my question I realize I did not mention that my initial DLL does rely on a PowerShell instance. Which is where I worked around my problem. In my DLL, I load a specific version if the DLL that is present in the GAC, and load it in my PowerShell instance:
var assembly = Assembly.Load("ThirdParty, Version=1.1.0.0, Culture=neutral, PublicKeyToken=XXXX");
var ps = PowerShell.Create();
ps.Commands.AddCommand("Import-Module").AddParameter("-Assembly", assembly);
ps.Invoke();

JSON.net functions no longer working in VS2012 [duplicate]

I have added third party reference (Json newtonsoft) dll in my script component (using edit script option), but when i run the package, I am getting an error
Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Any suggestions?
I will not be able to add the dll in GAC.
I am using SQL Server 2008.
By "Running," I assume running from agent/command-line is failing? It should work from within BIDS/SSDT. The short answer is the DLL must be registered with the GAC or you can download the source code and add that project into the script task and then reference said project.
Looking at the project, it should be a strongly signed DLL (based on presences of Dynamic.snk) and thus capable of being added to the GAC. Oh, but you state you will not be able to add it into the GAC, implying it's a permission not a capability issue.
If that's the case, either compile the project in with the source or surround it with a web service wrapper and then reference the service.
I also saw this answer, seems you can try loading the references dynamically.
Automated deployment of mixed SSIS / DLL solution
You can using Reflection to load dll at runtime from file system without needing to install in GAC . This is helpful if permission to install in GAC is not availaible .
//Add a Static Constructor which is guaranteed to be called exactly once
// “before the first instance is created or any static members are referenced.”,
// so therefore before the dependent assemblies are loaded.
static ScriptMain()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
//Provide path to dll stored in folder on file system
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string path = #"D:\DLL\";
return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.dll"));
}
Ofcourse you need to also Add Reference to dll in script task .

Add third party dll reference in ssis script component

I have added third party reference (Json newtonsoft) dll in my script component (using edit script option), but when i run the package, I am getting an error
Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Any suggestions?
I will not be able to add the dll in GAC.
I am using SQL Server 2008.
By "Running," I assume running from agent/command-line is failing? It should work from within BIDS/SSDT. The short answer is the DLL must be registered with the GAC or you can download the source code and add that project into the script task and then reference said project.
Looking at the project, it should be a strongly signed DLL (based on presences of Dynamic.snk) and thus capable of being added to the GAC. Oh, but you state you will not be able to add it into the GAC, implying it's a permission not a capability issue.
If that's the case, either compile the project in with the source or surround it with a web service wrapper and then reference the service.
I also saw this answer, seems you can try loading the references dynamically.
Automated deployment of mixed SSIS / DLL solution
You can using Reflection to load dll at runtime from file system without needing to install in GAC . This is helpful if permission to install in GAC is not availaible .
//Add a Static Constructor which is guaranteed to be called exactly once
// “before the first instance is created or any static members are referenced.”,
// so therefore before the dependent assemblies are loaded.
static ScriptMain()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
//Provide path to dll stored in folder on file system
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string path = #"D:\DLL\";
return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.dll"));
}
Ofcourse you need to also Add Reference to dll in script task .

Assembly.GetExportedTypes() does not work in .NET 4.0 (same code runs in 3.5)

Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0. The same code is working perfectly in a project targeted at an older .NET version.
NOTE: *The code works for local assemblies but when trying to open them off a network drive it does not work, that is my problem... It looks to be loading the assembly, but it claims it can't find it when using GetExportedTypes(). Again if I open a dll off my machine it works, this error only occurs with dlls located on network drives *
EDIT: The error is thrown on GetExportedTypes
The Code:
Assembly assembly;
Type[] t;
assembly = Assembly.LoadFrom(dllPathOpenFileDialog.FileName.ToString());
t = assembly.GetExportedTypes();
The Error:
Could not load file or assembly '*..***.***.****, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
(I used asterisks to hide personal information - the file name)
The way certain security policies are handled was changed in .NET 4.0. This article should provide some background.

Unable to load assembly with windows service

I have created a windows service in C# VS2008 that uses a reference to an external class library to wrote. I have added the reference to it in VS2008. When I run start the service it throws an exception when trying to access the external DLL:
Could not load file or assembly 'vcribAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
The DLL is in the same directory as the service.exe file. Is there something special that I need to do for windows services like putting the DLL in another directory?
It could be that vcribAPI.dll relies on other assemblies as well. I suggest using Reflector and open up the dll to see what other dll's it might reference.
I encountered exactly the same error.
The working directory of services is different from the application directory (typically C:\Windows\System32).
For example, the method AssemblyName.GetAssemblyName throws a FileNotFoundException if you try to locate an assembly deployed in the application directory.
In this case, the solution is to define Environment.CurrentDirectory with the application directory before assembly loading.
Sample code :
const string SCHEMA_FILE = #"file:\";
var appAssembly = Assembly.GetExecutingAssembly();
var path = Path.GetDirectoryName(appAssembly.CodeBase);
if (path.StartsWith(SCHEMA_FILE))
path = path.Remove(0, SCHEMA_FILE.Length);
Environment.CurrentDirectory = path;
Does you service have rights to read in the folder?
Does the assembly have other dlls or assemblies it depends on?
If so, they also need to be in this directory.
To be certain, start up the Assembly Loader Log (fusion log). See this howto (Debugging Assembly Loading Failures).

Categories