Is there a way to include an assembly referenced using Unity when using publish website?
I'm a using the repository pattern and Unity to manage the dependency to my concrete repository objects. In itself this is not an issue however I am running into a problem when using publish website in both VS and the TFS build process.
I believe the problem is being caused by the fact that although the project which contains the concrete repository objects is referenced in my application, the classes that it contains are never used directly. This is because I am using Unity to create instances of the concrete objects at runtime using the Uni开发者_StackOverflow社区ty config held in my web.config.
The repository project is being compiled when and included in the output when built but when publish website is used in either VS of TFS the repository assembly is missing. Therefore when the web application is deployed to the web server it is not possible to use it as you are greeted with the error:
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
This is because Unity cannot find the missing assembly as you would expect.
So my specific question is; Is there a way to ensure that repository assembly is copied to the output directly when publishing the website in VS and TFS without directly using one of the concrete classes in the project?
So with some digging around I have found a solution to my problem. This soltuion only fixes the problm when building with TFS but to be honest with continuos integration I'm not bothered about publishing from VS.
My inspiration came from an answer given by Mike Hadlow
What's the best way to get TFS to output each project to its own directory?
Basically the solution is to modify the build script for web project so that it copies the missing assembly to the publish website output folder.
Here is what I did:
- I right clicked on my web project in the solution explorer and clicked 'Unload Project'.
- I then right clicked on the unloaded project and clicked 'Edit [Project Name]'
Scrolling right down to the bottom of the file I found the following commented out build target:
<Target Name="AfterBuild"> </Target>
I modified this adding the command to copy the missing assembly:
<Target Name="AfterBuild">
<Copy SourceFiles="$(OutDir)[Missing Assembly Name]" DestinationFolder="$(OutDir)_PublishedWebsites\$(MSBuildProjectName)\bin" SkipUnchangedFiles="true" />
</Target>
Now when building the solution in TFS the missing assembly is included in the bin folder of the published website output.
This solution could be used to copy any other missing content to the published website output. As the files that you want to copy are likely to be different for each project doing it in this way is probably the best option. I am however thinking about creating a custom version of Microsoft.WebApplication.targets which copies minified CSS and script files.
精彩评论