-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement SqlGeography and SqlGeometry for Spatial Types support for .Net Core #30
Comments
@ststeiger does it work on master, rather than 2.1? It seems it was added here |
I'm experiencing the same problem, but when interacting with a table that has a HierarchyId column on it. |
I believe the main issue here is that Microsoft.SqlServer.Types.dll is not available for .NET Core. This library is part of the SQL Server product. @karelz based on this and our exchange on dotnet/core#2273 (comment), I added the "tracking-external-issue". Is this the right label? There are a few workarounds:
|
That's the right label + no assignment + Future milestone + bug label. |
That is correct. While I could probably implement most of the calculations, the results are going to be slightly off. I don't think that's a good idea (for now perform those calculations server-side as part of your query). I'd rather we can get the native part of the spatial types open-sourced, so we can recompile it for more platforms (that would be a lot less work than porting it to C#). |
+1 for implementing Microsoft.SqlServer.Types for .NET Standard. We're attempting to convert our class libraries from .NET Framework to .NET Standard, and the code is littered with references to SqlGeography and SqlGeometry. I appreciate @dotMorten for building his nuget package, but it doesn't implement enough of the original to be viable for us to port to. Trying to convert to using NetTopologySuite looks promising, but would be a huge effort to implement... |
Well, basically, If there were any fully managed C# library that would correctly implement STUnion of two WGS84-polygons, that would have been sufficient. |
@ststeiger @justintoth Are you still running on Windows though? If so, we can still use the native library and call into it from .NET Core. Just wouldn't work on Linux/mac/android/ios etc. |
@dotMorten Yes, we're only running our applications on Windows. I saw in SO threads that you can supposedly reference the .NET framework version of the Microsoft.SqlServer.Types dll's and then call something like this in .NET Core: Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); However, I tried it and the Utilities class doesn't exist in Microsoft.SqlServer.Types so it was a dead end for me. Any idea the proper way to do this? |
@justintoth I was able to make it work in .NET core. I added the <ItemGroup>
<Content Include="$(USERPROFILE)\.nuget\packages\microsoft.sqlserver.types\14.0.1016.290\nativeBinaries\**\*.dll">
<Link>SqlServerTypes\%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="$(USERPROFILE)\.nuget\packages\microsoft.sqlserver.types\14.0.1016.290\content\SqlServerTypes\Loader.cs" Link="Loader.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.Types" Version="14.0.1016.290" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
</ItemGroup> After this, I was successfully able to do the following in a .NET Core 3.0 app (I'm guessing it'll work on earlier versions too): SqlServerTypes.Utilities.LoadNativeAssemblies(".");
SqlGeometry p1 = SqlGeometry.Point(23, 34, 4326);
SqlGeometry p2 = SqlGeometry.Point(23, 35, 4326);
var union = p1.STUnion(p2); Of course this will ONLY work when running your app on Windows as x86 or x64, |
WOAH I even got this working on UWP. And because I don't have to deal with AnyCPU, I don't even need the <ItemGroup>
<PackageReference Include="System.Data.SqlClient">
<Version>4.6.0</Version>
</PackageReference>
<Reference Include="Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(USERPROFILE)\.nuget\packages\microsoft.sqlserver.types\14.0.1016.290\lib\net40\Microsoft.SqlServer.Types.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(Platform)'=='x64'">
<Content Include="$(USERPROFILE)\.nuget\packages\microsoft.sqlserver.types\14.0.1016.290\nativeBinaries\x64\*.dll">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup Condition="'$(Platform)'=='x86'">
<Content Include="$(USERPROFILE)\.nuget\packages\microsoft.sqlserver.types\14.0.1016.290\nativeBinaries\x86\*.dll">
<Link>%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> (just note that the binaries violate store certification checks so you won't be able to put it in the store) |
@dotMorten: Yea well, our Windows-only shop runs on Windows. I think if I remember right, I just did it in SQL on SQL-server, which worked on Linux, too - to my not little surprise. Which would probably indicate that the binaries are ported, or at the very least they found a replacement.
|
@dotMorten How did you add the Microsoft.SqlServer.Types nuget package to your .NET Core package when it's a .NET Framework nuget package? When I try to add it to my .NET Standard class library it fails to add. |
@justintoth i just "did it". Nothing fancy. It warns you it might not be compatible but still goes ahead. Are you targeting 3.0? I can't remember if it's a 3.0 thing to allow that |
@dotMorten It's a .NET Standard 2.0 project. Here is the error I get when trying to download from nuget packet manager.
|
Yeah that wouldn't work. You'd need to target .NET Core App. The title here is about supporting .NET Core, not .NET Standard. |
@dotMorten: If it helps implementing, this is how to do STUnion (with polygons) in NetTopologySuite: If the union is done, all that is needed is converting the geometry to sql-server geometry. Some help for ST_Area covered in dept here: STDistance is trivial (on a sphere) because it can be googled.
I have updated DotSpatial to NetStandard2_0 here: All you need is DotSpatial.Projections and NetTopologySuite for polygons, and DotSpatial.Positioning for distance. |
This is one of the last things keeping us from moving to .net core. Libraries are no doubt going to use this. |
@qcc-na This ended up working for us...
Although this is using the .NET Framework version of Microsoft.SqlServer.Types, it has worked for us so far in our .NET Standard class libraries and .NET Core applications. Good luck! |
@justintoth: Copying a native *.dll will not work on Linux, Mac or Android. At the very least, do a System.IntPtr.Size*8 == 64, and copy either a 32 or 64 bit library from the embedded resources to the ExecutingAssembly directory, and load the library with LoadLibrary (ASP.NET) or use SetDLL ! Realistically, you'll need to have to deploy the right C/C++ runtime libraries from the embedded resources as well, plus any pinvokes need to work with the same signature types on 32 & 64 bit, on all processors, on all operating systems supported by .NET Core/NetStandard2. Just saying implicitly, the approach with a native library is a technically impossible/prohibitive proposition, if it is to be guaranteed to work on (xcopy) deployment (without administrative rights to install anything, such as the C/C++ runtime). If you want this to work with simply copying the dll, you need to statically link the C/C++ runtime, which you can't because you don't have the dll's source code. Come to think of it even further, you'll need to check the dll's license to see if you are even allowed to do so (without an sql-server license, publicly) in the first place ... I read somewhere (don't remember where) that the PostgreSQL entity-framework provider does internally convert the NetTopologySuite geometry types to pgsql-types - perhaps the place to start looking. But i think MS geometry has a reverse rotation of the polygon points. Very expedient. |
As recently announced in the .NET Blog, focus on new SqlClient features an improvements is moving to the new Microsoft.Data.SqlClient package. For this reason, we are moving this issue to the new repo at https://github.com/dotnet/SqlClient. We will still use https://github.com/dotnet/corefx to track issues on other providers like System.Data.Odbc and System.Data.OleDB, and general ADO.NET and .NET data access issues. |
@David-Engel just wanted to give you the heads up on why I moved this issue here. I understand that technically Microsoft.SqlServer.Types isn't a component of SqlClient, and that the SqlClient team doesn't own it, but from the customer perspective, these types are part of the functionality offered by SqlClient. I will continue to talk to the owners on SQL Server about the priority of enabling this. But we need an issue to track it somewhere and I believe it belongs here more than on CoreFx. |
I have seen that the SQL Server team now have a preview of a newer version of Microsoft.SqlServer.Types that is now compatible with .NET Standard 2.1 ( https://www.nuget.org/packages/Microsoft.SqlServer.Types/160.900.6-rc0)). Are there plans to update System.Data.SqlClient or will this only be supported in Microsoft.Data.SqlClient? |
@stevetalkscode Not really that great. There's a runtimes folder in there indicating it still only supports Windows x86 and x64. No ARM64 support, nor ios, android, macos or linux support. |
I did a quick proof of concept using @ErikEJ's latest RC package (see ErikEJ/EntityFramework6PowerTools#103) and using the SqlTypes 16 RC in a .NET 6 console app running on WSL and got a DbGeography written to a SQL database which would normally fail (with the could not load DLL error) |
@stevetalkscode I decompiled the library and I still see a large amount of calls into native code for lots of spatial operations. The serialize/deserialize is all in managed code, so that should just work as you found. Try for instance calling It they were to target .NET6 instead of netstandard2.1, they could use the |
@dotMorten But an Improvement on Windows where you got the error in the OP until now. |
Fair point. I hadn't got as far as testing those bits yet. |
System.Data.SqlClient is in strict maintenance mode. It's not receiving any feature updates. |
(on Windows anyway, more platform in the works I have been told) fixes dotnet#322 fixes dotnet#30
Is this actually complete? Last I checked this only works on x86 and x64 Windows, which isn't in the spirit of claiming .net core support. With .NET 6 running on mac, linux, ios and android, there's not really much benefit to this work until we have a proper crossplatform solution. The nuget package explorer confirms it too with only runtimes supplied for 2 of the 3 Windows architectures and none for anything else: It is in fact rather weird that the library claims to be |
Haha, that's not weird, that's expected. But I guess quick & dirty was cheapter than proper engineering. If you still haven't jumped ship to PostgreSQL/CockroachDB, you have nobody but yourselfs to blame. |
Supporting only Windows is yet another example of why .NET fails to get some love on UNIXy platforms, marketing message is everything is cross platform in modern .NET, yet when we dive into the details there are hurdles like in this case. |
@Wraith2 @cheenamalhotra Guys, is there any plans to support SqlGeometry/SqlGeography on Linux? I think lot of folks would like to know if its even planned.. |
I have no plans to contribute in this area. The sort of response above from @ststeiger illustrates why. If someone else wanted to work on integrating @dotMorten 's open source geopgraphy stuff instead of relying on the closed source windows-only implementation that the sql server team (note: not the team that works on sql client and you see posting in this repo) maintains, then I'd be happy to help. I just have no need for the api personally and no need to expose myself to harmful toxicity. |
@dmytro-gokun Have you tested on Linux and what is not working for you? |
@ErikEJ Methods like STContains(), STArea() etc |
@Wraith2 I understand. I would be happy to help. The problem is that we would need access to that closed source in order to implement it in 100% compatible way. I guess, no one want STContains() which work differently on Windows & Linux. Do you think there is any chance we can get hold of that source code? |
No |
@ErikEJ I see. Then i do not think i will be able to help. Reverse engineering C code or trying to guess what exact algorithms were used is not my favorite passtime :) Does MS at least plan to have this ported to Linux at some point? I guess it would be fair to let community know what to (not) expect. |
It is my impression it is in their plans. |
@dotMorten Would be really nice if someone from MS has actually confirmed or denied this :) |
The previous versions of sqlserver.types also had an "msvcr.dll" c++ runtime in these folders. Can anyone confirm if that is no longer needed? Thanks |
Cannot run Microsoft.SqlServer.Types because \Microsoft.SqlServer.Server\IBinarySerialize.cs is missing in System.Data.
Since no source code is available, and contact owners on nuget yields a HTTP-500, I'm opening an issue here. I'd like to compute a polygon union...
And while you are at it, the version for the full .NET framwork (core also) should also work if SQL-Server is not installed on the machine that Microsoft.SqlServer.Types is executed on...
The text was updated successfully, but these errors were encountered: