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.
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.
Related
So I've been stuck on this error for a good week now, and it has been very frustrating
(Impossible to load the file or assembly 'Audio.Default.Switcher.Wrapper.dll' or one of its dependencies. The specified module wasn't found)
I'm working with Visual Studio 2017 and I've downloaded the SoundSwitch project, and everything is working perfectly fine. I'm able to run "SoundSwitch" without any errors.
The way the solution works (as I'm able to understand) is that the "SoundSwitch" C# project is the "master" and it has references to "Audio.Default.Switcher.Wrapper" as well as "SoundSwitch.UI.UserControls"
"Audio.Default.Switcher.Wrapper" as a reference to "AudioDefaultSwitcher"
Both "Audio.Default.Switcher.Wrapper" and "AudioDefaultSwitcher" seem to compile as .dll files
"SoundSwitch.UI.UserControls" cannot be started and is used for display stuff I guess
"SoundSwitch" is the only startable project (not counting mine of course)
The K005_test is MY project, I've added it and it has references (only) to "SoundSwitch" and I'm trying to use the functions from the SoundSwitch project inside K005_test. And so far my K005_test project consists only of
Program.cs (where I get the error by the way)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Exception thrown here
}
}
}
Form1.cs (where I put my code for the test)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This is a test");
var test = SoundSwitch.Model.AppModel.Instance.AvailablePlaybackDevices; //Error this line exists
}
}
}
And when I run my K005_test project, it compiles and runs just fine, except when I click the button Then I get the error I posted above. Also the message box doesn't open.
What I've tried so far :
Flipping everything to "x86" or "Win32", it seems that it might be related a problem with 64bit something
Using references to both "Audio.Default.Switcher.Wrapper.dll" and "SoundSwitch" in K005_test
copy "Audio.Default.Switcher.Wrapper.dll" pretty much everywhere and praying the gods
Trying to understand where it looks for "Audio.Default.Switcher.Wrapper.dll" but I wasn't able to find anything when looking in the exception details
Nothing has helped in any way.
For anybody wondering, I've uploaded my entire project folder here
https://drive.google.com/open?id=1OABjDiZyF0B-1-b15_9lzMrPZ4UpyKqU
I have created a class library to be used in a other project (which is not a .Net project), i have built the solution the dll file was generated, but when i try to explor my dll using dllexp i foud that it's empty.
My class is declared public as you can see bellow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PCLImportCLin.ServiceReference1;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
namespace PCLImportCLin
{
public class ImportCL
{
public int geTroupeau()
{
// Rest of the code
}
}
}
What you are looking for, I guess, is this:
(Assuming you are using Visual Studio) In your project, right-click 'References', go to 'Browse' and then click the 'Browse...' button. Go to your dll path, select it. There you go. Now you can use it like this using Your.Dll.Namespace;
I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).
To avoid requiring a Dll be registered for all users of a spreadsheet, I'm trying to use late binding so that users do not need to add a reference to the Dll.
I've created the Dll in C# with Visual Studio, and even though I've included "using RGiesecke.DllExport;" and used DllExport on a function to return an object containing the functions I need to access in VBA, I still get the error "Run-time error '453': Can't Find DLL entry point CreateDotNetObject in C:\temp\MyFunctions.dll."
The DLL code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Data;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Client;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework;
using Microsoft.TeamFoundation.Common;
using Microsoft.VisualBasic;
using System.Diagnostics;
using RGiesecke.DllExport;
namespace MyFunctions
{
public interface IMyFunctions
{
string GetWorkItemLinkList(string WIIDs);
}
[CLSCompliant(true), ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class MyFunctions : IMyFunctions
{
TfsConfigurationServer server;
WorkItemStore store;
private void TFSconnect()
{
//Code to connect
}
[CLSCompliant(true), ComVisible(true), Description("GetWorkItemLink func")]
public string GetWorkItemLink(int WIID)
{
TFSconnect();
//Code to build return string "message"
return message;
}
[CLSCompliant(true), ComVisible(true), Description("GetWorkItemLinkList func")]
public string GetWorkItemLinkList(string WIIDs)
{
TFSconnect();
//Code to build return string "returnStr"
return returnStr;
}
}
static class UnmanagedExports
{
[DllExport]
[return: MarshalAs(UnmanagedType.IDispatch)]
static Object CreateDotNetObject()
{
return new MyFunctions();
}
}
}
and the declaration in VBA is as follows:
Private Declare Function CreateDotNetObject Lib "c:\temp\MyFunctions.dll" () As Object
But when I try to instantiate an object, I get the error I mentioned above.
Sub test()
Dim o As Object
Set o = CreateDotNetObject()
End Sub
This is my first time attempting to use custom dll functions in Excel without adding a reference in the VBA. The functions do work if I add a reference (early binding), but the DLL is not going to be propogated to everyone who uses the spreadsheet, and I need it to not crash when they run normal functions.
EDIT: Additional info. I just noticed that in addition to the DLL, when I build the solution in Visual Studio I also get an " Object FileLibrary" and an "Exports Library File". When I register the DLL is there anything I should be doing with either the .exp or .lib?
Thanks,
Mike
I was building the solution with the Platform Target in the class library properties set to "Any PC", which apparently does not allow exports. When I switch it to "x86" it totally works.
I have 3 Projects. A, B and X
Project X is a class library and holds my EntityFramework EDMX Database Model.
Project X is referenced in Project A.
I want to use Project A and Project X in Project B.
I found out that is a bit more complicated:
I have 4 Projects. A, B, C and X
Project X is a class library and holds my EntityFramework EDMX Database Model.
Project A is a Silverlight Project.
Project B is a SilverlightApplication.Web Project.
Project C is a class library project.
Project X is referenced in Project B.
Project A has Project B as reference. Project B is auto generated in Project A.
Project C should reference Project A, B and X.
Error Message:
"The Type "ProjectX.Location" exists in both "D:\Projectgroup\ProjectA\bin\ProjectA.dll" and "D:\Projectgroup\ProjectX\bin\ProjectX.dll"
When I look in the auto generated code of project B in Project A, I see some classes of the DatabaseModel (edmx) of Project X, but not all, which are in the namespace of Project X.
try using extern Alias it lets you to wrap assembly inside root-level namespaces named by the alias, which enables them to be used in the same file. though i would recommend you to review the structure of your code so that this could be avoided
Referencing the same assembly in the form
/==>A==\
/ \
B=======> X
is not a problem at all, and should not cause any errors or warnings... as long as the X is the same identity via both routes. So basically make sure you only have one version of X. This is easier if you don't have strong-naming enabled, but even with strong-naming it should just be a case of ensuring your references are correct.
I think your problem that you have defined two different types with same name in the same namespaces but different assemblies.
For example I got mentioned error in following cases:
ClassLibrary1.Class1.cs (Assembly1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
}
}
ClassLibrary2.Class1.cs (Assembly2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
}
}
Program.cs (Assembly3)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class1 a;
}
}
}
Therefore, please make sure that you haven't the same name type for different assemblies.