SDK格式VSTO Outlook插件项目编译成功但无法生成.vsto和.dll.manifest文件的原因与解决方法咨询
SDK格式VSTO Outlook插件项目编译成功但无法生成.vsto和.dll.manifest文件的原因与解决方法咨询
我最近在折腾把经典VSTO Outlook插件项目转成SDK风格格式,还想保留完整的VSTO功能。现在两个csproj都能成功编译出可用的.dll,但SDK风格的项目就是生成不了必需的VSTO部署文件(.vsto和.dll.manifest),这可愁死我了,想问问这是为啥,该怎么解决?要是有关于SDK风格和VSTO兼容性问题的解决思路,那就太感谢了!
对了,最终解决方案我已经从相关讨论里找到了,多亏了@lauxjpn的提示~
问题现状
- 正常工作:
BIEM-OldStyle.csproj(经典MSBuild格式):能生成所有必需文件,包括.vsto和.dll.manifest - 异常情况:
BIEM-SdkStyle.csproj(SDK风格):能编译出.dll,但缺失部署清单文件.vsto和.dll.manifest
项目文件对比
正常工作的经典项目(BIEM-OldStyle.csproj)
这个经典格式的项目能完美生成所有VSTO必需文件:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <ProjectTypeGuids>{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{2F40F2E6-067E-42CA-99AE-F2B33A9E1F5F}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>Minimal.BIEM</RootNamespace> <AssemblyName>Minimal.BIEM.Outlook</AssemblyName> <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <TargetFrameworkProfile> </TargetFrameworkProfile> <DefineConstants>VSTO40</DefineConstants> <IsWebBootstrapper>False</IsWebBootstrapper> <OldToolsVersion>4.0</OldToolsVersion> <VSTO_TrustAssembliesLocation>true</VSTO_TrustAssembliesLocation> <!-- VSTO-specific properties for the ClickOnce deployment --> <!-- The ApplicationVersion is used in .VSTO file and in .dll.manifest The ApplicationVersion cannot be added to AssemblyInfo.cs --> <ApplicationVersion>3.2.4.0</ApplicationVersion> <OfficeApplication>Outlook</OfficeApplication> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> <DefineConstants>$(DefineConstants);DEBUG;TRACE</DefineConstants> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> <PropertyGroup> <SignAssembly>true</SignAssembly> <AssemblyOriginatorKeyFile>config\MyPrivateKey.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <PropertyGroup> <SignManifests>true</SignManifests> <ManifestKeyFile>config\BIEM20_TemporaryKey.pfx</ManifestKeyFile> <ManifestCertificateThumbprint>6E88CD736C2FD0A5EEDD529E060650F99DADEB89</ManifestCertificateThumbprint> </PropertyGroup> <PropertyGroup> <DelaySign>false</DelaySign> </PropertyGroup> <ItemGroup> <None Include="config\BIEM20_TemporaryKey.pfx" /> <Compile Include="ThisAddIn.cs"> <SubType>Code</SubType> </Compile> <None Include="ThisAddIn.Designer.xml"> <DependentUpon>ThisAddIn.cs</DependentUpon> </None> <Compile Include="ThisAddIn.Designer.cs"> <DependentUpon>ThisAddIn.Designer.xml</DependentUpon> </Compile> <AppDesigner Include="Properties\" /> <None Include="config\MyPrivateKey.snk"> <Link>MyPrivateKey.snk</Link> </None> </ItemGroup> <ItemGroup> <Reference Include="Accessibility" /> <Reference Include="Microsoft.Office.Interop.Outlook, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <EmbedInteropTypes>True</EmbedInteropTypes> </Reference> </ItemGroup> </Project>
问题原因分析
SDK风格的MSBuild项目默认不会自动导入VSTO相关的MSBuild任务和目标,而经典格式的项目通过ProjectTypeGuids(其中{BAA0C2D2-18E2-41B9-852F-F413020CAA33}是VSTO项目的专属GUID)触发了VSTO部署清单生成的相关逻辑。另外,SDK项目可能缺少一些VSTO部署必需的属性配置和目标导入,导致无法触发部署文件的生成流程。
解决方法
结合@lauxjpn的提示,我们需要在SDK风格的csproj中添加以下配置,手动启用VSTO部署清单的生成:
- 添加VSTO项目类型GUID:在
PropertyGroup中加入ProjectTypeGuids,和经典项目保持一致,让MSBuild识别这是VSTO项目 - 导入VSTO相关MSBuild目标:添加对VSToolsPath下的VSTO目标文件的导入,触发部署文件生成任务
- 保留VSTO部署必需的属性:比如
ApplicationVersion、OfficeApplication、SignManifests等相关配置,这些是生成正确部署清单的必要参数 - 确保正确的目标框架和定义常量:保留
DefineConstants中的VSTO40,目标框架和经典项目保持一致
修正后的SDK风格项目示例
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net472</TargetFramework> <OutputType>Library</OutputType> <RootNamespace>Minimal.BIEM</RootNamespace> <AssemblyName>Minimal.BIEM.Outlook</AssemblyName> <!-- 加入VSTO项目类型GUID,让MSBuild识别为VSTO项目 --> <ProjectTypeGuids>{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <DefineConstants>VSTO40;DEBUG;TRACE</DefineConstants> <VSTO_TrustAssembliesLocation>true</VSTO_TrustAssembliesLocation> <!-- VSTO部署必需属性 --> <ApplicationVersion>3.2.4.0</ApplicationVersion> <OfficeApplication>Outlook</OfficeApplication> <SignAssembly>true</SignAssembly> <AssemblyOriginatorKeyFile>config\MyPrivateKey.snk</AssemblyOriginatorKeyFile> <SignManifests>true</SignManifests> <ManifestKeyFile>config\BIEM20_TemporaryKey.pfx</ManifestKeyFile> <ManifestCertificateThumbprint>6E88CD736C2FD0A5EEDD529E060650F99DADEB89</ManifestCertificateThumbprint> <DelaySign>false</DelaySign> </PropertyGroup> <!-- 导入VSTO相关MSBuild目标,触发部署文件生成任务 --> <PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> </PropertyGroup> <Import Project="$(VSToolsPath)\VSTO\Microsoft.VisualStudio.Tools.Office.targets" Condition="Exists('$(VSToolsPath)\VSTO\Microsoft.VisualStudio.Tools.Office.targets')" /> <ItemGroup> <Compile Include="ThisAddIn.cs" /> <Compile Include="ThisAddIn.Designer.cs"> <DependentUpon>ThisAddIn.cs</DependentUpon> </Compile> <None Include="ThisAddIn.Designer.xml"> <DependentUpon>ThisAddIn.cs</DependentUpon> </None> <None Include="config\MyPrivateKey.snk" Link="MyPrivateKey.snk" /> <None Include="config\BIEM20_TemporaryKey.pfx" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.Office.Interop.Outlook" Version="15.0.4797.1003" /> <!-- 其他必要的引用 --> </ItemGroup> </Project>
验证步骤
- 保存修改后的SDK风格csproj文件
- 在Visual Studio中执行清理解决方案,再执行重新生成解决方案
- 检查输出目录(比如
bin\Debug\),应该能看到成功生成的.vsto和.dll.manifest文件
内容来源于stack exchange
相关产品推荐
相关产品推荐

