.NET Web API Unit Test for model with DbGeography System.DllNotFoundException: Unable to load DLL 'SqlServerSpatial110.dll' - asp.net-web-api2

I have a working ASP NET Web APIs, some of which are using Spatial Data Types using DbGeography. I added this line to the startup code to make it work here.
SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
I have a unit testing project, which is referencing the project mentioned above. When trying to run a test that calls a method that uses DbGeograohy, I get the error "System.DllNotFoundException: Unable to load DLL 'SqlServerSpatial110.dll'".
This error appeared previously in the Web API project, and I solved by adding the line above to the startup class, which basically loads the native Windows library.
I want to learn if it's possible to have a "Startup" class for the tests, where the library would be loaded before executing them.
Cheers :)

Related

Starting Test Server in Memory to test Razor Pages with Playwright

My objective is to start Playwright in memory and run some end to end tests against a in-memory test server.
I've forked Jonathan's great repo which demonstrates exactly what I want to do, however it's using .Net core 3.1 and a MVC project which uses Program.cs & Start.cs.
I have forked and upgraded the project files to .net 6, I have also added a razor project which uses the new top-level statements in program.cs
https://github.com/carlblanchard/InMemoryPlaywrightDemo (Repo has the failure .net 6.0)
https://github.com/JD-Innovensa/InMemoryPlaywrightDemo (Report that works in .net 3.1)
When running the test When_ClickEmployeeMenuOption_Then_EmployeesPageDisplayed_Chrome the following errors occurs.
System.InvalidOperationException: 'A public method named 'ConfigureAutomatedTesting' or 'Configure' could not be found in the 'Program' type.'
I can't see any decent documentation on how to run a test server against a project with a minimal program.cs file.
If anyone knows how to do this please help, it's been really annoying me.
Thanks in advance.

How to use Microsoft.AspNetCore.DataProtection in the console application?

On the page Non-DI aware scenarios for Data Protection in ASP.NET Core I find the code that is intended to be a console application.
It is unexpected for me because console application references to ASP.NET Core library:
using Microsoft.AspNetCore.DataProtection;
When I tried to run it, I expectedly got an error message about missing assembly.
I asked Microsoft about this https://github.com/dotnet/AspNetCore.Docs/issues/17098. But I just got the come-off answer, I just got the reference to the page which is upper in the content: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/introduction?view=aspnetcore-3.1#package-layout
When I run the code in the ASP.NET app, the following statement dataProtectionProvider.CreateProtector("Program.No-DI") returned Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.
Which library contains KeyRingBasedDataProtector?
I included the package https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.Abstractions/, but it expectedly does not contain implementation.
In general, are ASP.NET libraries are suitable to use in console applications?
After all, how to run the code from the page Non-DI aware scenarios for Data Protection in ASP.NET Core?
Thank you
I included 3 packages to the console project:
Microsoft.AspNetCore.DataProtection
Microsoft.AspNetCore.DataProtection.Abstractions
Microsoft.AspNetCore.DataProtection.Extensions
This makes the console project working.

Referencing a COM object via dll in .Net Core 2.0

I am trying to upgrade an old asmx webservice to Web API. Idealy I would like to use .Net Core as that is what we have been developing in. The issue is that the API must communicate with a legacy system using a COM object.
I have copied over the dll (already not ideal, I know) and, the .Net Core API project is able to add it as a reference and the code all compiles. However, on running the code I get the following error when instantiating an object from the dll:
Retrieving the COM class factory for component with CLSID
{CCA0B90B-DFDE-4DD3-9C7B-9769A3F12201} failed due to the following error:
80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
I've tried to do exactly the same thing using the full .Net framework and everything works as expected so I imagine it is an issue with Core - maybe the portability requirements.
It will not be possible to rewrite the integration to the legacy system at this point and I was just wondering if there was any way around this without using the full .Net framework.
Update
I don't know if this will help but I can created a .Net Framework console app which runs the code just fine. when I reference the project from my .Net Core API and call the exact same code the error above still occurs.

Buildserver can not find Entity Framework Sql Provider

We are in the process of switching from Entity Framework 4 to Entity Framework 6. The package manager did its job, and the solution ran it’s tests without a hitch.
However on the build server we get the following error message running the tests:
Initialization method xxx.SetUp threw exception. System.InvalidOperationException:
The Entity Framework provider type
'System.Data.Entity.SqlServer.SqlProviderServices,
EntityFramework.SqlServer' registered in the application config file
for the ADO.NET provider with invariant name 'System.Data.SqlClient'
could not be loaded. Make sure that the assembly-qualified name is
used and that the assembly is available to the running application.
See http://go.microsoft.com/fwlink/?LinkId=260882 for more
information.
If we open the solution locally on the build server, with a Visual Studio installed there, the test run without error again.
We use Cruise Control .Net in combination with SVN as tooling for our continuous integration. Ms Build is used to build the solution and the tests are run with Ms Test.
Has anybody experienced similar problems or has any thoughts on a solution?
This really sucks MS. But fortunately some better programmer than I had the same problem and made this excellent post:
http://robsneuron.blogspot.nl/2013/11/entity-framework-upgrade-to-6.html
Adding this line to our DbContext constructor fixes the problem:
var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
The variable is never used, is not needed when using VS to build, it is only there to make sure Ms Build does its job...

FileNotFoundException when referencing (managed) C++ Assembly from a C# Console App

I am in the process of writing a .NET wrapper for legacy native c++ code. My strategy is to write a CLR class library that wraps the native code.
To test whether the class library is functioning properly, I created two console apps in separate solutions:
A C++ CLR console app
A C# console app
Both of these contain the same simple test code to exercise the class library.
Both apps build as expected. The C++ app runs just fine, but the C# app is giving me a FileNotFoundException when it tries to load my class library.
I have a constraint that forces me to use VS2008 and .NET 3.5. Everything is built with Win32 or x86 configurations.
For both console apps, I am using a project reference to the class library.
In each case, builds copy the dll (and the intermediate files) to the same directory where each app is built.
I tried using the fusion log viewer, but logs are disabled on my machine and I do not have administrator privileges.
Has anyone ever seen this before?
Can someone please point me to a good site that outlines the differences between the way C# and C++ CLR apps load assemblies?
Since this is my first attempt to bridge C++ and C# I assume I am just making a simple mistake somewhere, but I am stumped as to what that is.
I have trolled the internet (including many SO postings) but have yet to find exactly what I need.
Thanks in advance,
Judd