Forum Xamarin.Forms

Failed Local Development: Xamarin.Forms 3.1 accessing ASP.NET Core 2.1 API Windows 10 VS2017 v15.8.6

walterlockhartwalterlockhart Member
edited October 2018 in Xamarin.Forms

Xamarin Dev Days Hands On Lab

Please note: I can't use the abbreviation for Hyper Text Transfer Protocol Secure (HTPS) in this forum because I haven't been around long enough :-)

I have followed the Xamarin Dev Days Hands On Lab walkthrough htps://github.com/xamarin/dev-days-labs/tree/master/HandsOnLab by creating a Xamarin.Forms 3.1 cross platform application (.NET Standard 2.0) using Microsoft Visual Studio Enterprise 2017 v15.8.6 on Windows 10.

I use the author's HTPS endpoint:

var json = await client.GetStringAsync("htps://demo8598876.mockable.io/speakers");

And everything works fine across all 3 clients (iOS, Android, UWP).

Local API - non-HTPS

I created a ASP.NET Core 2.1 API without 'configure for HTPS' i.e., a non HTPS endpoint that GETs the list of speakers in the browser:

htp://localhost:60684/api/speakers

Within my Xamarin.Forms app I changed my GetSpeakers Task as follows:

                    // Get base URL.
                    string baseUrl = GetBaseUrl();

                    // Get JSON from remote server.
                    //var json = await client.GetStringAsync("htps://demo8598876.mockable.io/speakers");

                    var json = await client.GetStringAsync(baseUrl + "speakers");

where:

        string GetBaseUrl()
        {
            return Device.RuntimePlatform == Device.iOS
                    ? "htp://127.0.0.1:60684/api/"
                    : "htp://10.0.2.2:60684/api/";
        }

I get the following errors:

iOS (non-HTPS):

with the following in info.plist:

      <key>NSAppTransportSecurity</key>
      <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
      </dict>

error message:

    Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x600003f9b750 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A90ADB46-23DF-4573-B243-550D920C825D>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
        "LocalDataTask <A90ADB46-23DF-4573-B243-550D920C825D>.<1>"
    ), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=htp://127.0.0.1:60684/api/speakers, NSErrorFailingURLKey=htp://127.0.0.1:60684/api/speakers, _kCFStreamErrorDomainKey=1} [-1004]

and:

Android (non-HTPS):

400 (Bad Request)

Local API - HTPS

I created a ASP.NET Core 2.1 API with 'configure for HTPS' i.e., a HTPS endpoint (with a self-signed certificate) that GETs the list of speakers in the browser:

htps://localhost:44304/api/speakers

Within my Xamarin.Forms app I changed my GetSpeakers Task as follows:

                    // Get base URL.
                    string baseUrl = GetBaseUrl();

                    // Get JSON from remote server.
                    //var json = await client.GetStringAsync("htps://demo8598876.mockable.io/speakers");

                    var json = await client.GetStringAsync(baseUrl + "speakers");

where:

        string GetBaseUrl()
        {
            return Device.RuntimePlatform == Device.iOS
                    ? "htps://127.0.0.1:44304/api/"
                    : "htps://10.0.2.2:44304/api/";
        }

I get the following errors:

iOS (HTTPS):

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x600003eac030 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <CCD5527F-9569-4355-A8D6-3F653EBE02F2>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <CCD5527F-9569-4355-A8D6-3F653EBE02F2>.<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=htps://127.0.0.1:44304/api/speakers, NSErrorFailingURLKey=htps://127.0.0.1:44304/api/speakers, _kCFStreamErrorDomainKey=1} [-1004]

and:

Android (HTPS):

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Basically, I'm stuck. I don't know what to do to fix this issue. I must be making a real simple mistake. Can someone help, please?

Regards

Walter

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @walterlockhart

    Could you visit the https://10.0.2.2:44304/api/ in your device browser?
    I think you have the wrong url. Please check your web API address.

  • @BillyLiu Thanks for your response.

    I am using the Android Emulator and iOS Simulator to test against the ASP.NET Web API end-point.

    I read in this article developer.android.com/studio/run/emulator-networking that the network address 10.0.2.2 is the special alias to the host loopback interface (i.e., 127.0.0.1 on your development machine) and also in my ASP.NET Web API the launchSettings.json file is telling me that the SSL Port is 44304:

    {
      "$schema": "htp://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "htp://localhost:55883",
          "sslPort": 44304
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "launchUrl": "api/speakers",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "SpeakersHTPS": {
          "commandName": "Project",
          "launchBrowser": true,
          "launchUrl": "api/speakers",
          "applicationUrl": "htps://localhost:5001;htp://localhost:5000",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    So, I'm a little confused by your answer. If the URL is incorrect then where can I find the correct URL?

Sign In or Register to comment.