C
C#13mo ago
cosj

❔ Program fails with reference when private=False but works when private=True

''' <ItemGroup> <Reference Include="Custom"> <HintPath>C:\SomeCustom.dll</HintPath>
<Private>False</Private> </Reference> </ItemGroup> ''' It builds fine with both Private True and False, but when setting it to False this error seems to suggest that it cannot find the dll. 'Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly It builds and runs fine with Private set to True. I've been looking for another way to set the path to the dll, but so far haven't been able to get it to work.
13 Replies
arion
arion13mo ago
When private=False it doesn't include that .dll when building, its up to you to rely on providing that .dll when the program executes. The method that uses code from that .dll will throw an exception when invoked. The default is Private being set to True All that does is toggle if that .dll hintpath should be included in the output directory.
cosj
cosjOP13mo ago
What I find weird is that it builds fine with private=False. So it finds the dll while building, I think? But then uses the wrong path while running, I Think? (I'm using vscode and net framework 4.8 by the way)
arion
arion13mo ago
When your program runs and hits a method that uses that .dll it resolves the assembly (tries to load it) and if it can't it throws an exception like you saw there.
But then uses the wrong path while running, I Think?
When you reference an assembly from a custom path thats usually fine in your project (pre-compiling) The program needs to find the .dll but no specific information is given to it on exactly how to do that. It won't scan your entire PC for that file, it will take ages But since you're running framework 4.8 you can specify a custom directory that the .dll is in.
cosj
cosjOP13mo ago
Yes, I was hoping it would find it using the HintPath
arion
arion13mo ago
HintPath is purely for building i believe If you want to re-use a certain .dll, .NET Framework has a thing called the GAC Global Assembly Cache, you could add it into that it will auto-resolve then since when resolving assemblies, it first checks the GAC, then your program's directory
cosj
cosjOP13mo ago
That would explain why it builds. I tried using "AppDomain.CurrentDomain.AssemblyResolve += OnResolve; " and loading it like that but my code crashes before it even gets to main. I'll have a look at the GAC. Thanks!
arion
arion13mo ago
Yeah, you'd have to do something like
public static void Main()
{
ResolveAssemblies();
OtherMethods();
}

public static void OtherMethods()
{
}
public static void Main()
{
ResolveAssemblies();
OtherMethods();
}

public static void OtherMethods()
{
}
Im pretty sure the assemblies get resolved when the method inside gets invoked (or any read-only properties get initialized)
arion
arion13mo ago
https://learn.microsoft.com/en-us/dotnet/framework/tools/gacutil-exe-gac-tool I found mine in C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\gacutil.exe but yours might be different
Gacutil.exe (Global Assembly Cache Tool) - .NET Framework
Get details about Gacutil.exe, the Global Assembly Cache tool. This tool lets you view and manipulate the global assembly cache and download cache.
cosj
cosjOP13mo ago
ok, looks like adding an AssemblyResolver only works when you don't have a using to the non-private assembly in the .cs file where the AssemblyResolver is. I moved the OtherMethods() to a separate file and now it is working as expected. Thanks for pointing me in the right direction!
arion
arion13mo ago
<a:AA_Neko_Approve:702397810120720475>
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server