What is the best way for multiple solutions/projects to reference a common assembly? - visual-studio-2010

I have several Visual Studio solutions/projects (some VB, some C#) that all reference a common DLL at design time. This DLL does not have to be copied to the output folder as it is only needed while writing code. Every few months this DLL will be updated to a newer version and all of my projects need to reference the updated version.
What is the best way to handle this?

You have to use a Shared Assembly, shared in GAC (Global Assembly Cache).
Say, you have programmed a class, a piece of software, whatever you have, and you want to make it a shared assembly.
First you make it an assembly by creating a new Class Library Project in VS and transporting all of the code to that project. Don’t build/run it yet! Note that we just transport the code but the assembly (the .DLL file) is not actually made yet, because we have not built the project yet.
Before building the assembly, we have to create/share a key by which the assembly is known, in 2 steps:
a) create the key by executing the cmd below in VS cmd:
sn -k "C:[DirectoryToPlaceKey][KeyName].key" b) share it by adding the attribute below to the AsseblyInfo.vb/cs file in the properties folder of your Class Library project:
In VB.Net:
In C#:
[Assembly: AssemblyKeyFile("C:[directory containing the key file][KeyName].key")] (just copy and paste this into the AssemblyInfo.vb/cs file, but write your directory and file name instead).
Now, you MAKE the assembly by building the project. Just build the project (just press F5 once, at least!). By doing so, the .dll file we need (the assembly) is created in the “bin” folder in the same folder of the project.
Now we share it by Copying the .dll file into the GAC (Global Assembly Cache: it’s where all the assemblies are gathered together. The directory is: “C:\windows\Microsoft.Net\assembly\GAC_MSIL” but you don’t need to know that since the tool below does that for you~) by executing the command below in the VS cmd:
gacutil -I "C:[PathToBinDirectoryInVSProject]\myGAC.dll"
YOU'RE DONE! You may now reference and use the shared assembly from all your applications, and whenever you want to update, just update the shared assembly.
Hope that helps!

You can create nuget resources, and use Nuget Server (Lastest version of teamcity support it) - and all updates w'll be handled by nuget.
Other approach is to create common folder and all project point's to dll in this folder (relativly) in this case you need to udate dll in one folder (after rebuild solution dll'll be updated automaticly)
Ofcourse you can allways install dll in GAC (Global Assembly Cache) this is handled by tool called gacutil.exe (in net is lot of example how to handle it).

Related

How to set external dependency on module in Intel Visual Fortran?

I am attempting to recreate a set of Fortran projects using Intel Visual Fortran (Parallel Studio XE 2013) with MS Visual Studio 2010. The projects had formerly been built using Compaq Visual Fortran 6.6, where they were all part of a single workspace. There are seven projects in the VS solution (what had been the workspace in CVF), of which three are static libraries and the other four are console applications that depend on the static libraries.
In addition, I have one Fortran module in a single file, in which all the type definitions reside, and which is included by means of the USE statement in each of the source files. In CVF this was included in the workspace as an "External Dependency", but MS VS 2010 does not seem to have the same property for its "solution". (?)
So here's my question: How do I add to the VS solution a .F90 source file that defines a module MODULENAME in such a way that when other source files call USE MODULENAME, the compiler will pull in the module MODULENAME defined in that file. I'll put it wherever MS VS wants me to put it, but I haven't figured out how to tell it where to look.
ANSWER: Thanks to everyone who answered down below. Based on those responses, I created a new static library project that contained the single source file that defines the module in question. I then set dependencies on this project for every other project that had any source files with the USE MYMODULENAME statement in them. Some of them might not have needed this dependency, if they already depended on other projects that depend on this module; I'm not sure about that. It doesn't seem to hurt to have the redundant dependencies; I assume that the IDE resolves this correctly and doesn't compile the module more than once.
For anyone who is as new to this as I am:
To add the module as a project, I right-clicked the solution and selected "Add New Project". In the dialog that popped up, I selected "Static Library" as the type.
To set the dependencies, I right-clicked the solution, selected "Properties" (there's also a button on the Solution Explorer toolbar) and went to the Dependencies panel.
You either:
Create a separate static library project for the single module that has just the single file, and then make projects that require that module/file depend on that static library project (one of the existing static library projects may already be a suitable container for this).
OR
Simply add the single file to each project that uses the module defined by the file.
Which is best mostly depends on what you think is best.
You can find this in Intel Fortran documentation:
use the -I (Linux OS) or /I (Windows OS) option to specify the path to search and locate the definedmod.mod file
So you need to compile your MODULE and accordingly set include directories in other projects Configuration Properties->Fortran->General->Additional Include Directories

Linked project references aren't being copied to target folder

I have 2 c++ projects in a solution.
ExecB (an executable) depends on ProjA (a dll).
So in ExecB's properties I add ProjA as a reference, and select Copy Local = true.
The problem is, ProjA's dll isn't being copied to ExecB's output folder folder. So the executable obviously doesn't run.
Any suggestions ?
For C++ projects, the Visual Studio template/wizard sets the Output Directory to a subfolder of the solution: $(SolutionDir)$(Configuration)\. This is so the DLL Search Path works well for the developer. It even works if you have added projects to the solution from outside the solution folder hierarchy; The build will put all binaries into the output folder for that solution.
If this isn't working, check the Output Directory property on all platform/configuration combinations of all your projects. Also make sure that the Build Configuration Manager shows that your selected solution build is building all the projects appropriate for the solution platform/configuration.
The Copy Local in Project References that you are trying applies only to referenced .NET assemblies. The docs are ambiguous and too terse on that. (Most often undistinguished use of "assembly" means .NET assembly rather than WinSxS assembly.)

What is the best way to include references to my own assemblies in a project template?

We have developed a library in C#, and now I wish to create a project template to aid in using the library correctly.
I want new projects to include a reference to the library assembly, but would prefer not to have to deploy the assembly to the GAC, or to depend on the assembly residing in some specific location.
What I am thinking is to include the .dll in the project template .zip file. That means it will end up somewhere inside the project folder of new projects. Perhaps in a folder named Lib. Then the reference hint in the project file can point to that folder. Is that a good idea? What problems might I face down the road?
Is there perhaps some mechanism for including such 3rd party libraries in project templates that I'm not aware of? How have you tackled this? Surely I'm not the first.
I have had to address this issue in the past. In one case, it was a logging library that was installed to the GAC, which meant the Reference element simply needed the assembly name. In another case, we installed the library to the file system, created a registry key that contained the location (in case the user got cute and changed the install location on us) and used a project template wizard to look up the registry key and populate a replacement item to have the correct location in the Reference's HintPath. (Note: the template wizard approach requires you to install your wizard's assembly to the GAC, which it sounds like you're trying to avoid...)
If you don't want your library to be installed in either the GAC or a specific location, the approach of including the assembly in the project is pretty much your only remaining option. On the positive side, deployment of your project template is fairly straightforward and you don't have to muck with the GAC, custom wizards, etc. On the negative side, if you ever create a new revision of your library, your users will need to update every project's copy of the library.

How to associate external files with an assembly

Let's say you have a class library project that has any number of supplemental files that also need to be included with the compiled assembly (e.g. simple text files or even a legacy unmanaged DLL that's wrapped by the assembly as an interop layer). While embedding the supplemental files into the assembly itself is relatively straightforward, we have situations where this is not possible or just undesirable. We need to have them as "sidecar" files (i.e. files alongside the assembly, potentially in subdirectories relative to the assembly)
Adding those files to the project with an appropriate value for "Copy to Output Directory" specified appears to be sufficient for projects that are completely self-contained within a solution. But if a separate project in another solution adds a reference to the assembly, it does not automatically pickup its sidecar files. Is there a way in the project to somehow mark the resulting assembly such that anything referencing the assembly will also know it needs to include the associated sidecar files? How do you do this?
You can use al.exe, but there also appears to be a C# compiler option. You want to create a multifile assembly using the /linkresource C# compiler option. Instructions are here, but the command is similar to this:
csc /linkresource:N.dll /t:library A.cs
Where N.dll is a native DLL that will go wherever the managed assembly goes (including into the GAC.) There's a very clear description at the link I provided.
Have you tried creating a setup for your solution ? There's an option of including sidecar files targeting to application installation directory.
Another option would be to include the sidecar files in the Assembly resources and un-wrap them to disk when run for the first time.
What if you create a merge module containing the library plus its dependencies? Your installer will then need to reference this module, but you will ensure all of the necessary files will be present.
Unfortunately there doesn't appear to be a lot of built-in support in Visual Studio for this, although I can definitely see the use case.
If you use Subversion for your source control, then you could link in an external reference as an externals definition. This would bring in the source code, and you'd be making a reference to the necessary assembly as a project reference instead of a DLL reference, and then the copy to output directory rules would come into play.
If that's not possible, another solution would be to include commands in the pre/post-build events of your in-solution project to copy the most up-to-date sidecar files from the remote assembly on a build. Of course this comes with the caveat that it doesn't set itself up automatically when you include the DLL in your project; you have to take manual steps to set it up.
I deal with this some time ago. Its a common problem.
You can create some postbuild actions:
http://www.codingday.com/execute-batch-commands-before-or-after-compilation-using-pre-build-or-post-build-events/
Hope this helps... :)
It appears to me that you're using the wrong type of reference. There are two types of references- Reference and ProjectReference. Reference is an explicit reference to a specific assembly. ProjectReference is a reference to another project (say .csproj).
What you're looking for is ProjectReference. VS and the default MSBuild targets are setup to do CopyLocal. If you set CopyToOutputPath true for your "sidecar" files, any ProjectReferences to this project now will also pull in the same files.
I'm not sure if you can to ProjectReferences across solutions in the IDE. I deal a lot with MSBuild where sln files are not relevant and this is how I deal with it.
What we did in our project is that we created as separate build file to do all those stuffs.
In your build file you can have tags to build your main solution, then add tags to copy files you need after build.
NAnt is also your option, but right now I'm happy using Rake as my build/debug automation.
Since this cannot be integrated within Visual Studio, what I'm doing is I create a task (either in MSBuild, NAnt or Rake), that executes vsjitdebugger.exe in the end to attach it to my Visual Studio when debugging.
These are just my styles for now, you can maybe create your own style.

How does visual studio determine what to copy to the output directory with multi-project solutions?

Let's say we have a solution with the following structure:
Project.DAL - Data access layer,
depends on a lower-level library,
e.g. Oracle.DataAccess w/copy local
= true
Project.BLL - Business logic layer, references Project.DAL as
project
Project.UI - UI layer,
compiles to executable, references
Project.BLL, default project
When Project.UI is compiled, VS is smart enough to copy Project.DAL.dll to the output directory, but it's not smart enough to figure out that I wanted Oracle.DataAccess to be copied to the output directory as well for distribution to clients.
Can anyone explain why this is so? Is it because it sees Oracle.DataAccess in the GAC and assumes that clients will have it in the GAC as well?
It's not that big of a deal, but it's kinda annoying that every time I add a new assembly reference, I have to remember to set it to copy local and add an item to copy it in my build script as well.
One more thing.
When you don't use the referenced DLL in your code at all, it will ignore the CopyLocal and won't copy it to your output directory.
Yes, Visual Studio will copy a DLL to the output path in any of the two conditions below:
The DLL is referenced explicitly with CopyLocal = true
The DLL is referenced without CopyLocal or implicitly through some other referenced DLL and is not in the GAC
The reason why it will not copy-local when the file is in GAC, is that when resolving assembly names the GAC has highest priority, i.e. even if you have a (different) local copy, the version from the GAC will be used.
I suggest you set up a library directory where you put all external assemblies that are referenced. Then you set up an automatic MSBuild script on a computer (or VM) that does not have the Oracle-file gac'ed (nor Visual Studio installed for that sake). That way, the file will be copied to the build, and you will have more control over what is done than when using VS.
I had a strange situation where even though the assembly was a project reference and was referenced with "Copy Local" showing up as "True" in the reference properties window, the DLL was not being copied to the output directory. I had an earlier version of the DLL in the GAC but I didn't see why this should prevent the DLL being copied.
I found that by unloading the project and manually editing the project reference XML as follows:
<ProjectReference Include="..\SomeProject.csproj">
<Project>{11111111-1111-1111-1111-111111111111}</Project>
<Name>Some Project Name</Name>
<Private>True</Private>
</ProjectReference>
The DLL was copied to the ouput directory as expected. I found that just setting Copy Local to True in the properties window meant the <Private> element was completely missing, but in the case of it being set to false it was present with a value of "False".
#RenniePet Here's a link to a blog which describes the method RenniePet described in a comment above (if you don't want to edit your project file manually as #Shaun suggested):
http://blogs.msdn.com/b/jjameson/archive/2009/11/18/the-copy-local-bug-in-visual-studio.aspx

Resources