Implementing a WebAPI

I am developing a Xamarin.Forms PCL application that needs to access an external DB (SQL Server). I have looked at a countless number of websites on how to implement this and I am coming up short. I have come to the conclusion that I need to implement a web service of some kind. How does this web service work? Do I have to get this hosted like a website? Where do I put my DB information? Once I have my web service up and running, how and where in my xamarin.forms code do I attempt to communicate? I have already read all of the material on the Xamarin website (Consuming Web Services etc.) but any other advice would be much appreciated.

Answers

  • PubuduGayanPubuduGayan NZMember ✭✭✭

    @AustinWolfe you are in right track. but make sure not to test your mobile app with localhost web service or APIs because it will not work. the reason is simulators and mobile phones are working like independent computer therefore it cannot read your local machine localhost. host your web api or Web service to AZURE or AWS or any other public server.

    regards,
    Pubudu

  • @PubuduGayan how do I actually connect all 3 code wise. How do I go about telling my mobile app where the API is and how do I go about telling my API where my database is? Any references would be great.

  • MichaelKayMichaelKay GBMember ✭✭
    edited September 2016

    @PubuduGayan not sure this is the best advise. You can test your application locally while developing. You need to have a local web server e.g. IIS. You then need to know the IP address of the machine where the IIS is being hosted. This can be the same machine or any machine on the network. Then in your code use the IP address rather the localhost. Your simulator should then be able to connect to this without any problem.
    On my local network a sample web api looks like this: http://10.2.0.45/test/prodwebapi/api/product/get/1

    @AustinWolfe - this is a thirty thousand bird view answer to your question:

    I'm assuming you are comfortable settuping up web api project. There are lots of tutorials on Google. You can implement and do your database calls on this level. Make sure you test your web api first using the method i described above.

    A sample class that talk to web api called MyService.cs looks like this:
    - Below classes will be in your shared project

    class MyService : IMyService
    {
    HttpClient client;
    
    internal MyService()
        {
            client = new HttpClient();
    
            client.MaxResponseContentBufferSize = 256000;
    
        }
    
    
    public async Task<IEnumerable<Product>> GetAllData()
        {
    
            IEnumerable<Product> listProd = null;
            var uri = new Uri(Constants.ServerAddress + "/api/product/get");
            try
            {
                HttpResponseMessage response = client.GetAsync(uri).Result;
                if (response.IsSuccessStatusCode)
                { 
                    var content = await response.Content.ReadAsStringAsync();
                    listProd = JsonConvert.DeserializeObject<IEnumerable<Product>>(content);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"ERROR {0}", ex.Message);
            }
    
            return listProd;
        }
    }
    

    Then i create (although not absolute necessary) an interface:

    public interface IMyService { Task<IEnumerable<Product>> GetAllData(); }

    Then you consume through the interface...

    Have you visit this link where more of what i mentioned above can been seen, and a detailed sample project can be downloaded from there:
    https://developer.xamarin.com/guides/xamarin-forms/web-services/consuming/rest/

    Hope this helps. Cheers

  • PubuduGayanPubuduGayan NZMember ✭✭✭

    @MichaelKay I missed some part on my answer that you covered moreover I am not saying I am 100% correct plus all the answers are depending on your requirement and your development environment. The reason I am advising to host to cloud it always hassle free plus it easy to manage rather than fighting with the local network policies.

    @AustinWolfe I am strongly advice not to connect SQL server via the PCL. Just use SQL lite brig whatever necessary data to the SQL lite (via the API or WEBSERVICE) process it on the app and sync it. You can use mobile service but I never used that.

Sign In or Register to comment.