[英]How to have PostSharp included in build process for ASP.NET WebApi project when installed using our own Nuget package?
We have a class library (C#, .NET 5) where we've added a reference to the PostSharp nuget-package (using PackageReference
in our csproj-file) and implemented a PostSharp OnMethodBoundaryAspect
.我们有一个 class 库(C#、.NET 5),我们在其中添加了对 PostSharp nuget 包的引用(在我们的 csproj 文件中使用
PackageReference
)并实现了 PostSharp OnMethodBoundaryAspect
。 This class library is then packaged into a Nuget package to be consumed by our micro services/WebApi-projects.然后将这个 class 库打包成一个 Nuget package 供我们的微服务/WebApi 项目使用。
When I add a reference to this nuget-package in one of our ASP.NET WebApi project I see that references to PostSharp and PostSharp.Redist are also being included along with our class library.当我在我们的 ASP.NET WebApi 项目之一中添加对此 nuget 包的引用时,我看到对 PostSharp 和 PostSharp.Redist 的引用也包含在我们的 class 库中。
However, when adding the OnMethodBoundaryAspect
to one of our functions in the WebApi-project the code in the aspect is not being executed.但是,当将
OnMethodBoundaryAspect
添加到 WebApi 项目中的一个函数时,方面中的代码未被执行。 If I add a reference to PostSharp directly in the WebApi-project it works just fine.如果我直接在 WebApi 项目中添加对 PostSharp 的引用,它就可以正常工作。
How do I make sure that PostSharp is being included in the build process in our WebApi-project when it is being included from our own Nuget-package?当 PostSharp 被包含在我们自己的 Nuget 包中时,我如何确保它被包含在我们的 WebApi 项目的构建过程中?
This answer to another question ( https://stackoverflow.com/a/68470518/182380 ) on StackOverflow mentions that PostSharp.targets
needs to be included in the build process and installing the PostSharp nuget-package will make this happen, but what if I want to include PostSharp from our own Nuget-package and have it included in the build process of the WebApi-project that way, how do I go about fixing that? StackOverflow 上对另一个问题 ( https://stackoverflow.com/a/68470518/182380 ) 的回答提到
PostSharp.targets
需要包含在构建过程中并且安装 PostSharp nuget-package 将实现这一点,但是如果我想从我们自己的 Nuget 包中包含 PostSharp,并以这种方式将其包含在 WebApi 项目的构建过程中,我 go 如何解决这个问题?
This is a result of NuGet's behavior of project references (build-time parts of the package are not taken from referenced projects).这是 NuGet 的项目引用行为的结果(package 的构建时部分未从引用的项目中获取)。
For your own package, it should be enough to have a dependency (in the.nuspec file or appropriately configured csproj) on PostSharp
package, which would then include all PostSharp
package assets in the project where you've referenced your package (this is the default behavior of NuGet dependency if you don't specify any includeAssets
and excludeAssets
).对于您自己的 package,在
PostSharp
package 上具有依赖性(在 .nuspec 文件或适当配置的 csproj 中)就足够了,这将在您引用 package 的项目中包含所有PostSharp
package 资产(这是NuGet 依赖项的默认行为(如果您未指定任何includeAssets
和excludeAssets
)。
In case of PackageReference
in your project file, you also need to set privateAssets
to none
, because the default behavior is Build,Analyzers
:如果您的项目文件中有
PackageReference
,您还需要将privateAssets
设置为none
,因为默认行为是Build,Analyzers
:
<PackageReference Include="PostSharp" Version="6.9.9" PrivateAssets="none" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.