C# Winform with CefSharp app crash on publish version - c#

I create an C# Winform application with CefSharp Project.
The application works great in the debug mode but when i try to publish it with VS2013 it's crash before the application start to run.
This is my program.cs file:
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += Resolver;
LoadApp();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void LoadApp()
{
var settings = new CefSettings();
settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
"CefSharp.BrowserSubprocess.exe");
settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + #"\TelegramParser\CEF";
settings.IgnoreCertificateErrors = true;
settings.WindowlessRenderingEnabled = true;
settings.SetOffScreenRenderingBestPerformanceArgs();
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
Application.Run(new Form1());
}
private static Assembly Resolver(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64" : "x86",
assemblyName);
return File.Exists(archSpecificPath)
? Assembly.LoadFile(archSpecificPath)
: null;
}
return null;
}
When i checked the publish folder i only see this files:
And from the CefSharp Wiki i understand that there are couple of files that missing. Any idea what can be the problem?
I run and publish the project with Visual Studio 2013.
I'm using Any CPU for the compile.
EDIT
<ItemGroup>
<Content
Include="$(SolutionDir)packages\cef.redist.x86.3.3497.1841\CEF\**\*" Exclude="$(SolutionDir)packages\cef.redist.x86.3.3497.1841\CEF\x86\**\*;$(SolutionDir)packages\cef.redist.x86.3.2526.1362\CEF\locales\**\*.pak">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\cef.redist.x86.3.3497.1841\CEF\**\en-GB.*;$(SolutionDir)packages\cef.redist.x86.3.3497.1841\CEF\**\en-US.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\cef.redist.x86.3.3497.1841\CEF\x86\**\*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\CefSharp.Common.69.0.0\CefSharp\x86\**\CefSharp.BrowserSubprocess.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content
Include="$(SolutionDir)packages\cef.redist.x64.3.3497.1841\CEF\**\*" Exclude="$(SolutionDir)packages\cef.redist.x64.3.3497.1841\CEF\x64\**\*;$(SolutionDir)packages\cef.redist.x86.3.2526.1362\CEF\locales\**\*.pak">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\cef.redist.x64.3.3497.1841\CEF\**\en-GB.*;$(SolutionDir)packages\cef.redist.x64.3.3497.1841\CEF\**\en-US.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\cef.redist.x64.3.3497.1841\CEF\x64\**\*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="$(SolutionDir)packages\CefSharp.Common.69.0.0\CefSharp\x64\**\CefSharp.BrowserSubprocess.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<Visible>false</Visible>
</Content>
</ItemGroup>
I added this and now the application crash with this error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in App.exe
Additional information: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found.

Any CPU is the issue.
You can select either x64 or x86 platform.
Make sure you have added reference to CefSharp.Winforms nuget
Make sure that the platform of solution is set to x86 / x64 explicitly instead of Any CPU.
Add below runtime binding in the configuration file:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x86"/>
</assemblyBinding>
</runtime>
CSProj file should have below property set:
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
Refer this article - which explains step by step process of setting up CEFSharp browser in winforms.

Related

How about building a C# unit test project in CMake?

I used the following methods, but I failed.
How to solve it? Or is there another way?
Looking forward to your reply. Thanks!!!
This is my CMake project:
dotnet_xunit_template.csproj.in
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectName>#PROJECT_NAME#</ProjectName>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
#source_files_string#
</ItemGroup>
</Project>
UnitTest1.cs
using Xunit;
public class UnitTest1
{
[Fact]
public void Test1()
{
int i = 0;
i = 2;
}
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(xunitproject)
set(source_files_string)
file(GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cs)
foreach(item ${SRC_FILES})
# <Compile Include="exe_test.cs" />
set(source_files_string
"${source_files_string}<Compile Include=\"${item}\" />
"
)
endforeach()
configure_file(dotnet_xunit_template.csproj.in ${PROJECT_NAME}.csproj #ONLY)
include_external_msproject(
${PROJECT_NAME} ${PROJECT_NAME}.csproj
)
Build the project into Visual Studio 2017, and the error message is as follows:
Your project does not reference ".NETFramework,Version=v4.0" framework. Add a reference to ".NETFramework,Version=v4.0" in the "TargetFrameworks" property of your project file and then re-run NuGet restore. ZERO_CHECK C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets 186

Source Generators dependencies not loaded in Visual Studio

I am working on source generator and I have problems with dependencies:
It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Flurl.Http, Version=3.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'
There is a lot of information on how to pack dependencies into nuget, but I reference analyzer project directly like this:
<ProjectReference Include="SG.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
In analyzer project I added <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> and all dependencies are available in output directory, but VS is not using that directory - it uses AppData\Local\Temp\VBCSCompiler\AnalyzerAssemblyLoader\[...] instead and it copies only one DLL there.
What can be done to make that work?
I found the way to make it work more or less reliably with some hacks.
Before that I also tried ILMerge, but it didn't work (missing method exceptions).
Solution:
First of all I embeded dependencies in source generator assembly like this:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" Visible="false" />
</ItemGroup>
Then I created AssemblyResolve handler for AppDomain (static constructor in generator class) like so:
AppDomain.CurrentDomain.AssemblyResolve += (_, args) =>
{
AssemblyName name = new(args.Name);
Assembly loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().FullName == name.FullName);
if (loadedAssembly != null)
{
return loadedAssembly;
}
string resourceName = $"Namespace.{name.Name}.dll";
using Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
if (resourceStream == null)
{
return null;
}
using MemoryStream memoryStream = new MemoryStream();
resourceStream.CopyTo(memoryStream);
return Assembly.Load(memoryStream.ToArray());
};
The way that this should work is outlined in the source-generators cook-book, with their example being:
<Project>
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Generates a package at build -->
<IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency -->
</PropertyGroup>
<ItemGroup>
<!-- Take a private dependency on Newtonsoft.Json (PrivateAssets=all) Consumers of this generator will not reference it.
Set GeneratePathProperty=true so we can reference the binaries via the PKGNewtonsoft_Json property -->
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" PrivateAssets="all" GeneratePathProperty="true" />
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<!-- Package the Newtonsoft.Json dependency alongside the generator assembly -->
<None Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>
However, it is my experience that this is still not reliable or robust; this is acknowledged, and I get the impression that improving this experience is part of future planning, but: right now, honestly it is better off simply not using dependencies outside of the core framework and the Roslyn libraries that are already loaded. If your dependencies are in the same repo as the analyzer/generator, you might be able to simply suck in the code during build, for example from here:
<ItemGroup>
<!-- compile what we need from protobuf-net directly; package refs cause pure pain in anaylizers-->
<Compile Include="../protobuf-net.Core/**/*.cs" Link="protobuf-net.Core"/>
<Compile Remove="../protobuf-net.Core/obj/**/*.cs" />
<Compile Include="../protobuf-net.Reflection/**/*.cs" Link="protobuf-net.Reflection"/>
<Compile Remove="../protobuf-net.Reflection/obj/**/*.cs" />
</ItemGroup>
There is a definitive answer to this now. The Source Generator Samples contain an example .csproj which has the required setup.
I used this to make a source generator with Sprache, works great.
Pasting the example .csproj here for reference:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftNetCompilersToolsetVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<!-- Generator dependencies -->
<PackageReference Include="CsvTextFieldParser" Version="1.2.2-preview" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Handlebars.Net" Version="1.10.1" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn)</GetTargetPathDependsOn>;GetDependencyTargetPaths
</PropertyGroup>
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGCsvTextFieldParser)\lib\netstandard2.0\CsvTextFieldParser.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGHandlebars_Net)\lib\netstandard2.0\Handlebars.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>
</Project>
ProjectReference is not the same as PackageReference!
The GeneratePathProperty will not take any action for ProjectReference because it is not a package.
So, you just have to add the TargetPathWithTargetPlatformMoniker pointing to your local DLL into Source Generator csproj.
Example with default output locations:
Folder structure
- Solution Folder
| - MySolution.sln
| + MyProject
| | - MyProject.csproj
| | + bin
| | | + Debug
| | | | + netstandard2.0
| | | | | - MyProject.dll
| | | + Release
| | | | + netstandard2.0
| | | | | - MyProject.dll
| + MySourceGenerator
| | - MySourceGenerator.csproj
MySourceGenerator.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyProject\MyProject.csproj" PrivateAssets="all"/>
</ItemGroup>
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="..\MyProject\bin\$(Configuration)\netstandard2.0\MyProject.dll" IncludeRuntimeDependency="false"/>
</ItemGroup>
</Target>
</Project>

Azure Function (using DI) cannot remove "/api" route prefix

So I'm aware of these two questions that seem to be asking the same thing:
How to remove the word ‘api’ from Azure functions url
How to change the base "/api" path on Azure Functions (v2)?
However, I can still not get rid of the "api" prefix in my route.
My host.json looks like:
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
And on my HttpTrigger I'm setting my custom route:
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "myapp")] HttpRequest request,
However, when I run the app locally the end point is coming up as:
[POST] http://localhost:7071/api/myapp
If I change my host.json to:
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": "something"
}
}
}
My app is now running on:
[POST] http://localhost:7071/something/myapp
So it appears that giving an empty string "" is just not working. Any ideas? (I've done all the usual stuff: clean solution, delete bin/obj folder etc.)
FYI from my function app I'm using:
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.7" />
EDIT:
I'm also referencing these packages from the function app (though I don't see how this would cause this problem):
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.1" />
EDIT 2
I've narrowed down the problem to this code that is called in Startup.cs:
IConfiguration configuration = builder.Services.BuildServiceProvider().GetService<IConfiguration>();
IConfiguration combinedConfig = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddAzureKeyVault(kvEndpoint, kvClient, new DefaultKeyVaultSecretManager());
.Build();
builder.Services.AddSingleton(combinedConfig);
// <-- this line causes the prefix "" to revert to "/api"
It essentially is adding key vault as a configuration provider to the stack of providers all ready there. (Note it doesnt matter what .Add methods I call on configuration builder, its the registration that is causing a problem). Is there another way to write this maybe?
So the mistake I seem to have made was very small. In the following code:
IConfiguration configuration = builder.Services.BuildServiceProvider().GetService<IConfiguration>();
IConfiguration combinedConfig = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddAzureKeyVault(kvEndpoint, kvClient, new DefaultKeyVaultSecretManager());
.Build();
builder.Services.AddSingleton(combinedConfig);
ConfigurationBuild.Build() actually returns a IConfigurationRoot, NOT a IConfiguration (IConfigurationRoot is a superset of IConfiguration). So when registering it I was loosing something (probably config provider information).
Simply changing:
IConfiguration combinedConfig
to
IConfigurationRoot combinedConfig
fixes the problem (or you can use var, which I probably should have!).
Though this fixes the problem I am still a little confused why before changing the routePrefix in host.json to some non-empty string works but setting it to empty string does not. Would have thought simply having the setting in the host.json at all would just apply the value and not having it there would mean reverting to the default "api".
I tried, and it's working!
I used VS2019, Created Azure function default project with HTTP trigger and remove route prefix from host.json
Proj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.31" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
After updating nuget packages with latest version, it's working.
Proj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="3.0.16" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.7" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

.NET Azure Functions - Dependency Injection Issue

On Startup.cs in my Azure Function v2 project:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using MyCompany.MyLib.Contracts; //namespace from external library
[assembly: FunctionsStartup(typeof(Startup))]
namespace Test
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient(typeof(Logging.ILogger<>), typeof(Logging.Logger<>));
builder.Services.AddTransient<IUserLogic, UserLogic>();
builder.Services.AddTransient<IBillingLogic, BillingLogic>(); //---> loads up from above referenced "MyCompany.MyLib.Contracts" namespace and this namespace is from externally referenced class library but with in same solution
}
}
}
The above code with my own custom classes within function app project like "EmailLogic", "Logger" works fine.
But the moment I added up custom classes to services container like "BillingLogic" from external C# library project which is added as reference project from the existing visual studio solution it throws up below issue:
"A host error has occurred during startup operation '945918c0-af3a-4d50-ab1d-ac405d4f1c7b'. [2/3/2020 2:11:02 PM] MyFunction.FunctionApp: Could not load file or assembly 'MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly ''MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
If these lines from "referenced external projects" are removed,
using MyCompany.MyLib.Contracts;
builder.Services.AddTransient<IBillingLogic, BillingLogic>();
startup.cs works as expected but referring this external class from referenced project is must for my solution.
My Azure function csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.Storage.Queue" Version="11.1.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Extensions.AzureStorage" Version="1.1.4" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">
<Private>true</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
MyCSharpLib.DataStore.csproj file:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.6" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
<PackageReference Include="Polly" Version="5.3.1" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyContractLib.Contracts\MyContractLib.Contracts.csproj" />
</ItemGroup>
</Project>
MyCSharpLib.DataStore
.\MyContractLib.Contracts\MyContractLib.Contracts.csproj
My Azure function csproj file:
<ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">
so
using MyCompany.MyLib.Contracts;
is coming through the ref to DataStore which then has ref to MyContractLib.Contracts
But it is not coping the dll as its silly, so either get Azure function csproj to ref MyLib.Contracts
or do this
How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?
which is on all your std libs add
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
so on both your standard libs
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
if this does not work i will delete

MSBuild. Create EmbeddedResource before build

I want to embed local references in the assembly before compiling the main unit. But the written target does not work.
<Target Name="EmbedLocal" BeforeTargets="CoreCompile">
<Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>
<ItemGroup>
<EmbeddedResource Include="#( ReferencePath->WithMetadataValue( 'CopyLocal', 'true' )->Metadata( 'FullPath' ) )"/>
</ItemGroup>
<Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
</Target>
#(EmbeddedResource) at this moment contains valid list of paths.
Update:
Now my import file contains:
<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<EmbedLocalReferences Condition=" '$(EmbedLocalReferences)' == '' ">True</EmbedLocalReferences>
</PropertyGroup>
<Target Name="EmbedLocal" BeforeTargets="ResolveReferences" Condition=" '$(EmbedLocalReferences)' == 'True' ">
<Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>
<ItemGroup>
<EmbeddedResource Include="#(ReferenceCopyLocalPaths->WithMetadataValue( 'Extension', '.dll' )->Metadata( 'FullPath' ))">
<LogicalName>%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
<Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
</Target>
</Project>
It works fine. Output assembly contains all .dll references as EmbeddedResource.
MSBuild. Create EmbeddedResource before build
You can try to use BeforeBuild action to the csproj file to include the embedded resources:
<Target Name="BeforeBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>
Now MSBuild will add this file as embedded resource into your assembly.
Update:
Thanks #Martin Ullrich. He pointed out the correct direction, we could use <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"> in the Directory.Build.props to resolve this issue. You can check if it works for you.
<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>

Categories