Xamarin shim assemblies and PCL - c#

I want to be able to convert some existing code which uses standard.csproj, droid.csproj and ios.csproj so that they are all based on PCL thus simplifying the solution.
I have read and looked at the shim solution suggested here:
http://slodge.blogspot.co.uk/2012/12/cross-platform-winrt-monodroid.html
But:
1: I am unsure how to actually use the shim assemblies - do I reference these instead of the GAC versions?
2: There is a lot of mention that iOS isn't working yet. Is this the case?
w://

The official support for PCLs from Xamarin is due in Alpha any day now - expect it before the end of June 2013.
Until then, you can see how to use PCLs by including the shim assemblies from MvvmCross - take a look at:
the PortableSupport shims source - https://github.com/slodge/MvvmCross/tree/v3/PortableSupport
the Nuget 2.5 PortableSupport package - https://www.nuget.org/packages/MvvmCross.PortableSupport/
the 25 videos on http://mvvmcross.wordpress.com/ all of which use PCLs
the accompanying source code for those videos - the .csproj files will show you how the shims are used - https://github.com/slodge/NPlus1DaysOfMvvmCross
lots of other PCL based x-plat samples on https://github.com/slodge/MvvmCross-Tutorials/
For additional shims, watch questions like TPL on PCL for MvvmCross for PCL Profile 78 (I know people have got this working - just waiting for them to answer the question...)
When Xamarin official PCL support ships then I anticipate that they will ship official shims and we can retire the MvvmCross ones. However, no-one at Xamarin has given me any answer about this - for latest on this, maybe see status of https://bugzilla.xamarin.com/show_bug.cgi?id=8035

Related

Microsoft.AspNet.WebApi.Core and outdated libraries

I am following a tutorial (this one in particular https://www.twilio.com/docs/usage/tutorials/how-to-secure-your-csharp-aspnet-web-api-app-by-validating-incoming-twilio-requests) in order to create an attribute for securing my Web API webhook. I am using .NET 6 preview 7 and it seems to me that in the tutorial they are using somewhat outdated libraries (correct me if I'm wrong), such as Microsoft.AspNet.WebApi.Core and System.Web.Http. I added these libraries to my project and after several hours I got it to work and my endpoint is seemingly secured. Now, as mentioned I added the Microsoft.AspNet.WebApi.Core library as a reference and Visual Studio 2022 even gives me an warning saying it might not be able to fetch the correct version of it due to version mismatching.
My question is: can this outdated library (or dependencies) interfere with my other .NET 6 API's/Web API Controllers and web infrastructure in ASP.NET Core? Typically to load other code, making me unable to leverage new functionalities, performance boosts and other improvements of .NET 6. There are a lot of DI going on and I feel like I do not have a full understanding of how everything is loaded and which libraries are actually used when I deploy my app to IIS. Ideally I would like to port this attribute class to use the latest .NET 6 libs but many of these objects and properties are handled differently in the newer libraries as I understood.
Thanks Daniel A. White for the help. My problem was that my endpoint is hosted on sub path and therefore the RequestRawUrl method in the example fetched slightly wrong URL.

Xamarin - Android - SQLite - Which C++ Library?

A bit of background...
SQLite is an open source C++ library compiled for multiple platforms. There are several commonly used C# wrappers for this library on several different platforms. Some of the common ones are:
https://github.com/praeclarum/sqlite-net.git
https://github.com/mattleibow/Mono.Data.Sqlite.git
https://github.com/oysteinkrog/SQLite.Net-PCL.git
Here is my one which is a stripped back fork from SQLite.Net-PCL.
https://github.com/MelbourneDeveloper/SQLite.Net.Standard.git
There are multiple versioning issues around SQLite on the different platforms, but recently, a big version issue came in to play on the Android platform. You can read about it here:
http://ericsink.com/entries/sqlite_android_n.html
According to Xamarin:
Note: Due to a change by Google, Android N will now only permit linking to NDK-provided native libraries. libsqlite.so is not an NDK-provided native library. Consequently, existing apps using e.g. Mono.Data.Sqlite.dll will crash when running on Android N. This may include other SQLite-using assemblies, not distributed with Xamarin.Android.
Xamarin.Android 7.0, which is part of Cycle 8, updates Mono.Data.Sqlite.dll to include a custom built version of libsqlite.so, named libsqlite3_xamarin.so.
https://developer.xamarin.com/releases/android/xamarin.android_7/xamarin.android_7.0/
So, my questions are: which Android C++ library should we be using for Android? Does the version depend on the version of the Android OS installed? Where do we get the C++ library from?
It sounds as though Google will only allow a customized version of the library. I guess this means that they will only accept signed libraries and the one compiled from Xamarin will be accepted. If this is the case, how do we get this library and package it up in a .NET Standard DLL, or include it in an Android app package?
I ask this question because this issue has been pointed out in my library here:
https://github.com/MelbourneDeveloper/SQLite.Net.Standard/issues/5
I feel like this is connected to the versioning issue. Anything that can shed some light on this would help. In my repo mentioned above, I have created a branches called Issues/5 which uses a different C++ library called libsqlite3_xamarin which seems to be bundled with Mono.Data.Sqlite but I still get the above issue when my phone goes to sleep.

Consuming packages from Xamarin projects

I'm trying to make Npgsql, the PostgreSQL provider for .NET, available for consumption by Xamarin users. I've explored various methods for doing this and seem to be blocked.
First, a Xamarin project can consume packages which target certain PCL profiles. The problem is that none of of these PCL profiles contain System.Data, which Npgsql requires, even though Xamarin itself allows you to use System.Data.
Second, Npgsql already supports the .NET Platform Standard (version 3), and the documentation on the standard contains the following sentence:
If a library targets .NET Platform Standard version 1.3, it can only run on .NET Framework 4.6 or later, .NET Core, Universal Windows Platform 10 (UWP), and Mono/Xamarin platforms.
However, trying to go down this path yielded several errors (this question is one of them). My guess is that the Xamarin tooling isn't yet fullynetstandard-aware.
Finally, it seems possible to import Npgsql as a shared project inside the user's solution, but this seems like a very hacky and wrong solution - it bypasses NuGet entirely.
Does anyone have any info on this?
That npgsql package seems to be depending on pre-release packages (RC - Release Candidate). Run the install with -pre option:
Install-Package npgsql -pre
This worked at least when installing to an iOS project. Not sure if it functions correctly as none of the dependencies are added to the References, only Npgsql.
I believe your speculation is correct of Xamarin tooling not being fully netstandard/PCL5 aware AFAIK(Your speculation is as good as mine). This is a huge effort by all parties involved to be unified. There have also been significant changes on the dotnet end that can alter this.
For your questions about System.Data:
System.Data is missing a bit of functionality: https://developer.xamarin.com/guides/ios/advanced_topics/system.data/#Missing_Functionality
System.Data is available via the Xamarin.iOS.dll assembly as it's not supported in PCLs.
https://developer.xamarin.com/guides/ios/under_the_hood/assemblies/
https://msdn.microsoft.com/en-us/library/system.data(v=vs.110).aspx (Notice no PCL items inside)
Finally, there should be a blog post next week about netstandard via https://blogs.msdn.microsoft.com/dotnet/2016/05/23/changes-to-project-json/
I would highly recommend that you join the .NET core slack channel to ask any questions you may have.
http://tattoocoder.com/aspnet-slack-sign-up/

The type or namespace name 'IPAddress' could not be found

I'm writing a Xamarin.Forms application and have added a PCL project into my main solution in Visual Studio 2013. I'm having problems with using several standard .net classes. Here's a list:
System.Net.IPAddress
System.Net.IPEndPoint
System.Net.WebClient
System.Net.Sockets.UdpClient
System.Timers.Timer
System.Timers.ElapsedEventArgs
System.Net.NetworkInformation.UnicastIPAddressInformation
My question is: How do I figure out which of the above are compatable with a Xamarin.Forms app, and how do I get them to work in my project/solution?
I've contacted Xamarin support, but get vague help and not specifics...
Does anybody have a good strategy for figuring out what classes are supported and what .dlls are needed in order to use them with a Xamarin.Forms project?
Thanks a bunch for your help.
PS: I've been given the following sites for reference, but they don't seem to have any concise information:
What is supported
NuGet package info
It sounds like you don't understand what Portable Class Libraries actually are. When you create a PCL, the subset of the .Net Framework classes that are available for you to use depends on what profile you choose. A profile are the platforms your PCL can support. In Xamarin, the default profile is Profile 78. Profile 78 supports .NET 4.5, WP8, Win8, WP Silverlight 8, Win Store 8, Xamarin.Android, Xamarin.iOS. So the classes that are available to you are the classes that are platform independent, that run on ALL platforms in profile 78.
Example: Take a look at the implementation of System.Net.IPAddress. The implementation makes use of Sockets. Upon further inspection of this implementation, you can see that that System.Net.Sockets is nothing more than a .NET wrapper around the WinSock API. The WinSock API is a Windows only DLL that is specific to the Windows operating family (ws2_32.dll). So, you can now see that System.Net.IPAddress are high level abstractions of Windows Socks (built for the Windows Platform). Since the ws2_32.dll does not exist in Android, nor iOS, etc, this API won't be available to you because Profile 78 specifies that we need to support other non-windows platforms.
Another example is System.Drawing.Bitmap. That's just a .NET wrapper around GDI+, which is based on the Win32 API. Of course Win32 is not available on non-windows platforms (even Windows Phone), which is why you won't see it in a PCL.
Now with that being said, you may think that PCLs are restrictive, but you can create a good PCL library if you think about them the right way. You can also use patterns like Dependency Injection and Bait and Switch.
So in short, don't count on Microsoft "adding support for all the base class libraries" because that's not what PCLs are.
You can still use System.Net.IpAddress in your Xamarin.Forms PCL by using DependancyService. Make an interface in PCL and declare a method "GetIpAddress" in it. Then , in android /iOS project, you can implement that interface in platform specific project and easily access System.Net.IpAddress. After that, Call DependancyService from PCL project to get the result which is IPAddress of your desire.
Cheers !!!
After some additional e-mail from Xamarin Support, and comments on my additional posting, it appears that MS is quite behind in supporting all of their classes via PCL. Some are supported, but not all by a long shot.
Anyway, in order to use my existing code, I would need to put the source .cs files that I would otherwise be using to create my new PCL project directly in the .iOS project and in the .droid project and recompile that way.
This basically defeats the advantage of using a PCL, but does provide an alternative that allows me to use most of my code unchanged. It would just be nicer if all the xamarin libraries were available as PCLs.. Oh well, eventually MS will catch up and get all of their PCLs up to date so that we can use our custom PCLs more often, instead of having to duplicate code all over the place. (Maintenance Nightmare)...
Anyway, thanks to all for the help.
(Working on the bleeding edge of technology is sometimes painful... But that's why we make the big bucks !!!)

Use MvvmCross with dot42

I'm currently trying some techniques for writing Android programs with C#. I found MvvmCross that is really nice for an old WPF programmer. Sadly, the free Xamarin version does not allow the use of MvvmCross, and licences are far too expensive for playing around.
Now i stumbles across dot42, but could not manage to load MvvmCross into a dot42 project via nuget. I get
Could not install package 'MvvmCross.PortableSupport 3.1.1'. You are trying to install this package into a project that targets 'Dot42,Version=v2.3.3', but the package does not contain any assembly references or content files that are compatible with that framework.
so I guess, Dot42 isn't supported by MvvmCross. Is it possible to use MvvmCross with dot42?
You won't currently be able to easily use nuget with dot42 - nuget hasn't yet added any extension for them.
Also, you won't be able to use MonoDroid-targetted assemblies for dot42 - those assemblies are built against Xamarin's runtime - not dot42.
However... if anyone wanted to get MvvmCross working against dot42, then I believe this should be possible - as dot42 supports quiet a good .Net functionality set, including PCLs. To do so would require editing the source code, cloning the MonoDroid projects and then changing the code that targets Xamarin/MonoDroid runtime objects to instead target the dot42 equivalents. I'd personally love to see this done, but haven't had time to work on it myself.

Categories