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.
Related
So I'm sure I'm missing something super obvious here, but I can't seem to pinpoint it.
I have a .NET 6 class lib:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>
</Project>
I have a builder in my lib that is trying to register the AddAuthorization service, but it will not resolve for some reason:
namespace MyLib;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
public class MyLibBuilder
{
public IServiceCollection Services { get; }
public MyLibBuilder(IServiceCollection services)
{
Services = services;
}
public MyLibBuilder MapAuthorizationPolicies()
{
Services.AddAuthorization();
return this;
}
}
I've:
triple checked that I have Microsoft.Extensions.DependencyInjection and Microsoft.AspNetCore.Authorization installed
Rebuilt the solution
Unloaded and reloaded the project
And no dice for any. I'm sure it'll click right away once i step away for a day, but it's really bothering me 🤣 What the heck am i missing?
Note: If I change it to a Microsoft.NET.Sdk.Web project, it will resolve, but then I need a Main to run which is moot here since this is just a class lib. Web has to be doing something else that I'm missing but i'm not seeing it...
According to this this was a breaking change.
The below was the recommended solution, though I did try referencing Policy still with no joy.
Either add a reference to Microsoft.AspNetCore.Authorization.Policy or use AddAuthorizationCore instead
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
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 ?
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>
I am building an Outlook WEB Add-in and added both the Microsoft.Azure.Storage.Common and Microsoft Azure.CosmosDB.Table nugets to my project, but the last one shows a warning:
Package 'Microsoft.Azure.CosmosDB.Table 1.1.2' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v.2.0'. This package may not be fully compatible with your project.
I reference them in the code and try to access my table just like it's shown in the documentation [1] [2] as follows, but as usual no Microsoft tech runs with first try as it is described:
using Microsoft.Azure.CosmosDB.Table;
using Microsoft.Azure.Storage;
...
CloudStorageAccount cloudStorageAccount =
CloudStorageAccount.Parse(strAzureCosmosDBConnectionString);
CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
CloudTable cloudTable = cloudTableClient.GetTableReference("users");
TableQuery<UserEntity> tableQuery = new TableQuery<UserEntity>().Where(TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, Account),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, ID)));
await cloudTable.ExecuteQuerySegmentedAsync(tableQuery, null);
I am getting Internal Server Error 500 and the exception of type of type System.IO.FileNotFoundException on this line:
CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
cproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.Azure.CosmosDB.Table" Version="1.1.2" />
<PackageReference Include="Microsoft.Azure.Storage.Common" Version="9.4.0.2-preview" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="2.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.2" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
<PackageReference Include="Microsoft.Graph" Version="1.9.0" />
<!--<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />-->
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<None Include="wwwroot\myHtmlTemplate.html" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
This is the stack trace:
at Microsoft.Azure.CosmosDB.Table.CloudTableClient..ctor(StorageUri storageUri, StorageCredentials credentials, TableConnectionPolicy connectionPolicy, Nullable`1 desiredConsistencyLevel)
at Microsoft.Azure.CosmosDB.Table.AccountExtensions.CreateCloudTableClient(CloudStorageAccount account) in d:\dbs\sh\aplrc\0506_210442\cmd\18\Product\SDK\Table.net\Lib\Common\Table\Account\AccountExtensions.cs:line 21
Message:
Could not load file or assembly 'Microsoft.Azure.Documents.Client, Version=1.20.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
The issue is that Microsoft.Azure.CosmosDB.Table (latest version 1.1.2) does not support .NET Standard. More details are available at https://github.com/Azure/azure-documentdb-dotnet/issues/344.
However, it is possible to use the Azure Storage SDK (WindowsAzure.Storage version 9.2.0).
Below is a sample code to create a new table in CosmosDB/Table account if it does not exist already:
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
namespace CosmosDBTableApp
{
class Program
{
static void Main(string[] args)
{
const string ConnectionString = "{CosmosDB/Table Connection String}";
const string TableName = "{Table Name}";
CreateTableIfNotExists(ConnectionString, TableName).Wait();
}
private static async Task CreateTableIfNotExists(string connectionString, string tableName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(tableName);
await table.CreateIfNotExistsAsync();
}
}
}