33

I have the following project structure:

Library1 <--[project reference]-- Library2 <--[ref]-- Executable
--------                          --------            ----------
ContentFile                      .cs files           .cs files
.cs files

All project references have CopyLocal = true.

When I build the project, the ContentFile gets copied to Library2's output directory, but not to Executable's output directory, meaning that the executable is missing ContentFile when the application runs.

Why is the content file being copied to Library2's output directory, but not Executable's? Is there a way to also copy it to the latter (I'd like to do that without build events because I'm sure people will forget that)?

I am looking for a reasonable and maintainable solution; one that requires minimal effort when adding new projects or new indirectly referenced content files, so that it is as unlikely as possible to simply forget doing it.

Using Post-Build events (like xcopy ../Library/bin/Debug/SomeDll.dll bin/Debug); and manually setting the output directory of projects to $(SolutionDir)/bin/... instead of the default (per-project basis), have both quickly become a huge mess. Adding the content files as links in the "main project" was too tedious as well. It would be fine if C# had the same default setting for the output file as Visual C++ does (that is, $SolutionDir/$Platform/$Configuration), but it doesn't.

I've also considered not using the standard MSBuild procedure at all and write a custom build target (like using gcc in Atmel Studio), but I didn't get far at all. Furthermore, I want Visual Studio's standard "Build" and "Debug" commands to work as they usually do.


UPDATE:

Here is more detail on what I am doing.

I have a solution with an Executable project. Furthermore, there is a bunch of projects that you could call Plugins. Executable references all those Plugins via a managed project reference.

Since the plugin projects are tailored to the executable, but may have reusable components, the main functionality is often implemented in an External project, leaving the Plugin project a mere wrapper (not always though).

Said External projects sometimes use native DLLs provided by third parties. Those DLLs are then added to the External project as a content file and have Copy to output dir set to Copy always. So the structure looks like the diagram above:

External <---------------[is not aware of]--------------------+
--------                                                      |
.cs files <--[reference]-- PluginWrapper <--[reference]-- Executable
"Native DLL"               -------------                  ----------
                           .cs files                      .cs files

The weird thing is, that the "Native DLL" gets copied to External's output directory (obviously), as well as PluginWrapper's, but not Executable's.

The developer's workflow would then be to write an External that works as a completely isolated entity (they are being reused very often), wrap it with a PluginWrapper, and then only add a project reference to the PluginWrapper to Executable. I find it odd that this is apparently such an uncommon thing to do.

I thought that maybe editing Executable's MSBuild target XML (to also include indirectly referenced content files) could have solved the problem.

I might want to look into adding the DLLs to the projects as embedded resources as suggested, but embedding native DLLs like that seems weird to me. In the end, changing the developer's workflow by stripping the '[is not aware of]' from the above diagram and adding a project reference to External, as Brenda Bell suggested, might be the most sensible solution, even if not ideal.

Update 2

Note that the embedded resouce idea may not work in all cases. If it is necessary to place a dependency in the executable's directory (not cwd or anything), then this may not work because of missing administrator privileges in the installation folder (to unpack the file from the embedded resource). Sounds weird, but this was a serious issue with one of the 3rd party libraries we were using.

dialer
  • 4,348
  • 6
  • 33
  • 56
  • Could you please be more specific on what solutions you had already considered/ discarded? It would be wasted time if anybody would suggest the things to you that you have already rejected yourself for some reason. – Jens H Dec 04 '12 at 16:12
  • @JensH Sorry, I forgot to add that. Using Post-Build events; and manually setting the output directory of projects to `$(SolutionDir)/bin/...` instead of the default (per-project basis), have both quickly become a huge mess. Adding the content files as links in the "main project" was too tedious as well. – dialer Dec 04 '12 at 16:35
  • Have you tried putting all three projects into the same solution, and setting their project dependancies as appropriate? – StarPilot Dec 04 '12 at 18:17
  • @StarPilot Yes, that's exactly the situation I described in the question. For some reason I don't know, it doesn't work. – dialer Dec 04 '12 at 19:05
  • 1
    You can also put all the content in a separate project and reference it from wherever needed. I'd still go with embedded resources though. – Sten Petrov Dec 10 '12 at 15:45

5 Answers5

23

Add Library1 reference to the Executable project.

Brenda Bell
  • 1,139
  • 8
  • 10
  • 3
    I would have preferred the idea of completely transparent modules, and as such, find the EmbeddedResource idea interesting, but in the long run I feel like this is the most sensible and practical approach. I still find it weird that VS doesn't put all the content files in the main project's output dir, though. – dialer Dec 10 '12 at 18:51
  • This partially worked for me, but now the Content files aren't being copied to Temporary Internet Files. In my case I have a WCF Service that references a class library, that references another Email class library that uses cshtml files as Email Templates. I added the reference to the Email project in the WCF project as you mention, so the template files are now being deployed to the WCF bin. BUT, they aren't being copied to Temporary Internet, so the calling code can't find the resource. Any idea how to fix this? – Tim Jul 31 '13 at 08:44
  • 1
    I ended up using a different solution. My resource files were templates (cshtml files) that I really want to keep as physical files for ease of editing and you get syntax checking. I just found from another post that you can add them as project resource files (Right click the project, Resources, Add Existing File). Now I can access and read them from code! So in my situation and I'm sure others, this is a great solution. Once you have them as a Resource, you can then just read them like this: System.Text.Encoding.UTF8.GetString(Resources.YourResourceFilename); – Tim Jul 31 '13 at 09:39
  • 2
    @dialer: I also found it hard to understand (why does VS behaves like this). [stackoverflow.com/a/7306075](http://stackoverflow.com/a/7306075) helped somewhat to understand. – robert4 Nov 27 '13 at 20:50
  • 7
    IMO, this answer is more a "quick-fix" than an actual solution. I'm having the same problem, and I don't have a better answer, but this fix is ugly. – Wagner Leonardi Mar 06 '14 at 23:26
  • @Wagner I agree, this is still not 100% solved. I think visual studio is simply not a sufficiently competent build system for anything nontrivial. I've put some thought into this since I've asked this question and I'm now convinced that making the output paths a *solution* setting instead of a *project* setting may be the first step to a more elegant system. – dialer Mar 07 '14 at 16:10
  • 2
    I was beating my head up against the wall WRT this very problem today. I eventually came across this open issue in the MSBuild's GitHub repo that I believe is the crux of the problem: https://github.com/Microsoft/msbuild/issues/1054 – Rafael Dowling Goodman Oct 02 '17 at 21:14
5

EDIT:

You can put all content in a separate project, set all its entries to "content" and "copy always" and reference that project from External and Executable

-

IMO you're looking for embedded resource, not for content files.

When you compile Library 1 the content files are placed in its bin folder. When Library 2 is compiled the compiler recognizes referenced code and includes it (Library 1.dll) but the content files aren't recognized since they aren't mentioned anywhere in Library 2. The same goes when linking Library 2 to the executable.

If your content files are relatively small (icons, templates etc) and you don't envision need to edit them if you were to lose the source code then you can embed them as resources and provide a public method to return the contents, such as:

 public static Stream GetResourceContent(string rName){
     string resName = System.Reflection.Assembly
         .GetExecutingAssembly().GetManifestResourceNames()
         .FirstOrDefault(rn => rn.EndsWith("."+rName));
    if(resName!=null)
        return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resName);
    else
        return null;
}

If your content is bound to change, such as templates etc, then consider including a copy with the executable project

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
3

Another option would be to embed ContentFile as a resource inside the Library1 assembly and extract it using Assembly.GetManifestResource().

See these links for more info:

http://www.attilan.com/2006/08/accessing-embedded-resources-using.html

http://blogs.msdn.com/b/alexdan/archive/2007/12/19/loading-embedded-resources-in-c-using-getmanifestresourcestream.aspx

MvdD
  • 22,082
  • 8
  • 65
  • 93
1

One variation on Brenda's answer would be to add the Library1's content file(s) as Link(s) in the Executable project. You'd still have the entries in the project but you wouldn't need manage multiple copies of each file.

0

Could be that you reference Library1 only from an XAML file in that case Visual Studio dont recognice the usage. Add a reference in code as well e.g. by applying the name attribute

Jens
  • 2,327
  • 25
  • 34