CS0246 error while creating incremental build - c#

I have made a small C# application, with 1 solution and 2 projects (test1,test2). The first project have two files Program.cs and function1.cs, while second project consist of only Program2.cs.
Project test1 creates .exe file, and test2 creates .dll file.
The problem is when I try to build it as VS creates .csproj it runs fine, but when I tried to edit that .csproj (or write my own) to implement incremental build (as it was in example on MS sites) it throws an CS0246
The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)
Despite that I have added the references first to test2.csproj, and then to .dll that it creates, and also using the using directive.
The same error occurs when I try to run in command prompt
csc.exe program.cs function1.cs but it can be quickly fixed by adding the /:r with the .dll as in example:
\csc.exe program.cs function1.cs /r:test2.dll
And then it runs fine. I just simply dont know how to acknowledge msbuild of that reference between files. And I'm also forced to used cmd instead of building it inside VS.
Below I put code that I use in my project.
Here's the snippet that I'm adding at the end of test1.csproj to force it into incremental build:
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="#(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="#(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
Here's the code for Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test1
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
string pause;
function1 var = new function1();
a = Convert.ToInt32(Console.ReadLine());
b = var.funkcja1(a);
Console.WriteLine(b);
pause = Console.ReadLine();
}
}
}
Here's function1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using test2;
namespace test1
{
class function1
{
public int funkcja1(int a)
{
int b;
Program2 p2 = new Program2();
b = p2.program2(a);
return (b);
}
}
}
And here's code for Program2.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test2
{
public class Program2
{
public class function2
{
public int funkcja2(int a)
{
return (a + 1);
}
}
public int program2(int c)
{
int d;
function2 var = new function2();
d = var.funkcja2(c) + 1;
return (d);
}
public static void Main(string[] args)
{
string pause;
pause = Console.ReadLine();
}
}
}

CS0246 error while creating incremental build
You should specify the reference when you build it with csc.exe, like References="..\test2\bin\Debug\test2.dll", otherwise, csc.exe could not know the test.dll is referenced by the test1.project, so the target Compile looks like:
<Target Name="Compile" Inputs="#(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="#(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
Besides, AFAIK, you should not edit that .csproj (or write my own) to implement incremental build directly. That because when you build the project test1, Visual Studio/MSBuild has already used incremental build by default, we do not need to incremental build it again. If you are want to implement incremental build, you can create a mini MSBuild Project File to implement incremental build. I have created a new Test.proj under the test1 project folder with following code:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="function1.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup>
<AssemblyName>MSBuildTestSample</AssemblyName>
<OutputPath>Bin\</OutputPath>
</PropertyGroup>
<Target Name="program2">
<MSBuild Projects="..\test2\test2.csproj" />
<Message Text="Target program 2" />
</Target>
<Target Name="Compile" Inputs="#(Compile)" Outputs="$(OutputPath) $(AssemblyName).exe" DependsOnTargets="program2">
<Message Text="Target program 1" />
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$OutputPath)')" />
<Csc Sources="#(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" References="..\test2\bin\Debug\test2.dll" />
</Target>
</Project>
Then we could build this Test.proj file with MSBuild command line.
Hope this helps.

Related

How to avoid certificate issues when setting a SQLite password?

I have C# application (targeting .NET Framework) and I try to set a password on my (completely new) SQLite database.
This is my code
using System;
using System.Data.SQLite;
namespace ConsoleApp10
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin");
var csWithoutPw = new SQLiteConnectionStringBuilder
{
DataSource = "C:\\Databases\\SQLiteWithEFPw.db",
Version = 3
}.ConnectionString;
SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(csWithoutPw);
conn.Open();
conn.ChangePassword("Kabouter");
conn.Close();
Console.WriteLine("End");
}
}
}
This is my csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="Stub.System.Data.SQLite.SEE" Version="1.0.115.6" />
<PackageReference Include="System.Data.SQLite" Version="1.0.115.5" />
<PackageReference Include="System.Data.SQLite.Linq" Version="1.0.115.5" />
</ItemGroup>
</Project>
Unfortunately, my code crashes when connecting with a NotSupportedException referring to some certificate issue I do not understand.
'{cannot find a suitable package certificate file for plugin in
"C:\Users\dacohen\source\repos\ConsoleApp10\ConsoleApp10\bin\Debug\net462\SDS-SEE.exml"
: "invalid file name"} {}'
How can I avoid this problem? I just want to set the password..... I found code online but for .NET Framwork 4.6.2 it does not seem to work or so.
In addition, this is why I set my password before opening.

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

Visual Studio Code : Debugging unit tests works with net50 target framework but not with net48

I'm setting a test environment for a .net library using VS Code. I tried different configurations and after reading this article, I came up with this solution :
Project file :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>
</Project>
Program.cs
using System;
using DebugTest.App;
using DebugTest.Test;
namespace DebugTest
{
class Program
{
static void Main(string[] args)
{
UnitTests test = new UnitTests();
test.Setup();
test.TestAdd();
}
}
}
Pseudo lib
using System;
namespace DebugTest.App
{
public static class Calculator
{
public static double Add(double x, double y)
{
return x + y;
}
}
}
UnitTest.cs
using System;
using DebugTest.App;
using NUnit.Framework;
namespace DebugTest.Test
{
public class UnitTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestAdd()
{
double x = 2d;
double y = 2d;
Assert.AreEqual(9999d, Calculator.Add(x, y));
}
}
}
With this configuration all works fine. I can :
dotnet build
dotnet test : starts the tests in terminal
launch with appropriate configuration in launch.json : If I set breakpoints, I can debug tests through main method
click on "Debug All Tests" or "Debug Test" lens in VSCode to debug tests.
Things got complicated when I tried to change the target framework from net5.0 to net48. I had to change my csproj this way :
New csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
<OutputType>exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>
</Project>
What still works :
dotnet build
dotnet test
What doesn't work anymore :
launch (I didn't forget to change the program path) : The program main is launched as I can see the exception for the failed test. In addition there are other warnings (The target process exited without raising CoreCLR...). If I set breakpoints in Program.cs or UnitTest.cs, they are not hit anymore
click on "Debug All Tests" or "Debug Test" lens : Raises an error in VSCode : Failed to start debugger with a stacktrace mentionning OmniSharp.
I tried many things (cleaning bin and obj folders, etc) and read many forums threads with similar problems but none of them were related to net48.
So, my question is : How can I achieve test debugging in a net48 project in VSCode ?

Mapster.Tool fails generating mapper

Mapster.Tool fails to generate any code.
The problem seems to be a class derived from CosmosClient - which i get from a nuget package.
I get this exception:
Cannot find library: Microsoft.Azure.Cosmos.Client
Unhandled exception. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Azure.Cosmos.Client, Version=3.16.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
at system.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeModule.GetTypes()
at System.Reflection.Assembly.GetTypes()
at Mapster.Tool.Extensions.Scan(CodeGenerationConfig config, Assembly assembly) in D:\git\Mapster\src\Mapster.Tool\Extensions.cs:line 177
at Mapster.Tool.Program.GenerateModels(ModelOptions opt) in D:\git\Mapster\src\Mapster.Tool\Program.cs:line 123
at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
at Mapster.Tool.Program.Main(String[] args) in D:\git\Mapster\src\Mapster.Tool\Program.cs:line 17
System.IO.FileNotFoundException: Could not load file or assembly
'Microsoft.Azure.Cosmos.Client, Version=3.16.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.Azure.Cosmos.Client, Version=3.16.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
Everything works fine when i disable the mapster build target.
Also, Microsoft.Azure.Cosmos.Client.dll exists in the target directory.
Soooo.. what am i doing wrong?
I dont understand why mapster cant load that assembly.
There also seems to be no way to make mapster ignore that class.
Heres the code.
using Mapster;
using Microsoft.Azure.Cosmos;
using System;
namespace MapsterTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class ApplicationDbClient : CosmosClient
{
public ApplicationDbClient() : base("ConnectinString")
{ }
}
[AdaptTo(typeof(MyModelDto)), GenerateMapper]
public class MyModel
{
public string SomeProperty { get; set; }
}
public class MyModelDto
{
public string SomeProperty { get; set; }
}
}
my csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mapster" Version="7.1.5" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.16.0" />
</ItemGroup>
<Target Name="Mapster" AfterTargets="AfterBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tool restore" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster model -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster extension -a "$(TargetDir)$(ProjectName).dll"" />
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet mapster mapper -a "$(TargetDir)$(ProjectName).dll"" />
</Target>
<ItemGroup>
<Generated Include="**\*.g.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
<Target Name="CleanGenerated">
<Delete Files="#(Generated)" />
</Target>
</Project>
Turns out this was a problem with multiple runtime assemblies.
See this github issue.
This has been fixed with mapster.tool version 8.2.0

C# Class System.Data.SqlClient can not load file or assembly

How do I use the SqlClient directive with .NET Standard 2.0?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
Having great difficulty with Visual Studio at the minute, when I run my program I am greeted with the following exception:
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'
Resulting in the application not running, the directive has been installed via NuGet and my class .csproj looks like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
The Form's framewwork is set to .NET framework 4.6.1...
I can not change the class framework to match, and I am not sure if this is causing the error?
The methods - that are causing the error - referenced from the Class:
//Connect to Database
public void Connection()
{
try
{
// Create SqlConnection
connString = "Data Source = xx; Initial Catalog = xx; User ID = xx; Password = xx";
con = new SqlConnection(connString);
con.Open();
}
catch (Exception ex)
{
string error;
error = ex.ToString();
}
}
Option 1) You might have already tried this.
Remove the reference & add it manually.
Option 2) Somehow System.Data.SqlClient dll is missing from your output or build folder
So, try adding post build script.
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy ..\..\..\packages\System.Data.SqlClient\runtimes\win\lib\netstandard2.0\System.Data.SqlClient.dll bin\Debug\appname\" Condition="'$(IsWindows)' == 'true'" />
<Exec Command="cp ../../../packages/System.Data.SqlClient/runtimes/unix/lib/netstandard2.0/System.Data.SqlClient.dll bin/Debug/appname/" Condition="'$(IsWindows)' != 'true'" />
</Target>

Categories