Saturday, September 28, 2019

Using AsyncStreams to call a web service

.Net Core 3.0 includes Asynchronous streams.   


Basically IAsyncEnumerable<T> is an asynchronous version of IEnumerable<T>.   To use it in a you await a foreach loop to consume the elements

await foreach (var location in GetISSLocationSequence())
{
    Console.WriteLine(location);
}




GetISSLocationSequence is an async function which yield 20 IIS locations.  
It waits 1 second between getting locations


public static async System.Collections.Generic.IAsyncEnumerable<stringGetISSLocationSequence()
{
    using HttpClient http = new HttpClient();
    for (int i = 0; i < 20; i++)
    {
        var json = await http.GetStringAsync("http://api.open-notify.org/iss-now.json");
        await Task.Delay(2000);
        yield return json;
    }
}

One other thing I would like to point out was the format for the using 
statement.  Notice in this function I am using a using statement to make 
sure the HtppClient get disposed of when we done with it.  Notice we no 
long have to put the code is {}.  The HttpClient will dispose at the end 
of the function

To demo this I put the code in a .net core console application.  To allow 
await in the sub main I changed it from a void to async Task


class Program
{
    static async Task Main(string[] args)
    {
  
        await foreach (var location in GetISSLocationSequence())
        {
            Console.WriteLine(location);
        }
    }

Sunday, May 19, 2019

Error with Cert when starting an Asp.Net core website on the Mac

On my Mac I tried to run a Asp.Net core web site and got an error when starting the web site about ssl cert was invalid.

Adding the certificate to the Trusted Root Certficates store failed with the following error: Failed with a critical error.

The sites were working a few days before so I tried this command line.

     

dotnet dev-certs https --trust


Got a response A valid HTTPS certificate is already present.

Ran app again and still got same error.


The way I fixed it on my Mac was to open the KeyChain Access app and deleted the localhost certificate




Ran 

dotnet dev-certs https --trust 




After this I was able to debug my asp.net core apps in vs for Mac 2019 and Rider