C# Class library within Class Library - c#

I have a C# solution in VS2010 that contains three projects. This solution has a client project (A) and a server project (B) which both build into applications. The third project (C) is where my classes that are common to both projects go (like a utility library) and this is built into a class library, which I reference in both the client and the server.
Now I want to reference a 3rd party library in my common library (C). I reference the dll and everything seems fine, I am able to use it within that project. However, when I try to use the class I created (that makes reference to the dll) in either the client or server, I get a FileNotFoundException (In regards to the 3rd party library).
I also tried to reference the 3rd party library in my client and server project as well as in the common code project, but the error is still occurring.
I also saw this question here, .NET Multiple Class Library in One Library, which suggests to me that you can reference a class within a class, so how would I go about doing it?

You need to copy the 3rd-party DLL, and all of its dependencies, to the folders containing your executables.
You can do that by setting Copy Local to true in the properties of the references.

Related

Clarify how VisualStudio "PackageReference" works

My test scenario using "PackageReference" mode:
Project A reference project B.
Project B use third party library installed via packagemanager.
The Third party library depends on other third party library (tpl2 for brevity).
I try to use tpl2 from A, does not work (Ok A does not reference tpl2).
But if i install at least one package on A (es. tpl3) then i can also use tpl2 from A without reference it.
I don't understand how this is possible, any clarification?
Usually, if project B is introduced into project A and project B uses the third-party library tpl2, then project A can call the third-party library tpl2 at runtime. But there are two exceptions:
If the third-party library used in project B is used as non-public (internal) or private (private) members, then A project will not be able to call these members, because they will not be exposed in the public API of A project.
In addition, if the third-party library used in project B depends on other third-party libraries that do not exist in project A, then project A may have a missing dependency error when compiling. In this case, project A needs to install and reference all third-party libraries and their dependencies that project B depends on in order to compile and run successfully.
In both cases, the advantages of Visual Studio are very prominent. In a nutshell, when you install tpl3 in project A, it triggers Visual Studio's mechanism for automatically importing package dependencies, which brings in all missing transitive dependencies, including tpl2, and makes them available to project A.
Of course, I can only make these answers because of the limited information you provided. If you have more ideas please speak up, but you need to provide more complete information.

C# Using references/class library in a shared project

I have just learned and created a shared project in visual studio 2017. I have noticed that the shared project did not have the "Reference" to refer to other resources (other projects, class library, …). I even take a look at the .shproj file and saw that it only Import the class I have created inside the shared project.
My problem is that if I want to create an add-in app, I need use the class library to call the necessary API that is exposed by the origin software.
How can I reference/add other project/ class library (or resources in general) to my shared project? Or is that even possible at all?
Part of my problem is also described here reference to a shared project from other shared project
But I need a more general solution. Thank you all for your help.
Long story short: shared projects don't, and realistically can't work that way.
In Visual Studio a shared project is just a container for files - source code, resources, etc - that you can add into other projects. This can be useful in some cases when you want to have the same code (and so on) in multiple projects without putting that code into a library.
Shared projects do not have references, do not have NuGet packages or anything, just the files that they contain. They don't even have the configuration data required to compile any source files they contain, and the compiler won't do much validation of the contents if the shared project isn't included into a full project of some sort.
And since the shared project doesn't have any way of specifying references or packages then you will need to add those references and packages to every project that links to the shared project. The compiler will tell you pretty quick if you miss one.
While it would be nice to have references in shared projects, it turns out to be much less simple than you might think. The same shared project can be included in projects that target different frameworks, platforms and architectures. Let's say you're building some code that will run on iOS, Mono, Windows .NET Framework and .NET Core, with specific code for each target and some shared code. If you try to add a NuGet dependency to the shared project it's going to blow up in your face on at least one of those. Same with most of the references. Add all the references you need for .NET Core and suddenly the other projects don't compile.
You need to add the reference in the project which consumes the shared project.
As an example, let's say you have "Project A" which references "Shared Project B", and you need to use Newtonsoft.Json in "Shared Project B". Since you can't add a reference to the shared project, you install the Newtonsoft Nuget package to Project A and your code in "Shared Project B" will automagically compile.

Code Generation in a Silverlight WCF RIA Services Project

I have a silverlight 4 project that uses wcf ria services to connect to the server. I have a public class that I need to use on both the client (silverlight) and server. What is the best way to accomplish this?
I have tried using the .shared.cs file convention on the server as well as a linked file (original is on the server). The client thought there was a duplicate class so I changed it to a partial class (I would prefer not to). Then it complained about duplicate properties so I tried the solution to this stack overflow question. The problem I run into using this solution is not being able to access my enumerations that are in the original class on the server.
I also tried a creating a class library project that both of the other projects could reference (silverlight client and server). The problem is finding a type of project to create that both of the projects can reference. Silverlight complains about non silverlight projects and vice versa.
Have you considered using the Portable Library Extension tools.
To quote from here, the Portable Class Library project enables you to write and build managed assemblies that work on more than one .NET Framework platform.
I think the best solution here would be to create a new project in a solution (you may call it Helpers, Common, etc) and add a reference in both client and server projects. That way you will use the same class in both of them and if you change the structure of a class it will affect both projects.
I'm not sure about where your problem came from (what do you mean by not being able to access your enumerations ?) however you may find useful to know that .NET supports a feature called assembly sharing that is meant to share some basic type across silverlight and .net. Just keep in mind that you should reference the Silverlight assembly, not the silverlight project. If this break your compilation order, add also a "fake" project reference into your csproj file like this:
<ProjectReference Include="..\MySilverlightSharedProject.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
as documented here

How do I centralize common code in silverlight with a classical Client/Server design?

The setting:
I got a silverlight application and a webservice both want to use the same code.
Here my problem:
If make an normal dll the silverlight
Application say's it can not
reference it.
If I make an Silverlight dll the
Webservice can not reference it.
I don't like duplicate code how can I share code between my Silverlight app and my webservice?
There are several approaches you can take to avoid duplication.
The most fundemental approach would be to use two different projects one targeting the .NET Framework, the other targeting Silverlight. Thus their references point at the appropriate set of assemblies and the Silverlight project defines the #SILVERLIGHT conditional compilation symbol. However they share the same set of code files, that is one will be using linked files. You would toggle around parts the need to be done differently using conditional compilation.
WCF RIA Services also offers a mechanism where you place code in *.shared.cs files in the server side project that will be automatically duplicated into a "Generated_Code" folder in the client side project.
There is a way to share a single set of code and a single output dll between both Silverlight 4 and .NET 4.0. However you will need to limit the references to a very narrow set of files. See Sharing Silverlight Assemblies with .NET Apps
Make two dll projects, one silverlight dll and one normal dll. Have them both reference a third project with the common code which produces a static lib. Having common code as a static lib is good anyway, since you can link it in with any sort of project.

Sharing C# code between Windows and Silverlight class libraries

We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.
A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.
This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.
You cannot set a reference from a Silverlight assembly to a regular .NET assembly but you can do so the other way round.
So create a shared Silverlight assembly and add your code to that assembly. Now you can set a reference fro both your regular .NET and you other Silverlight assembly to the shared Silverlight assembly.
The restriction is that you can only put code in there that would work on both the .NET and Silverlight CLR but that is no different from sharing code.
Since this question has been answered, there is a new solution from Microsoft, Portable Class Libraries. Here is the blog post where they announced it.
I'm just about to start playing round with them for sharing code between silverlight and some server side code I'm writing, so can't add too much past the links at the moment.
Silverlight runtime is different from the normal .NET runtime. So you need to do tricks at the project level to share code between multiple platforms.
Here's how I've done this for Autofac IoC container.
With this approach you do not have to create different projects for each platform being targeted.
PS: there is also a Project Linker tool from the Composite WPF that allows to link Silverlight and WPF projects (creates multiple projects). But it does look messy.
there is a similar problem with XNA projects. Since you can target several different platforms, you're required to have different projects. The reason for this is because the base class libraries that the project references are platform specific, so you can't have just one project.
If you're curious, you can get a bit of insight from this blog:
To recompile the source for another
platform, you need another project.
The reason for this is because the
projects need to reference different
assemblies for both the XNA Framework
and the underlying .NET Framework
(Xbox 360 and Zune use the .NET
Compact Framework), and C# projects
don't provide support for referencing
different assemblies for different
platforms.
Try this http://buildassilverlight.codeplex.com/
I had some dependency issues when referencing to a Silveright class library in .Net.
An alternative way when using Visual Studio 2010 and WCF RIA 1.0:
Create a normal .Net library assembly.
Create a Silverlight class library. In the configuration of the assembly specifiy the first .NET library as the "WCF RIA Service link"
Put your code in the .NET library as "ClassName.shared.cs" files.
WCF RIA will handle copying the file to the Silverlight assembly.

Categories

Resources