3

I'm new to Xamarin, I'm using VS 2017 Enterprise with the latest Xamarin updates.

I want to add an API so that the Db can communicate with both my mobile app and MVC project.

I created a cross-platform blank, .NET Standard project.

I added a new folder to the solution and within that folder a class to write my RestAPI code.

When writing the code I used HttpClient but it gives me an error asking if I'm

missing an assembly or reference.

How can I write code for my REstApi if I can't use HttpClient?

OR is there a better way to allow my Db to communicate with both my MVC project and mobile app?

I will publish both my MVC project and mobile app on Azure. Thanks

1
  • I would suggest you don't directly link the mobile app with the database but rather create a REST Web-API and then use that for DB connection, And yes you can use HTTPClient in xamarin Commented Sep 25, 2018 at 12:45

1 Answer 1

0

Firstly

For the error that appears: missing an assembly or reference.

The HttpClient lives in the "System.Net.Http" namespace.

You'll need to add: using System.Net.Http;

as mentioned here


secondly

is there a better way to allow your Db to communicate with both your MVC project and mobile app?

yes there is better way,after publish your MVC Project,

you can use Azure Mobile Client.

  • step 1

Open "Package Manager Console" and type

Install-Package Microsoft.Azure.Mobile.Client -Version 4.0.2

or you can get the last version from Azure Mobile Client SDK

This library provides features for creating Windows and Xamarin mobile apps that connect to Azure Mobile Apps

  • step 2

assume you have a class called "user", and you want to read, insert, update and delete data

take a look at the following code example:

using Microsoft.WindowsAzure.MobileServices;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

 public class User {/*....*/ }


  public class AzureServices
  {
      private static readonly string url = "http://xxxxx.azurewebsites.net";

      /*
       * The Azure Mobile Client SDK provides the MobileServiceClient class, 
       * which is used by a Xamarin.Forms application to access the Azure Mobile Apps instance
      */

      public MobileServiceClient Client;
      public IMobileServiceTable<User> UserTable;

      public AzureServices()
      {
          /* 
           * When the MobileServiceClient instance is created,
           * an application URL must be specified to identify the Azure Mobile Apps instance.
          */

          Client = new MobileServiceClient(url);

          //calling the GetTable method on the MobileServiceClient instance, which returns a IMobileServiceTable<User> reference.
          UserTable = Client.GetTable<User>();
      }

      // Querying Data
      public async Task<ObservableCollection<User>> GetAllUsers(bool sync = false)
      {
          var userList = await UserTable.ToEnumerableAsync();
          return new ObservableCollection<User>(userList);
      }
      //Inserting Data
      public async Task AddUser(User item)
      {
          await UserTable.InsertAsync(item);
      }
      // Updating Data
      public async Task UpdateUser(User item)
      {
          await UserTable.UpdateAsync(item);
      }
      // Deleting Data
      public async Task DeleteUser(User item)
      {
          await UserTable.DeleteAsync(item);
      }

  }

For more information visit Azure Mobile App

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.