Microsoft SqlServer Management: method not found creating a ServerConnection - c#

Microsoft.SqlServer.SqlManagementObjects 161.47027.0
Microsoft.Data.SqlClient 5.0.0
After upgrading Microsoft.Data.SqlClient from 4.1.0 to 5.0.0
I got this error creating a ServerConnection object:
using Microsoft.SqlServer.Management.Common;
var serverConnection = new ServerConnection("localhost", "sa", "mypwd");
System.MissingMethodException: 'Method not found: 'Void
Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Encrypt(Boolean)
It looks for a not implemented set_Encrypt method inside a Microsoft.Data.SqlClient.SqlConnectionStringBuilder
Does it mean Microsoft.SqlServer.SqlManagementObjects has not been updated yet to support Microsoft.Data.SqlClient 5.0.0?
If so do I need to wait for this update before I can upgrade Microsoft.Data.SqlClient 5.0.0 into my project?

You can also upgrade the ServerManagementObjects reference to 170.7.0-preview to resolve. Apparently the issue is with the smo library not the sqlclient library.
https://github.com/dotnet/SqlClient/issues/1702
Their code should be backwards compatible, but they need to be recompiled against MDS 5.0

Below is MyConnectionString"
"Server=tcp:xxx.xxxx.windows.net;Authentication=Active Directory Default;Database=TestDB;TrustServerCertificate=True;MultipleActiveResultSets=True;"
Had the error:
Method not found: 'Void Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Encrypt(Boolean)'. When usingMicrosoft.Data.SqlClient 5.0.1.`
Fixed By: Downgraded the package Microsoft.Data.SqlClient to 4.1.0.and Using Microsoft.SqlServer.SqlManagementObjects 161.47021.0.
Fixed the error. Thank you for this post.

We had the same MissingMethodException when attempting the update of Microsoft.Data.SqlClient 4.1.0 to 5.0.0 in our system. Simply updating Microsoft.Data.SqlClient - independently of Microsoft.SqlServer.SqlManagementObjects - resulted in the exception when constructing the Microsoft.SqlServer.Management.Common.ServerConnection using SQL Server Authentication (not Windows Authentication).
This snippet causes the MissingMethodException if using Microsoft.Data.SqlClient 5.0.0 (worked in 4.1.0):
Return New ServerConnection With {
.ApplicationName = My.Application.Info.Title,
.ServerInstance = connectionParameters.DataSource,
.ConnectTimeout = connectionParameters.ConnectionTimeout,
.LoginSecure = False,
.Login = connectionParameters.UserName,
.Password = connectionParameters.Password
}
For now, we are holding off on updating Microsoft.Data.SqlClient, but went ahead with the Microsoft.SqlServer.SqlManagementObjects update (161.47021.0 to 161.47027.0). I am hopeful that an upcoming Microsoft.Data.SqlClient version will fix this issue.

Related

Running remote PowerShell script using .Net 5.0

The method call in C# is like this -
public void GetKey()
{
WSManConnectionInfo connectioninfo = new WSManConnectionInfo();
var ss = new NetworkCredential("xxx.yyyy\\Administrator", "PassFail2Hehe");
connectioninfo.ComputerName = "<some IP Address>";
connectioninfo.Credential = new PSCredential(ss.UserName, ss.SecurePassword);
//Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
//runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
var re = ps.AddScript("Get-Service");
var results = re.Invoke();
}
}
I am using the NuGet package 'Microsoft.PowerShell.5.ReferenceAssemblies 1.1.0', as it is showing as the recommended package in Visual Studio 2019 for resolving the types of "Runspace", "PowerShell" etc.
However, I am getting the exception when the instance of "Runspace" or "PowerShell" is created. The exception is like this - "Common Language Runtime detected an invalid program."
I found this post and realized that I was getting the warning indeed for the NuGet package -
"Package 'Microsoft.PowerShell.5.ReferenceAssemblies 1.1.0' was restored using '.NETFramework,Version=v4.6.1,
.NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2,
.NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project."
As per suggested in the post, I installed Powershell Core 7.1.3, but the warning and exceptions were not resolved. Then I switched to ".Net Standard 2.0", since it the project was a class library, but nothing changed. The same warning message and exception.
How can I make remote PowerShell call using ".Net Core 5.0" (or ".Net Standard 2.0") ?
If you want to target .Net 5, the correct nuget package to use is Microsoft.PowerShell.SDK

UseMySql in Pomelo throwing exception

So I have seen the strangest behavior with the Pomelo package for .NET Core.
var optionsBuilder = new DbContextOptionsBuilder<AssessmentContext>();
var cn = String.Format(config["ConnectionStrings:AwsConnection"], config["Aws:MySQLPassword"]);
optionsBuilder.UseMySql(cn);
db = new AssessmentContext(optionsBuilder.Options);
I've had the above code in unit tests to set up connect to a MySQL database in AWS. This has worked just fine for about a week now. However, starting today, when it gets to optionsBuilder.UseMySql(cn); now I'm getting an exception.
Assessment.Tests.Data.Contexts.AssessmentContextTests.ConnectToMySQL:
Outcome: Failed
Error Message:
Class Initialization method Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize threw exception. System.TypeLoadException: System.TypeLoadException: Could not load type 'MySql.Data.MySqlClient.MySqlConnectionStringBuilder' from assembly 'MySqlConnector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d33d3e53aa5f8c92'..
Stack Trace:
at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql[TContext](DbContextOptionsBuilder`1 optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
at Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize(TestContext context) in
This has been working fine. I haven't updated any libraries, but it's failing and I'm unable to connect. Any thoughts? I'm using .NET Core 3.1, and pomelo version 3.0.1.
It's a version mismatch. MySqlConnector changed its namespace in version 1.0.0. Since this is a breaking change and Pomelo is SemVer2 compliant, Pomelo is still using MySqlConnector < 1.0.0 (currently 0.69.9).
Later versions of Pomelo (e.g. the current version 3.2.2) explicitly restrict the MySqlConnector version to below 1.0.0, so that using a MySqlConnection version >= 1.0.0 would lead to a compile time error instead of a runtime error.
So to fix the issue, use the latest version of MySqlConnector below 1.0.0.
On a side note, you should consider using EF Core 3.1.8 and Pomelo 3.2.2. There have been many bugfixes for Pomelo since version 3.0.1.

Rebus - System.MissingMethodException : Method not found: 'System.Threading.Tasks.Task Rebus.Bus.IBus.Send'

When calling SendRequest using Rebus.Async, I get this error:
System.MissingMethodException : Method not found: 'System.Threading.Tasks.Task Rebus.Bus.IBus.Send(System.Object, System.Collections.Generic.Dictionary`2)'.
How can I resolve this?
Check your NuGet package versions. Make sure one isn't a prerelease version while the others are stable.
For me, I had installed a Rebus extension that depended on the prerelease version of another Rebus package. Updating all Rebus packages to the latest (prerelease where applicable; latest stable otherwise) versions fixed the issue.

Azure function SDK Causing Newtonsoft issue

We are using azure function SDK in visual studio 2017 in .Net Frame work 4.6. We have added nuget packages
Microsoft.Azure.Devices.Shared 1.15.1
Newtonsoft.Json 12.0.1
AzureFunctions.Autofac 3.0.7 (for dependency injection )
when creating a instance of Twin we are getting a the following exception message
"message": "Could not load file or assembly 'Newtonsoft.Json,Version=10.0.0.0, > Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or
one of its dependencies. The system cannot find the file specified.",
We tried downgrading the package to
Newtonsoft.Json 10.0.1
Then we are getting the same error by replacing missing Newtonsoft.Json version 10.0.1 with Newtonsoft.Json version 12.0.1 .
this is the code we are following .
Twin updatetwin = new Twin();
updatetwin.ETag = "*";
var body = await RequestBody(req);
var newtwin =JsonConvert.DeserializeObject<PropertyViewModel>(body);
I am using
Newtonsoft.Json 9.0.1
with .net framework 4.6 and
Microsoft.NET.Sdk.Functions 1.0.13
You can check required dependencies in nuget package manager-

Simple.Data.PostgreSql throwing 'NpgsqlTypes.BitString' not found

After installing Npgsql and Simple.Data.PostgreSql from NuGet, and then using the following code:
var connectionString = ConfigurationManager.ConnectionStrings["runningJournal"].ConnectionString;
var db = Database.OpenConnection(connectionString);
var userId = db.user.Insert(userName: "foo").userId;
I get this exception:
System.TypeLoadExceptionCould not load type 'NpgsqlTypes.BitString'
from assembly 'Npgsql, Version=3.0.4.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
at Simple.Data.PostgreSql.TypeMap..cctor()
I assume that this is an issue with Simple.Data.PostgreSql since accessing the database from Npgsql works just fine. Is this a bug, or is there some other dependency that I need to manually add? Perhaps something else?
Npgsql 2.x had an NpgsqlTypes.BitString type which was removed in 3.0, which uses .NET's built-in BitArray. It would seem that the version of Simple.Data.PostgreSql which you're using is compiled against Npgsql 2.x, but somehow you're using Npgsql 3.0 in your project. Make sure you're using Npgsql 2.2.7 and not 3.0.x, and perhaps let the Simple.Data.PostgreSql project know to release a new version which works with Npgsql 3.0.

Categories

Resources