kevbite / CompaniesHouse.NET

A simple .NET client wrapper for CompaniesHouse API
MIT License
37 stars 44 forks source link

CompaniesHouse sample code not working - example please #65

Closed hughmullally closed 6 years ago

hughmullally commented 6 years ago

I'm having a problem running your sample code. When function SearchAllAsync is called (with the await keyboard), it does'nt return to the following statement in that function, but to the next statement in the calling function. No exception is thrown.

I'm sure I'm doing something really dumb (probably to do with the async code), but it would help to have a simple full working example that I could try running.

I'm using Visual Studio 2017, .net framework 4.5.2 and CompaniesHouse.net 3.2,1

kevbite commented 6 years ago

Have you got an example of what you are trying to run?

hughmullally commented 6 years ago

Thanks for replying so quickly. Here is the code:

using System.Diagnostics;
using CompaniesHouse;
using CompaniesHouse.Request;

namespace CompaniesHouseApi
{
    class Program
    {
        private static string ChKey = "TM4vsfrlRKop0jVbzZK0V4l1OmtluusevXMQLFpW";
        private static CompaniesHouseSettings Settings { get; set; }

        static void Main(string[] args)
        {
            ProcessRequest();
        }

        private static async void ProcessRequest()
        {
            Settings = new CompaniesHouseSettings(ChKey);

            using (var client = new CompaniesHouseClient(Settings))
            {
                var request = new SearchRequest()
                {
                    Query = "SpecSavers",
                    StartIndex = 10,
                    ItemsPerPage = 10
                };

                var result = await client.SearchAllAsync(request);
                foreach (var item in result.Data.Items)
                {
                    Debug.Print(item.Kind);
                }
            }
        }
    }
}
kevbite commented 6 years ago

You need to change your Main method to return a Task:

static async Task Main(string[] args)
{
    await ProcessRequest();
}

However you will need to be using C# 7.1, More info on this can be found here - https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/

hughmullally commented 6 years ago

Perfect. I was using VS2017, but for some reason the 4.7.1 runtime wasn't available. I downloaded that, made the code changes you suggested, and my initial test program works fine.

Thanks very much for your help and the quick responses.

kevbite commented 6 years ago

Glad you got it working! Any other problems just open another issue.