How to get ASP.NET MVC Publish to copy over a nati

2019-08-18 17:49发布

问题:

I have created an ASP.NET MVC 4 website in VS 2013. One of the library assemblies I depend on wraps a native dll. I copy that native DLL to the project's output dir in the project's post-build event. That allows me to run the website under IIS Express and that works fine. However, when I go to Publish the site the native DLL gets left in the dirt. Ideally I want either the Win32 or x64 native dll published depending on what platform is currently selected in Visual Studio. Anybody know how to get native DLLs to be scooped up by the Publish command?

BTW I've tried the trick where you "add link" the DLL to the project and set the Build Action to None and set the "Copy to Output Directory" to Copy Always. While that works, it only works for one particular bitness of the native dll. That is less than ideal.

回答1:

You can add conditions to the .csproj (or .vbproj) file by editing it by hand.

  1. Save any work in the project.
  2. Right-click the project node in solution explorer, and click Unload Project.
  3. The whole project will collapse into a single node. Right click that node and select Edit <Project Name>.csproj (or .vbproj).

Scroll down and look for your DLL that you used for. It should look something like:

<ItemGroup>
    <Content Include="bin\Release\x86\My.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

Duplicate it and add a condition. In this case, you want to change it based on platform. Here is one way to do that. Do note that you should analyze the project file to determine the best condition to add for your particular configuration. You should also change the directory to match the (relative) location of your 32 and 64 bit DLL files.

<ItemGroup Condition=" '$(PlatformTarget)' == 'x86' ">
    <Content Include="bin\$(Configuration)\x86\My.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>
<ItemGroup Condition=" '$(PlatformTarget)' == 'x64' ">
    <Content Include="bin\$(Configuration)\x64\My.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

Visual Studio doesn't display this correctly (it will show a broken DLL link and a valid DLL link), but rest assured this method functions. It also helps to understand that a Visual Studio project file is nothing more than an MSBuild configuration file. So, although Visual Studio doesn't support a way to add or display conditions or some other MSBuild options, they all will function correctly if configured according to the MSBuild documentation.



回答2:

You need to include the dll in your project and then once it is there right click on the dll, select Properties, and then make sure "Copy to Output Directory" is set to "Copy Always"