I have a class library (TestClassLib) with following classes/interfaces.
IA.cs
A.cs (Implements IA)
IB.cs
B.cs (Implements IB)
A and B have no dependency.
In the TestClasslib.csproj, i have the following XML
<PropertyGroup>
<Builtdir>built</Builtdir>
<AssemblyName>Dabba</AssemblyName>
<OutputPath>Bin\</OutputPath>
<Configurations>Debug;Release;Test</Configurations>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<CSFile Include="*.cs;" Exclude="B.cs" />
</ItemGroup>
<Target Name="Compile">
<Csc Sources="#(CSFile)" OutputAssembly="$(AssemblyName).dll" TargetType="dll" />
</Target>
</Project>
Just want to exclude B.cs when the dll is created.
Is it possible to exclude few classes which has no dependency on other classes ?
If you want to exclude some class in build process,
add the annotations below in .csproj. Then the generated dll is not related to B.cs.
<ItemGroup>
<Compile Remove="B.cs" />
</ItemGroup>
Related
i tried:
<ItemGroup>
<Content Update="config.yml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
also tried the below:
<ItemGroup>
<Content Update="config.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
i also tried replacing the PreserveNewest with true
neither work, it still bundles it inside the .exe file
If this is a Visual Studio Web Site Project
<ProjectGroup>
<!-- Exclude Files -->
<ExcludeFilesFromDeployment>appConfiguration.json;*.yml</ExcludeFilesFromDeployment>
<!-- Exclude Folders -->
<ExcludeFoldersFromDeployment>temp;otherfolder</ExcludeFoldersFromDeployment>
</ProjectGroup>
</Project>
Refrence : https://weblog.west-wind.com/posts/2020/Jul/25/Excluding-Files-and-Folders-in-Visual-Studio-Web-Site-Project
As per Microsoft documentation for Asp.Net 4 the structure of the .wpp.targets file looks like :
<Project ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFolders Include="[semi-colon-separated folder list]">
<FromTarget>[arbitrary metadata value]</FromTarget>
</ExcludeFromPackageFolders>
<ExcludeFromPackageFiles Include="[semi-colon-separated file list]">
<FromTarget>[arbitrary metadata value]</FromTarget>
</ExcludeFromPackageFiles>
</ItemGroup>
</Project>
Reference : https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/excluding-files-and-folders-from-deployment
Edit : after your comment can you try :
<ItemGroup>
<Content Update="*.yml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
keep it like *.yml
To find your framework : click on your project folder in solution explorer then go to properties you will see
I'm using ConfuserEx to obfuscate my app, but it requires whole .dll binary file.
So, is there a way to obfuscate it using cli and then pack it to single file, or
access binary before compressing it to single file, so i can obfuscate it
I tried to do exclude main binary by ExcludeFromSingleFile, but it didn't work
My .crproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<AssemblyName>jay-$(RuntimeIdentifier)</AssemblyName>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Update="$(AssemblyName).dll">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CloudFlareUtilities" Version="1.3.0" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="YamlDotNet" Version="9.1.4" />
</ItemGroup>
</Project>
You need to add it as a post-build event, for example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<AssemblyName>jay-$(RuntimeIdentifier)</AssemblyName>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<!-- runs: TheAppToPassItTo.exe "<path to dll>" -->
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="TheAppToPassItTo.exe "$(TargetPath)"" />
</Target>
<ItemGroup>
<PackageReference Include="CloudFlareUtilities" Version="1.3.0" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="YamlDotNet" Version="9.1.4" />
</ItemGroup>
</Project>
Edited To Add
So, it turns out that the new "single file" executable option doesn't build it's executable from the bin directory, but from the obj directory. This has to be a simple oversight from the .NET team. There are many times where you'd want to modify your executable code before packing it up. What you are asking for is not unreasonable.
We can accomplish this by implementing a kludge until this gets rectified. We will still use the "post-build" job, but we are going to do some string replacement to build the correct path to the executable you want to modify.
This is the new script:
#ECHO off
SET tp=$(TargetPath)
SET tp=%tp:\bin\=\obj\%
ECHO Target file to modify: %tp%
YourObfuscatorEngine.exe "%tp%"
This will get the target path, in my case it is:
D:\Repositories\Source\ConsoleApp2\ConsoleApp2\bin\Release\netcoreapp3.1\win-x64\ConsoleApp2.dll
Then we do a string replace. We replace \bin\ with \obj\. The path will then be:
D:\Repositories\Source\ConsoleApp2\ConsoleApp2\obj\Release\netcoreapp3.1\win-x64\ConsoleApp2.dll
Now when you call your obfuscator engine, it will modify the correct file.
Please keep in mind that if you turn on the PublishReadyToRun option, your path will change to:
D:\Repositories\Source\ConsoleApp2\ConsoleApp2\obj\Release\netcoreapp3.1\win-x64\R2R\ConsoleApp2.dll
Which will make this a tad more complicated. So just keep that in mind if you decide you want to do this.
At the end of the day, your post-build script will look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<AssemblyName>jay-$(RuntimeIdentifier)</AssemblyName>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="#ECHO off
SET tp=$(TargetPath)
SET tp=%25tp:\bin\=\obj\%25
ECHO Target file to modify: %25tp%25
YourObfuscatorEngine.exe "%25tp%25"" />
</Target>
<ItemGroup>
<PackageReference Include="CloudFlareUtilities" Version="1.3.0" />
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="YamlDotNet" Version="9.1.4" />
</ItemGroup>
</Project>
if you only need to modify the file on publish, I suggest hooking into the publish pipeline:
<Target Name="ObfuscateAssembly" BeforeTargets="PrepareForPublish">
<Exec Command="some.exe %(IntermediateAssembly.FullPath)" />
</Target>
In case a larger process is needed, e.g. for this sample all dependencies need to be present for the obfuscator to work on all assemblies, an extended method of hooking into the build process would be after ComputeResolvedFilesToPublishList where the SDK figures out what files are needed to publish.
Here's the full example where the obfuscator works on all the assemblies in a directory:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishReadyToRun>True</PublishReadyToRun>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>False</SelfContained>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<Target Name="CalculateObfuscationInputs" DependsOnTargets="_ComputeAssembliesToPostprocessOnPublish">
<PropertyGroup>
<ObfuscationDir>$(IntermediateOutputPath)obfuscation\</ObfuscationDir>
</PropertyGroup>
<ItemGroup>
<AssembliesToObfuscate Include="#(ResolvedFileToPublish->WithMetadataValue('PostprocessAssembly', 'true'))" />
<AssembliesToObfuscateTemporaryLocation Include="#(AssembliesToObfuscate->'$(ObfuscationDir)%(Filename)%(Extension)')" />
<_PdbsToObfuscateInput Include="#(AssembliesToObfuscate->'%(RelativeDir)%(Filename).pdb')" />
<PdbsToObfuscate Include="#(_PdbsToObfuscateInput)" RelativePath="%(_PdbsToObfuscateInput.Identity)" Condition="Exists(%(_PdbsToObfuscateInput.Identity))" />
<PdbsToObfuscateTemporaryLocation Include="#(PdbsToObfuscate->'$(ObfuscationDir)%(Filename)%(Extension)')" />
</ItemGroup>
<MakeDir Directories="$(ObfuscationDir)" />
</Target>
<Target Name="PrepareForObfuscation" Inputs="#(AssembliesToObfuscate);#(PdbsToObfuscate)" Outputs="#(AssembliesToObfuscateTemporaryLocation);#(PdbsToObfuscateTemporaryLocation)">
<Copy SourceFiles="#(AssembliesToObfuscate);#(PdbsToObfuscate)" DestinationFiles="#(AssembliesToObfuscateTemporaryLocation);#(PdbsToObfuscateTemporaryLocation)" SkipUnchangedFiles="True" />
</Target>
<Target Name="ObfuscateAssembly" AfterTargets="ComputeResolvedFilesToPublishList" DependsOnTargets="CalculateObfuscationInputs;PrepareForObfuscation">
<Exec Command="some-obfuscator.exe $(ObfuscationDir)" />
<ItemGroup>
<ResolvedFileToPublish Remove="#(AssembliesToObfuscate);#(PdbsToObfuscate)" />
<ResolvedFileToPublish Include="#(AssembliesToObfuscateTemporaryLocation);#(PdbsToObfuscateTemporaryLocation)" />
</ItemGroup>
</Target>
</Project>
I have the below MSBuild to generate .cs files from my proto files. The build works fine until I do a rebuild where it complains of Source file 'generated-proto-output/Trade.cs# specified multiple times.
How do I delete my .cs files before building/rebuilding everytime?
Error
Severity Code Description Project File Line Suppression State
Warning CS2002 Source file 'generated-proto-output\ErrorTrade.cs' specified multiple times MyComp.Trade.Model C:\dev\workspaces\trade-model-workspace\model\csharp\MyComp.Trade.Model
build snippet in csproj file
<ItemGroup>
<Protobuf Remove="%(RelativePath)generated-proto-output/**/*.cs" />
<Protobuf Include="../../proto/**/*.proto" ProtoRoot="../../proto/" OutputDir="%(RelativePath)generated-proto-output/" GrpcServices="None" />
<Protobuf Update="../../proto/**/*Service.proto" GrpcServices="Both" />
</ItemGroup>
UPDATE - Complete CSProj file (as requested by Lance)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>TradeStore.Model</PackageId>
<ProtoIncludes>.;../../proto</ProtoIncludes>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<Protobuf_NoWarnMissingExpected>true</Protobuf_NoWarnMissingExpected>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
<PackageReference Include="Grpc" Version="1.19.0" />
<PackageReference Include="Grpc.Tools" Version="1.19.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<FilesToDelete Include="%(RelativePath)generated-proto-output/*.cs" />
</ItemGroup>
<Target Name="DeleteSpecificFiles" BeforeTargets="Build">
<Message Text="Specific Files: #(FilesToDelete)"/>
<Message Text ="Beginning to delete specific files before build or rebuild..."/>
<Delete Files="#(FilesToDelete)"/>
</Target>
<ItemGroup>
<Protobuf Include="../../proto/**/*.proto" ProtoRoot="../../proto/" OutputDir="%(RelativePath)generated-proto-output/" GrpcServices="None" />
<Protobuf Update="../../proto/**/*Service.proto" GrpcServices="Both" />
</ItemGroup>
</Project>
Try adding CompileOutputs="false" to the directive. This will suppress the warning and won't require you to delete files before building csharp protobuf build integration
How do I delete my .cs files before building/rebuilding everytime?
Try the following script with BeforeTargets below:
<Project...>
...
<ItemGroup>
<FilesToDelete Include="MyPath/*.cs" />
</ItemGroup>
<Target Name="DeleteSpecificFiles" BeforeTargets="build">
<Message Text="Specific Files: #(FilesToDelete)"/>
<Message Text ="Beginning to delete specific files before build or rebuild..."/>
<Delete Files="#(FilesToDelete)"/>
</Target>
</Project>
In addition:
Not seeing the whole content of your .csproj, so I can't figure out why the build snippet you use can't work. But a message task may help output some message whether the engine finds the files by your given path.
In visual studio, if you go Tools=>Options=>Project and Solutions=>Build and Run to change the build out verbosity to Detailed, you will see detailed output message after every build and rebuild.Ctrl+Fand type the Target name you will find the details about delete process:
Hope it makes some help for your trouble-shooting.
I'm trying to load a ViewComponent from a different assembly but in the main project I'm getting the error below
InvalidOperationException: A view component named 'Pro.ItemViewComponent' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.
Library.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None
Remove="Views\Shared\Components\ContainerViewComponent\Default.cshtml"/>
<None Remove="Views\Shared\Components\ItemViewComponent\Default.cshtml" />
<None Remove="Views\Shared\_ViewImports.cshtml" />
</ItemGroup>
<ItemGroup>
<Content
Include="Views\Shared\Components\ContainerViewComponent\Default.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Include="Views\Shared\Components\ItemViewComponent\Default.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
<Content Include="Views\Shared\_ViewImports.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<Pack>$(IncludeRazorContentInPack)</Pack>
</Content>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views/**/*.cshtml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
</ItemGroup>
Startup.cs in main project.
var assembly = typeof(Pro.ItemViewComponent).Assembly;
var embeddedFileProvider = new EmbeddedFileProvider(
assembly,
"Pro"
);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Add(embeddedFileProvider);
});
I have followed many articles and some questions and answer in StackOverflow but I didn't find any solution, What should I do to share the ViewComponent from a different assembly?
The problem was really simple in the configuration in Startup.cs, I had to add services.AddMvc().AddApplicationPart(myAssembly); the full configuration is below.
var myAssembly = typeof(MyViewComponent).Assembly;
services.AddMvc().AddApplicationPart(myAssembly);
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Add(new EmbeddedFileProvider(myAssembly, "ComponentLib"));
});
I'm compiling a projet both for net462 and dotnetcore2.0.
I have set the net462;dotnetcore2.0
It seems to work but I need to load an embedded resource like this:
using (var stream = assembly.GetManifestResourceStream("Alcuin.Admin.Api.Beans.TypeTraduction.json"))
using (var reader = new StreamReader(stream))
result = reader.ReadToEnd();
It gives me back a null stream.
Here is my csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net462</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<None Remove="Beans\TypeTraduction.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Beans\TypeTraduction.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Common\Alcuin.Common.Basics\Alcuin.Common.Basics.csproj" />
<ProjectReference Include="..\..\..\..\Common\Alcuin.Common.Graph\Alcuin.Common.Graph.csproj" />
</ItemGroup>
</Project>
Does someone know how to make it work properly?
Thanks.
Nevermind... I just had to remove this section :
<ItemGroup>
<None Remove="Beans\TypeTraduction.json" />
</ItemGroup>
Problem solved.