6

I want to launch a web api host when launching the application on xamarin, which I will make requests for, but I don’t understand how to connect the web api project and xamarin.forms or is there some other way of debugging the application.

I know that you can deploy the application on the azure service, but I want it to work locally

SantaZ
  • 97
  • 1
  • 7
  • when you run your ASP Core project locally, it will get assigned a port number (I believe you can configure this in the app.json). Use the server IP and port number in your Forms app. Note that by default the VS dev server will not accept remote requests. – Jason Feb 29 '20 at 13:52
  • 1
    Sounds interesting. Have you tried copying the contents of your Program.cs and Startup.cs (of your Web API project) to your Xamarin project and execute it on application start? – ˈvɔlə Feb 29 '20 at 13:58
  • @Jason can you tell in more detail and, if possible, give an example? – SantaZ Feb 29 '20 at 14:28

3 Answers3

2

Those are two different applications.

Xamarin.Forms

Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows developers to build Android, iOS, and Windows applications from a single shared codebase.

WebApi Core

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the . NET Framework.

The first one is deployed on app stores and installed on devices.

The web api is deployed in web servers (like IIS) or on azure, and can be accessed over internet via http calls.

During development, you can build and deploy on your localhost. Check here for setup guidance: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio

You can consume your api from xamarin forms as described here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest

Example:

public async Task<List<TodoItem>> RefreshDataAsync ()
{
  ...
  var uri = new Uri (string.Format (Constants.TodoItemsUrl, string.Empty));
  ...
  var response = await _client.GetAsync (uri);
  if (response.IsSuccessStatusCode)
  {
      var content = await response.Content.ReadAsStringAsync ();
      Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
  }
  ...
}
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
1

This is pretty simple with Visual Studio 2019/2017.

After creating your Xamarin forms project, add an ASP.NET Core app to your solution; Visual Studio will manage everything. To debug the ASP project, set it as Startup project and click on the beautify Play/Start button and the browser should start and wait for a breakpoint or your stuff ...

You can debug Xamarin and the ASP Web app at the same time.

First we assume that the Web App act as an API endpoint for your app ; so that you will call it to send or request data ... Then we assume you have a web client to call your endpoint and controllers in your Web app to receive that requests and respond ...

OK all that given, do this:

  • Right click on the solution in the Solution Explorer
  • Pick Properties
  • At the left side of the properties page, select Startup Projects
  • Multiple startup projects radio box
  • Action column set Start on every project you want to debug
  • Then ok and play

Hope this helped.

Useful links

Fabrice T
  • 648
  • 9
  • 23
  • Isn't it a really bad idea to add a web app directly in your Xamarin Forms solution? That solution gets deployed on app stores. The web app would make the app much larger than it needs to be and wouldn't be able to run from the app store. You'd have to host the web app from somewhere else separately like AWS for the production app to actually be able to make any calls. I think it's best to make two separate projects. – Wes Thompson Oct 11 '20 at 08:40
  • 1
    You should read the above carefully and the question please. No one is inside no one but in conjunction – Fabrice T Oct 11 '20 at 18:59
  • You literally said, "After creating your Xamarin forms project, add an ASP.NET Core app to your solution." How is that interpreted any other way than adding a web app to the same solution? – Wes Thompson Oct 12 '20 at 03:46
  • A solution is a collection of projects sharing the same goal. XF is the front and ASP the back ; the both work together in the same solution but are not deployed in the same environment ... really you should read carefully or think twice .... – Fabrice T Oct 12 '20 at 08:48
  • @WesThompson the web app wouldn't get compiled into the mobile app. Having all your projects in one solution can be useful, it's helpful to organise files and projects, and also handy if you use source generators or share code like logic or DTOs between them. But only projects that you mark as dependencies for the mobile app get included, and even then the linker will remove bits that aren't referenced. – Matt G Mar 08 '21 at 21:27
0

You can run two instances of Visual Studio locally, one for the API project and the other for the Xamarin app.

In addition to Athanasios Kataras's answer, this documentation tells you how to connect to a local web service from the iOS Simulator or Android Emulator: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services