.aspnet core 8 将文件 Profile.pubxml 保存至 Properties\PublishProfiles\Profile.pubxml 文件中,然后使用 MSBuild 命令发布项目。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
	<PropertyGroup>
		<!--
		配置说明网址:
		https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-8.0
		-->
		<WebPublishMethod>MSDeploy</WebPublishMethod>
		<LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
		<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
		<LastUsedPlatform>Any CPU</LastUsedPlatform>
		<SiteUrlToLaunchAfterPublish>http://192.168.171.137/</SiteUrlToLaunchAfterPublish>
		<ExcludeApp_Data>false</ExcludeApp_Data>
		<ProjectGuid>5e2b1c2c-2975-4077-b83e-40590d2333cb</ProjectGuid>
		<SelfContained>false</SelfContained>
		<MSDeployServiceURL>https://192.168.171.137:8172/msdeploy.axd</MSDeployServiceURL>
		<DeployIisAppPath>Default Web Site</DeployIisAppPath>
		<RemoteSitePhysicalPath />
		<SkipExtraFilesOnServer>false</SkipExtraFilesOnServer>
		<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
		<EnableMSDeployBackup>true</EnableMSDeployBackup>
		<EnableMsDeployAppOffline>true</EnableMsDeployAppOffline>
		<UserName>X</UserName>
		<_SavePWD>true</_SavePWD>
		<_TargetId>IISWebDeploy</_TargetId>
		<TargetFramework>net7.0</TargetFramework>
		<RuntimeIdentifier>win-x64</RuntimeIdentifier>
		<PublishSingleFile>true</PublishSingleFile>
		<EnvironmentName>Production</EnvironmentName>
		<!--使用不受信任的证书发布到服务器-->
		<AllowUntrustedCertificate>true</AllowUntrustedCertificate>

		<!--配置进程托管类型 inprocess、outofprocess 进程内,进程外-->
		<!--
		差异:
		https://learn.microsoft.com/zh-CN/aspnet/core/host-and-deploy/iis/in-process-hosting?view=aspnetcore-8.0
		-->
		<AspNetCoreHostingModel>outofprocess</AspNetCoreHostingModel>
	</PropertyGroup>
	<ItemGroup>
		<!--配置MSdeploy跳过以下文件的部署-->
		<Content Update="appsettings.json" CopyToPublishDirectory="Never" />
		<Content Update="appsettings.Development.json" CopyToPublishDirectory="Never" />

		<!--将文件复制 到目录-->
		<ResolvedFileToPublish Include="appsettings.Development.json">
			<RelativePath>appsettings.Production.json</RelativePath>
		</ResolvedFileToPublish>
	</ItemGroup>
	<!--
		可以使用.net CLI命令发布:
		dotnet publish DYApi.csproj /p:PublishProfile="Profile.pubxml" /p:AllowUntrustedCertificate=true /p:Username=x /p:Password=123
	-->
	<Target Name="CustomActionsBeforePublish" BeforeTargets="BeforePublish">
		<Message Text="开始发布了..." Importance="high" />
	</Target>
	<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
		<Message Text="发布完成了." Importance="high" />
	</Target>
</Project>

VS生成后移动文件

将以下配置加到 .csproj 文件的最后。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<ItemGroup>
    <FilesToMove Include="$(OutputPath)System.*.dll"/>
</ItemGroup>

<Target Name="CustomAfterBuild" AfterTargets="Build">
    <Message Text="Moving Files @(FilesToMove)" Importance="high"/>
        <Move SourceFiles="@(FilesToMove)" DestinationFolder="$(OutputPath)libs">
        <Output 
            TaskParameter="DestinationFiles"
            ItemName="FilesWritten"/>
        </Move>
        <Message Text="@(FilesWritten)"/>
</Target>
  • FilesToMove 是文件列表,支持通配符。
  • Move 是 MSBuild 内置的 Task,用来移动文件。
  • DestinationFolder 是目标文件夹。
  • FilesWritten 是移动后的文件列表。
  • Message 是 MSBuild 内置的 Task,用来输出信息。
  • Importance 是输出信息的级别,可选值有 high、normal、low。
  • AfterTargets 是目标,可选值有 BeforeTargets、AfterTargets、DependsOnTargets。
  • Target 是一个 MSBuild 目标,用来执行任务。
  • ItemGroup 是 MSBuild 内置的 Task,用来定义文件列表。

参考地址 https://learn.microsoft.com/zh-cn/visualstudio/msbuild/move-task?view=vs-2022

MSBuild生成后使用Dotfuscator混淆

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
	<PropertyGroup>
		<!--Dotfuscator路径-->
		<DotfuscatorPath>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\PreEmptiveSolutions\DotfuscatorCE\dotfuscator.exe</DotfuscatorPath>
	</PropertyGroup>
	
	<Message Text="清理混淆路径文件 $(OutputPath)\Dotfuscated" Importance="high" />
	<Exec Command="rmdir /S /Q $(OutputPath)\Dotfuscated"/>
	<Exec Command="mkdir $(OutputPath)\Dotfuscated"/>

	<!-- 需要混淆文件 -->
	<ItemGroup>
		<FilesToObfuscate Include="$(OutputPath)\X_YuQingFast*.exe" />
		<FilesToObfuscate Include="$(OutputPath)\X_YuQingFast*.dll" />
	</ItemGroup>

	<!--执行混淆-->
	<Message Text="执行混淆..." Importance="high" />
	<Message Text="&quot;$(DotfuscatorPath)&quot; -in:@(FilesToObfuscate->'%(FullPath)', ',') -out:$(OutputPath)\Dotfuscated" Importance="high" />
	<Exec Command="&quot;$(DotfuscatorPath)&quot; -in:@(FilesToObfuscate->'%(FullPath)', ',') -out:$(OutputPath)\Dotfuscated" />
	<Message Text="混淆完成." Importance="high" />

	<Message Text="移动混淆文件..." Importance="high" />
	<!--将混淆后的文件替换掉源文件-->
	<Exec Command="move $(OutputPath)\Dotfuscated\*.* $(OutputPath)" />

	<!--删除混淆目录-->
	<Exec Command="rmdir /S /Q $(OutputPath)\Dotfuscated"/>
</Target>