{"id":38918,"date":"2019-02-25T14:36:04","date_gmt":"2019-02-25T19:36:04","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=38918"},"modified":"2019-08-30T08:34:45","modified_gmt":"2019-08-30T15:34:45","slug":"debug-local-asp-net-core-web-apis-android-emulators","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/debug-local-asp-net-core-web-apis-android-emulators\/","title":{"rendered":"Quick Tip: Debugging Local ASP.NET Core Web APIs on Android Emulators"},"content":{"rendered":"<p>\t\t\t\tWhen developing mobile applications with a web API backend there is always a need to debug locally on your development machine. If you are using Visual Studio for Mac and debugging iOS applications you know it is as easy as running your web API locally and using <b>localhost<\/b> as the URL for web requests. However, this is not the case for Android debugging, because Android emulators have their own networking configuration whereas the iOS simulator uses the same network as the local machine. But we&#8217;ve got you, Android app developers, covered. With a little &#8220;know how&#8221; you can now also debug your Android apps locally regardless if you developing on Visual Studio on Windows or Visual Studio for Mac.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-38923\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/LocalDebugging.png\" alt=\"\" width=\"800\" height=\"422\" \/><\/p>\n<p>The first thing is to understand that Android has a special IP address to communicate and loop back to the host machine. The default Android emulators use <b>10.0.2.2<\/b> for this communication instead of <b>localhost<\/b>. Other emulators may use a different IP address, for instance, Genymotion uses <b>10.0.3.2<\/b>. With this knowledge, you can configure your backend API to integrate with your mobile app. And since it&#8217;s usually common to place your web API calling code in shared code, you can use <a href=\"https:\/\/docs.microsoft.com\/xamarin?WT.mc_id=xamarin-blog-jamont\">Xamarin.Essentials<\/a> to determine what device the app is running on in order to pick the correct IP address.<\/p>\n<h2>Setup Xamarin.Essentials<\/h2>\n<p>To get started with Xamarin.Essentials you need to install the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Essentials\/\">NuGet package<\/a>. Use Xamarin.Essentials for your .NET Standard library if you are using one to share code across your iOS, Android, and UWP app projects.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-38745\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Install.png\" alt=\"\" width=\"764\" height=\"232\" \/><\/p>\n<p>After installing the NuGet, there is a small amount of code on Android that is required to initialize Xamarin.Essentials.<\/p>\n<p>In the Android project&#8217;s `MainLauncher` or any `Activity` that is launched, Xamarin.Essentials must be initialized in the `OnCreate` method:<\/p>\n<pre class=\"lang:c# decode:true \">protected override void OnCreate(Bundle savedInstanceState) {\r\n  \/\/...\r\n  base.OnCreate(savedInstanceState);\r\n  Xamarin.Essentials.Platform.Init(this, savedInstanceState); \/\/ add this line to your code, it may also be called: bundle\r\n  \/\/...\r\n<\/pre>\n<p>To handle runtime permissions on Android, Xamarin.Essentials must receive any <i>OnRequestPermissionsResult<\/i>. Add the following code to all <i>Activity<\/i> classes:<\/p>\n<pre class=\"lang:c# decode:true \">public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)\r\n{\r\n  Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);\r\n\r\n  base.OnRequestPermissionsResult(requestCode, permissions, grantResults);\r\n}\r\n<\/pre>\n<h2>Using Android Emulator Settings<\/h2>\n<p>Now that Xamarin.Essentials installed you can use the <a href=\"https:\/\/docs.microsoft.com\/xamarin\/essentials\/device-information?WT.mc_id=xamarin-blog-jamont\">Device Information<\/a> to check what platform the app is currently running on. Here is how you can transform the Xamarin.Forms project templates with an ASP.NET Core Web API to debug on Android:<\/p>\n<pre class=\"lang:c# decode:true \">public partial class App: Application\r\n{\r\n    public static string IPAddress = DeviceInfo.Platform == DevicePlatform.Android ? \"10.0.2.2\" : \"localhost\";\r\n    public static string BackendUrl = $\"http:\/\/{IPAddress}:5000\";\r\n}\r\n<\/pre>\n<p>After that, you can start making web requests with the newly optimized URL:<\/p>\n<pre class=\"lang:c# decode:true \">HttpClient client;\r\n\r\npublic AzureDataStore()\r\n{\r\n    client = new HttpClient();\r\n    client.BaseAddress = new Uri($\"{App.BackendUrl}\/\");\r\n}\r\n\r\npublic ask&lt;IEnumerable&gt; GetItemsAsync(bool forceRefresh = false)\r\n{\r\n    var json = await client.GetStringAsync($\"api\/item\");\r\n    return Task.Run(() =&gt; JsonConvert.DeserializeObject&lt;IEnumerable&gt;(json));\r\n}\r\n<\/pre>\n<h2>Debugging on Windows<\/h2>\n<p>One important thing to remember on Windows is that you must use the default ASP.NET Core web server, i.e. Kestrel, to run the web API as IIS has additional isolation that will not work when communicating to this IP address.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/NotIIS.png\" alt=\"\" width=\"321\" height=\"204\" class=\"aligncenter size-full wp-image-38964\" \/><\/p>\n<p>You&#8217;ll also need to ensure that you don&#8217;t use the <strong>WebHost.CreateDefaultBuilder(args)<\/strong> option when bootstrapping your ASP.NET Core API as this will default to using IIS on windows. You can find out more about configuring Kestrel in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/servers\/kestrel?view=aspnetcore-2.2#how-to-use-kestrel-in-aspnet-core-apps?WT.mc_id=xamarin-blog-jamont\">ASP.NET Core documentation<\/a>. If you are debugging your application on Visual Studio for Mac it defaults to Kestrel so you have nothing to worry about. <\/p>\n<h2>Learn More<\/h2>\n<p>To learn more about Android networking in the emulator be sure to read through the <a href=\"https:\/\/developer.android.com\/studio\/run\/emulator-networking.html\">full documentation<\/a> from Google. Get started with ASP.NET Core to power your mobile applications and check out our awesome <a href=\"https:\/\/developer.xamarin.com\/guides\/cross-platform\/asp-net-core\/\">getting started guide on ASP.NET Core and Xamarin<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Debugging your ASP.NET Core Web API backend against you Android emulator should be simple, and with this quick tip you can use Xamarin.Essentials and Kestral to enable local debugging.<\/p>\n","protected":false},"author":544,"featured_media":40873,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2,367],"tags":[437,24],"class_list":["post-38918","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","category-xamarin-forms","tag-debugging","tag-xamarin-essentials"],"acf":[],"blog_post_summary":"<p>Debugging your ASP.NET Core Web API backend against you Android emulator should be simple, and with this quick tip you can use Xamarin.Essentials and Kestral to enable local debugging.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/38918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=38918"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/38918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/40873"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=38918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=38918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=38918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}