Using HttpClient in a Xamarin core project - xamarin

I am doing a project for multiple platforms where I have followed best practices and created a core project to contain all the code that is the same on all platforms.
To do the network communication I would like to use HttpClient which seems to be the recommended approach for communicating with web services going forward.
I am NOT doing a PCL but just a standard C# library to contain the core project. However it seems that there is no common implementation of HttpClient but only platform specific ones. I cannot reference and use it in my core but only in my Android and iOS projects.
I would very much prefer to have all networking code in my core project and as I see it there are two options - neither of which I like.
Make a common interface that I can use in the core project that is implemented as a wrapper on both Android and iOS.
Use another technology that is supported in the core project - I am thinking webclient or the like.
What is the recommended approach and why hasn't Xamarin made HttpClient available as a common library?
Hope someone can help,
Thanks

I recommend checking out ModernHttpClient https://github.com/paulcbetts/modernhttpclient
It is a cross-platform networking library for iOS and Android. From the project:
This library brings the latest platform-specific networking libraries
to Xamarin applications via a custom HttpClient handler. Write your
app using System.Net.Http, but drop this library in and it will go
drastically faster.

You should be able to use System.Net.Http in your core library just fine, I've done it recently.
Make sure your core library is a .Net 4.5 project. There is a setting in project options for selecting the .Net version. After checking this, all you should have to do is reference System.Net.Http.dll.
If that doesn't solve your issue, are you on Windows or Mac(Xamarin Studio)?

I'm afraid you can't use HttpClient on Mono at the moment due to licensing restrictions. There is a UserVoice suggestion that you can vote on if you like.

Related

YO .net core 2.x demo project with react/redux?

This is a "Veracity Platform" question - which I guess has a fairly small audience.
I'm using the veracity platform's application generator (based on Yeoman) to generate a sample application for .net core 2.x, using node.js.
yo #veracity/veracity:netcore-webapp
This creates a good basic .net core 2.x MVC project. Is there a way to further specify that I want to additionally include the "React/Redux" project template, like I would when creating a project in Visual Studio? That way the client app and the front-end webpack stack get started for me, which is nice.
Thanks in advance for any suggestions.
We do not have these configurations included in the generator yet. Uservoice will be added to Developer.veracity.com soon, but for now you may forward such feature requests also in the github project of the generator. Open an issue, and I will see when we can get it in to the backlog.
https://github.com/veracity/generator-veracity
Brede Børhaug - Manager Veracity for Developers

Cannot add Judopay to Xamarin forms project

I am trying to include Judopay in my project, and I was able to add it to Android and iOS, but not to the Xamarin forms project. There is no NuGet to be included and I can't seem to add the component reference. Any idea on how can I add it? I cannot use any of these:
using JudoDotNetXamarin;
using JudoPayDotNet.Enums;
using JudoPayDotNet.Models;
Again, they only work on iOS or Android, but not Xam forms.
That is because they only provide platform implementations. You will need to tell your Forms app which implementation to use at what time.
The correct way to go about this is with some form of IoC, using dependency injection or service location. Luckily Xamarin Forms has this integrated:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/
Using the references from the Android or iOS projects in your shared code will likely not work completely or break things. Maybe not immediately but surely later on. (Except when there really is no platform-specific functionality, then you might get away with this.)

Import ReactNative libraries into a NativeScript application

I have some Android development experience along with React.js. I'm working on a new mobile project and I haven't been sold entirely on either framework yet.
I'm thinking about using the NativeScript framework for its maturity and because it has more libraries with the functionality that I need but I'd like to design my views in a manner similar to ReactJS. I know I could write my own modules for ReactNative but that would require then I'd have to write modules for both Android and iOS. I'd like to avoid that if I could.
I was thinking about importing the necessary ReactNative libraries into a NativeScript project and then being able to write views in a way similar to ReactNative but still using NativeScript.
Is this doable?
Not sure about your exact question. I did just see an integration between Preact and NativeScript you may be interested in. Take a look here: https://github.com/staydecent/nativescript-preact
I believe that repo is in the early stages, but if enough people want something similar, it'll get worked on faster.
Using React Native in NativeScript is not the right approach, either of it are not built to work together and wouldn't work due to architectural reasons. Here is an NativeScript official comparison of these two.
Both are built for solving same problem (building native apps using JS, without webview hybrid app approach) by trans-compiling XML based view layer(which both these framework has its own syntax and so cant be shared) to build Native UI.
Said that view layer has its own syntax for both and cant be shared, model and controller layer cant be shared as well, due to the fact that NativeScript has few things packed as part of the application package, like JavaScript runtime, c++ and Java API layer which helps in exposing all the Native API without the need for any explicit native API wrapper we have in ReactNative.
React Native have platform specific UI components as well as hardware capabilities, which is not the case with NativeScript. Offcouse, that comes with the cost of a fatter app package size(50+MB) compared to <10MB for ReactNative. More on ReactNative vs NativeScript comparison here.

Download a zip file using xamarin forms app

I Am Newbie in Xamarin. Our Xamarin forms app supports Android and IOS. Most of the app functionality is written in PCL class library which is referred in the above platforms.
Now we need to download a zip file using this app. Some posts are suggesting to handle it in common PCL library and some are suggesting to handle it in respective native projects. I am bit confused, which is the better approach of the two. Are there any limitations in this approaches?
If possible could you please share a sample app. Thanks in advance.
You can write the code to download the file in PCL. For downloading file you can make use of HTTPClient. At the same time you have to call dependency service and write the dependent code in corresponding android/ios projects to specify the file output path.
https://developer.xamarin.com/recipes/ios/network/web_requests/download_a_file/ uses webClient which is written in android. You can use HTTPClient to get same result in PCL

Xamarin Android SQLite.Net NuGet package

I'm having a go at creating my first Android app using Xamarin. I'll be needing a database, and the Xamarin docs recommend the "SQLite-net-pcl" NuGet package (here). When I go to install this package it lists approx 50 dependencies that it wants to install! How much is this going to increase the size of my app by?
Is this normal with Xamarin development? Coming from a WPF background I admit I haven't got my head around all this .Net core/standard stuff yet.
And why do the docs recommend this package over the built-in 'Android.Database.Sqlite' namespace? Does the ease of use (of the SQLite.net package) outweigh the increase in app size?
How much is this going to increase the size of my app by?
Not much, Xamarin links out (removes) the unused code when you build your app in release mode. For example if you're not using any methods from System.IO, the assembly will not be included in the final build
Does the ease of use (of the SQLite.net package) outweigh the increase in app size?
Part of it is ease of use and the real advantage is you can reuse the code on iOS or any other platforms. Using Android.Database.Sqlite doesn't help you much if you're planning porting your app to other platforms
What determines which of those 50+ assemblies get pulled into my app?
Dependency on the netstanderd is what pulling those 50+ assemblies, otherwise Sqlite-net-pcl just need SQLitePCLRaw.* assemblies
There is nice series explaining netstanderd from one of the .Net engineers on youtube here - https://www.youtube.com/playlist?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY Hope it will answer your questions regarading netstanderd

Resources