JSON Serialization and Deserialization using System.Text.Json with Refit

13/11/2019 Leave a comment

Refit is a very useful library that allows to define an interface for a REST API and call it hiding all the HTTP and JSON serialization/deserialization bits. A lot of samples of its usage are available on GitHub.

By default, Refit uses JSON.NET under-the-hood to handle serializing and deserializing JSON. However, we can change this behavior and use the new System.Text.Json objects by defining a custom Content Serializer, i.e. a class that implements the IContentSerializer interface:

public interface IContentSerializer
{
    Task<T> DeserializeAsync<T>(HttpContent content);
    
    Task<HttpContent> SerializeAsync<T>(T item);
}

So, we can provide an implementation like the following:

public class JsonContentSerializer : IContentSerializer
{
    private readonly JsonSerializerOptions serializerOptions;

    public JsonContentSerializer(JsonSerializerOptions serializerOptions = null)
    {
        this.serializerOptions = serializerOptions ?? new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            WriteIndented = true,
        };
    }

    public async Task<T> DeserializeAsync<T>(HttpContent content)
    {
        using var utf8Json = await content.ReadAsStreamAsync()
            .ConfigureAwait(false);

        return await JsonSerializer.DeserializeAsync<T>(utf8Json,
            serializerOptions).ConfigureAwait(false);
    }

    public Task<HttpContent> SerializeAsync<T>(T item)
    {
        var json = JsonSerializer.Serialize(item, serializerOptions);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        return Task.FromResult((HttpContent)content);
    }
}

At lines 20-21 and 26, we’re using the JsonSerializer class from System.Text.Json to perform serialization and deserialization.

Finally, we need to tell Refit to use this implementation instead of the default one:

var settings = new RefitSettings
{
    ContentSerializer = new JsonContentSerializer()
};

var api = RestService.For<IMyServiceApi>("<service_url>", settings);

At lines 1-4, we create a RefitSettings object that specifies the Content Serializer to use. Then, we pass it to the static RestService.For method (line 6). Now all the JSON content will be handled with System.Text.Json.

Categories: .NET Core, C#

Update on using HostBuilder, Dependency Injection and Service Provider with .NET Core 3.0 WPF applications

07/11/2019 5 comments

Some months ago we talked about how to use .NET Core 3.0 Dependency Injection and Service Provider with WPF. Now that the final version of .NET Core 3.0 has reached its GA, it’s time to update that article showing the right approach to use HostBuilder, Dependency Injection and Service Provider in WPF applications.

So, after creating a .NET Core WPF application, we need to add the NuGet package Microsoft.Extensions.Hosting to the project. It will allow us to use HostBuilder and, moreover, it automatically imports a bunch of other required packages.

Now, Let’s open the App.xaml file and remove the StartupUri property of the Application class. Then, write the following code in the App.xaml.cs file:

public partial class App : Application
{
    private readonly IHost host;

    public App()
    {
        host = Host.CreateDefaultBuilder()
               .ConfigureServices((context, services) =>
               {
                   ConfigureServices(context.Configuration, services);
               })
               .Build();
    }

    private void ConfigureServices(IConfiguration configuration, 
        IServiceCollection services)
    {
        // ...
        services.AddSingleton<MainWindow>();
    }

    protected override async void OnStartup(StartupEventArgs e)
    {
        await host.StartAsync();

        var mainWindow = host.Services.GetRequiredService<MainWindow>();
        mainWindow.Show();

        base.OnStartup(e);
    }

    protected override async void OnExit(ExitEventArgs e)
    {
        using (host)
        {
            await host.StopAsync(TimeSpan.FromSeconds(5));
        }

        base.OnExit(e);
    }
}

At line 7, we create a HostBuilder using the Host.CreateDefaultBuilder method. As we can read in the documentation, it automatically configures some defaults (that are tipically useful for every application):

If we don’t want these configuration, but instead we prefer to manually configure every setting, we can simply initialize a new HostBuilder. In any case, it is possible to invoke other methods to further configure our host:

host = Host.CreateDefaultBuilder()  // Use default settings
       //new HostBuilder()          // Initialize an empty HostBuilder
        .ConfigureAppConfiguration((context, builder) =>
        {
            // Add other configuration files...
            builder.AddJsonFile("appsettings.local.json", optional: true);
        }).ConfigureServices((context, services) =>
        {
            ConfigureServices(context.Configuration, services);
        })
        .ConfigureLogging(logging => 
        {
            // Add other loggers...
        })
        .Build();

Going back to the first App.xaml.cs file, at line 8 we call HostBuilder.ConfigureService, that is responsible to add all our services in the .NET Core IoC Container. This method in turn calls our ConfigureServices implementation (lines 15-20), in which we register all the services used by the application in the exact same way of ASP. NET Core. We’ll complete this method in a moment, but for now let’s notice that we register also the MainWindow class (line 19). This is important because, in this way, the window itself becomes part of the Dependency Injection chain. It means that, after calling this method, at line 26-27 we can get it from the ServiceProvider and then show it. But, more important, it means that we can pass to the MainWindow constructor all the dependencies it needs, as we do for ASP.NET Core Controllers.

In the OnStartup override (lines 22-30), we call the StartAsync method to start the program (line 24): at this moment, it calls IHostedService.StartAsync on each implementation of IHostedService that it finds in the DI container (we don’t have any of this in our app, so in fact this method does nothing). Finally, as said before, at line 26-27 we get the MainWindow from the ServiceProvider and then show it.

As last step, in the OnExit method (lines 32-39), we call StopAsync to gracefully stop the host.

Even if the actual services aren’t yet registered, we can run the application and see that everything works as expected.

Now it’s time to complicate the things a bit. Let’s add a file named appsettings.json to the root folder of the project. Set its Build Action property to Content and Copy to Output Directory to Copy if newer:

{
  "AppSettings": {
    "StringSetting": "Value",
    "IntegerSetting": 42,
    "BooleanSetting": true
  }
}

This file is automatically loaded and made available to the application by the CreateDefaultBuilder method we talked before. Then, we create an AppSettings.cs file to hold configuration settings. This file will map the settings that we write in appsettings.json:

public class AppSettings
{
    public string StringSetting { get; set; }
 
    public int IntegerSetting { get; set; }
 
    public bool BooleanSetting { get; set; }
}

Moreover, create also a sample service with its interface:

public interface ISampleService
{
    string GetCurrentDate();
}
 
public class SampleService : ISampleService
{
    public string GetCurrentDate() => DateTime.Now.ToLongDateString();
}

Now we must register these services in the IoC Container, as usual:

private void ConfigureServices(IConfiguration configuration,
    IServiceCollection services)
{
    services.Configure<AppSettings>
            (configuration.GetSection(nameof(AppSettings)));

    services.AddScoped<ISampleService, SampleService>();

    //...
}

As said before, the MainWindow itself is in the IoC Container. So, when we get it from the Service Provider, it will automatically be injected with all the required services. So, we just need to modify its constructor:

public partial class MainWindow : Window
{
    private readonly ISampleService sampleService;
    private readonly AppSettings settings;
 
    public MainWindow(ISampleService sampleService, 
                      IOptions<AppSettings> settings)
    {
        InitializeComponent();
 
        this.sampleService = sampleService;
        this.settings = settings.Value;
    }
 
    // ...
}

Running this code, we’ll obtain a result like the following:

The .NET Core 3.0 WPF application with dependecies injected

The .NET Core 3.0 WPF application with dependecies injected

You can download the sample app using the link below:

Update on using HostBuilder, Dependency Injection and Service Provider with .NET Core 3.0 WPF applications

Categories: .NET Core, C#, WPF

A simple NavigationService with Dependency Injection for WPF on .NET Core 3.0

21/05/2019 1 comment

We already talked about how to use Dependency Injection and Service Provider with WPF on .NET Core 3.0. In that occasion we said that windows themselves are registered in the IoC, so that we need to use the ServiceProvider to get and load them, for example:

var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();

This approach works correctly, but we need to use two instructions every time we want to open a new window. Moreover, we don’t have a straight way to pass parameters to the new window. To make things simpler, we can write a couple of classes to manage this task:

public interface IActivable
{
    Task ActivateAsync(object parameter);
}

public class SimpleNavigationService
{
    private readonly IServiceProvider serviceProvider;

    public SimpleNavigationService(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public async Task ShowAsync<T>(object parameter = null) where T : Window
    {
        var window = serviceProvider.GetRequiredService<T>();
        if (window is IActivable activableWindow)
        {
            await activableWindow.ActivateAsync(parameter);
        }

        window.Show();
    }

    public async Task<bool?> ShowDialogAsync<T>(object parameter = null) 
        where T : Window
    {
        var window = serviceProvider.GetRequiredService<T>();
        if (window is IActivable activableWindow)
        {
            await activableWindow.ActivateAsync(parameter);
        }

        return window.ShowDialog();
    }
}

First of all, at lines 1-4 we define an IActivable interface to mark Windows that support “activation”, i.e. the ability to receive parameters upon loading. Then, the SimpleNavigationService class encapsulates all the logic we need:

  • it provides methods to open a window, getting it from the ServiceProvider (lines 15-36), so every dependency is automatically resolved and passed to its constructor;
  • if the window implements the IActivable interface, it calls the ActivateAsync method before actually showing it.

Now we just need to properly register the SimpleNavigationService and all the windows of our application in the ConfigureServices methods we defined in the previous post:

private void ConfigureServices(IServiceCollection services)
{
    // ...

    // Add SimpleNavigationService for the application.
    services.AddScoped<SimpleNavigationService>();

    // Register all the windows of the applications.
    services.AddTransient<MainWindow>();
    services.AddTransient<DetailWindow>();
}

At the end of the OnStartup method, we can use the new service to show the MainWindow:

protected override void OnStartup(StartupEventArgs e)
{
    // ....

    var navigationService = ServiceProvider
                            .GetRequiredService<SimpleNavigationService>();
    var task = navigationService.ShowAsync<MainWindow>();
}

Now let’s see how to use it. Let’s suppose to have a window with a TextBox and a Button:

<Window
    ...>

    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Parameter:" />
            <TextBox x:Name="ParameterTextBox" Width="300" />
            <Button
                x:Name="OpenDetailWindowButton"
                Click="OpenDetailWindowButton_Click"
                Content="Go to Detail Window" />
        </StackPanel>
    </Grid>
</Window>

We want that, pressing the Button at lines 8-11, the DetailWindow will be opened, receiving the value of the TextBox. So, the code-behind in MainWindow.xaml.cs is the following:

public partial class MainWindow : Window
{
    private readonly SimpleNavigationService navigationService;

    public MainWindow(SimpleNavigationService navigationService)
    {
        // ...
        this.navigationService = navigationService;
    }

    private async void OpenDetailWindowButton_Click(object sender, 
                                                    RoutedEventArgs e)
    {
        var result = await navigationService
                           .ShowDialogAsync<DetailWindow>(ParameterTextBox.Text);
    }
}

We pass the SimpleNavigationService in the MainWindow constructor through Dependency Injection, then in the button event handler we call the ShowDialogAsync method specifying the type of the Window to open and the parameter to initialize it. To be able to handle this, the DetailWindow code-behind should be something like that:

public partial class DetailWindow : Window, IActivable
{
    public DetailWindow()
    {
        // ...
    }

    public Task ActivateAsync(object parameter)
    {
        ParameterTextBox.Text = parameter?.ToString();
        return Task.CompletedTask;
    }
}

As DetailWindow implements the IActivable interface (line 1), SimpleNavigationService will ensure that the ActivateAsync method is called before showing the UI. In this sample, we just set the parameter in a TextBox (line 10). Of course, also the DetailWindow class could receive dependencies in its constructor, if necessary.

You can download the sample app using the link below:

A simple NavigationService with Dependency Injection for WPF on .NET Core 3.0

Categories: .NET Core, C#, WPF

Using Entity Framework Core with WPF on .NET Core 3.0

19/03/2019 2 comments

We talked about how to configure a .NET Core 3.0 WPF application in the same way as we do with ASP.NET Core. We can keep using this approach also if we need to use Entity Framework Core. It is important to note that tipically we use an external service to perform data access (for example, a REST Web APIs backend), but in some cases (especially if we’re working with a legacy system) it might be necessary to access a database directly from within our application.

And the story is again the same: you use the same code of ASP.NET Core. Let’s see how to do that. As always, take this sample as reference. First of all, we need to add the NuGet package for Entity Framework Core:

Adding Entity Framework Core to .NET Core 3.0 WPF application

Adding Entity Framework Core to .NET Core 3.0 WPF application

In this example, we’re using Entity Framework with SQL Server, but of course we can use every database supported by this OR/M.

Now, let’s add a connection string in the appsettings.json file:

{
  "ConnectionStrings": {
    "SqlConnection": ""
  }
}

Then, we must create our data context, i.e., a class that inherits form DbContext and provides access to the database:

public class DataContext : DbContext
{
    // DbSet properties declarations...

    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
    }
}

The DataContext class contains a constructor that takes a DbContextOptions as argument. It is used to pass configuration settings to the Context via Dependency Injection. For the rest, we can add to the class the usual DbSets we are accustomed to and configure the model by overriding the OnConfiguring and OnModelCreating methods.

Finally, we need to register the DbContext in the ServiceProvider collection, like we did in the previous post:

private void ConfigureServices(IServiceCollection services)
{
     services.AddDbContext<DataContext>
        (options => options.UseSqlServer(
                    Configuration.GetConnectionString("SqlConnection")));

            
     services.AddTransient(typeof(MainWindow));
}

At lines 4-5 we setup the DataContext to use SQL Server with the connection string from the appsettings.json file we defined before.

Now we’re ready to use the DataContext in every Window we need it. For example:

private readonly DataContext dataContext;

public MainWindow(DataContext dataContext)
{
    InitializeComponent();

    this.dataContext = dataContext;
}

As we can see, also in this case the approach is the same we use with ASP.NET Core. Of course, we can choose to pass the DataContext to a business service, instead of accessing it directly: as long as the service is itself in the IoC container, everything works in the same way.

Categories: .NET Core, C#, Entity Framework, WPF

Using HttpClientFactory with WPF on .NET Core 3.0

13/03/2019 1 comment

In the last post, we talked about how to use Dependency Injection with WPF applications running on .NET 3.0. We saw that with .NET Core 3.0 we can use the same approach that we are accustomed to when working with ASP.NET Core.

An interesting feature that we typically use with ASP.NET Core is the HttpClientFactory: it is a factory, available since .NET Core 2.1, for creating HttpClient instances to be used in our applications. Also in this case, we can use the same pattern within a WPF application running on .NET Core 3.0.

Let’s start from the sample we created in the last post. First of all, we need to add the Microsoft.Extensions.Http NuGet package (as always, remember to select the Include prerelease check):

Adding Microsoft.Extensions.Http NuGet package to WPF application

Adding Microsoft.Extensions.Http NuGet package to WPF application

Then, we need to register the HttpClientFactory. Let’s do it in the ConfigureServices method of App.xaml.cs:

private void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddHttpClient();

    services.AddTransient(typeof(MainWindow));
}

There are multiple way to use the HttpClientFactory. At line 5 we can see the simplest approach: using the AddHttpClient extension method we register a default HttpClientFactory from which we can get the actual HttpClient when we need it.

So, for example in the constructor of MainWindow we pass a reference to it:

private readonly IHttpClientFactory httpClientFactory;

public MainWindow(IHttpClientFactory httpClientFactory)
{
    InitializeComponent();

    this.httpClientFactory = httpClientFactory;
}

In this way, when we need an HttpClient, we just need to retrieve it from the factory, as usual:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var client = httpClientFactory.CreateClient();
    var response = await client.GetStringAsync
                         ("https://marcominerva.wordpress.com/feed/");
    
    // ...
}

That’s it. That simple. If you want to read a great article that explains why we should always use HttpClientFactory, take a look to HttpClient Creation and Disposal Internals: Should I Dispose of HttpClient? by Steve Gordon.

Categories: .NET Core, C#, WPF

Using .NET Core 3.0 Dependency Injection and Service Provider with WPF

06/03/2019 9 comments

UPDATE: this article is based on a preview release of .NET Core 3.0. Please refer to Update on using HostBuilder, Dependency Injection and Service Provider with .NET Core 3.0 WPF applications for the latest one.

We all know that .NET Core provides built-in support for Dependency Injection. We typically use it in ASP.NET Core (starting form the ConfigureServices method in the Startup.cs file), but the feature isn’t limited to this framework. So, as .NET Core 3.0 supports also Windows Clients development, we can use it in our WPF and Windows Forms applications.

Let’s see how to do that, for example, in WPF using Visual Studio 2019. Suppose we want to create a service and we also have some application settings; we want to pass both of them to each window of our application via Dependency Injection.

First of all, we must add the required NuGet packages to the project. Right click on the Solution Explorer, select the Manage NuGet Packages command and add the following packages (be sure to select the Include prerelease check):

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Options.ConfigurationExtensions
  • Microsoft.Extensions.Configuration.Json
Adding Dependency Injection support to a .NET Core 3.0 WPF application

Adding Dependency Injection support to a .NET Core 3.0 WPF application

These packages are necessary to enable Dependency Injection support (the first one) and to store and retrieve application settings in the classic appsettings.json file (the other ones). They will automatically get all the required dependencies.

Then, let’s add a file named appsettings.json to the root folder of the project. Set its Build Action property to Content and Copy to Output Directory to Copy if newer:

{
  "AppSettings": {
    "StringSetting": "Value",
    "IntegerSetting": 42,
    "BooleanSetting": true
  }
}

All the prerequisites are met, so we can start writing our code. Let’s open the App.xaml file and remove the StartupUri property of the Application class. Then, we need to override the OnStartup method in App.xaml.cs:

public partial class App : Application
{
    public IServiceProvider ServiceProvider { get; private set; }

    public IConfiguration Configuration { get; private set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        var builder = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
         .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        Configuration = builder.Build();

        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        ServiceProvider = serviceCollection.BuildServiceProvider();

        var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
        mainWindow.Show();
    }

    private void ConfigureServices(IServiceCollection services)
    {
        // ...

        services.AddTransient(typeof(MainWindow));
    }
}

In this method we create the Service Provider and configure the IoC container in a similar way of ASP.NET Core. We only need a bit of initialization. First of all, at line 9-13 we create an IConfiguration object that allows to read settings from the appsettings.json file (line 11). Then, we create an instance of a ServiceCollection class that will hold our services. Finally we call the ConfigureServices method (as we have in ASP.NET Core).

Within the latter (lines 24-29), we register all the services used by the application in the exact same way of ASP. NET Core. We’ll complete this method in a moment, but for now let’s notice that we register also the MainWindow class (line 28). This is important because, in this way, the window itself becomes part of the Dependency Injection chain. It means that, after calling this method, at line 20-21 we can get it from the ServiceProvider and then show it. But, more important, it means that we can pass to the MainWindow constructor all the dependencies it needs, as we do for ASP.NET Core Controllers.

Even if the actual services aren’t yet registered, we can run the application and see that everything works as expected.

Now it’s time to complicate the things a bit. First of all, let’s create an AppSettings.cs file to hold configuration settings. This file will map the settings that we write in appsettings.json:

public class AppSettings
{
    public string StringSetting { get; set; }

    public int IntegerSetting { get; set; }

    public bool BooleanSetting { get; set; }
}

Then, create also a sample service with its interface:

public interface ISampleService
{
    string GetCurrentDate();
}

public class SampleService : ISampleService
{
    public string GetCurrentDate() => DateTime.Now.ToLongDateString();
}

Now we must register these services in the IoC Container, as usual:

private void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>
        (Configuration.GetSection(nameof(AppSettings)));
    
    services.AddScoped<ISampleService, SampleService>();

    // ...
}

As said before, the MainWindow itself is in the IoC Container. So, when we get it from the Service Provider, it will automatically be injected with all required services, if any. So, we just need to modify its constructor:

public partial class MainWindow : Window
{
    private readonly ISampleService sampleService;
    private readonly AppSettings settings;

    public MainWindow(ISampleService sampleService, 
                      IOptions<AppSettings> settings)
    {
        InitializeComponent();

        this.sampleService = sampleService;
        this.settings = settings.Value;
    }

    // ...
}

Running this code, we’ll obtain a result like the following:

The .NET Core 3.0 WPF application with dependencies injected

The .NET Core 3.0 WPF application with dependecies injected

You can download the sample app using the link below:

Using .NET Core 3.0 Dependency Injection and Service Provider with WPF

Categories: .NET Core, C#, WPF

Integrating Cognitive Service Speech Recognition in UWP apps

09/08/2018 1 comment

The Speech service, part of Cognitive Services, is powered by the same technologies used in other Microsoft products, including Cortana and Microsoft Office.

We just need to create a speech resource in Azure Portal to obtain the keys to use it in our apps. Note that, at the time of writing, the service is in Preview and is available only in East Asia, North Europe and West US.

The service is available either using the SDK or the REST API. Let’s see how to use the former in a UWP app.

First of all, we have to add the Microsoft.CognitiveServices.Speech NuGet package to the solution:

Microsoft.CognitiveServices.Speech NuGet package

Microsoft.CognitiveServices.Speech NuGet package

Then, we create a simple UI with a Button to start recognition and a TextBox to show the result:

<Grid Padding="50">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button
        x:Name="RecognitionButton"
        Grid.Row="0"
        Margin="0,0,0,20"
        Click="RecognitionButton_Click"
        Content="Start Recognition" />
    <TextBox
        x:Name="RecognitionTextBox"
        Grid.Row="1"
        HorizontalAlignment="Stretch"
        Header="Recognized text"
        IsReadOnly="True" />
</Grid>

As the app need to use the microphone, it’s important to add the corresponding capability by double clicking the Package.appxmanifest file and go to the Capabilities tab:

Addung Micophone Capability to app

Addung Micophone Capability to app

Then, we can finally write the code to perform recognition:

private async void RecognitionButton_Click(object sender, RoutedEventArgs e)
{
    const string SpeechSubscriptionKey = "";
    const string SpeechRegion = "";
    const string Culture = "it-IT";

    var isMicAvailable = await CheckEnableMicrophoneAsync();
    if (!isMicAvailable)
    {
        return;
    }

    RecognitionButton.Content = "Recognizing...";
    RecognitionButton.IsEnabled = false;
    RecognitionTextBox.Text = string.Empty;

    var cognitiveSpeechFactory = SpeechFactory.FromSubscription
        (SpeechSubscriptionKey, SpeechRegion);

    // Starts recognition. It returns when the first utterance has been 
    // recognized.
    using (var cognitiveRecognizer = cognitiveSpeechFactory.
        CreateSpeechRecognizer(Culture, OutputFormat.Simple))
    {
        var result = await cognitiveRecognizer.RecognizeAsync();

        // Checks result.
        if (result.RecognitionStatus == RecognitionStatus.Recognized)
        {
            RecognitionTextBox.Text = result.Text;
        }
        else
        {
            await new MessageDialog(result.RecognitionFailureReason,
                result.RecognitionStatus.ToString()).ShowAsync();
        }
    }

    RecognitionButton.Content = "Start recognition";
    RecognitionButton.IsEnabled = true;
}

private async Task<bool> CheckEnableMicrophoneAsync()
{
    var isMicAvailable = false;

    try
    {
        var mediaCapture = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Audio
        };

        await mediaCapture.InitializeAsync(settings);
        isMicAvailable = true;
    }
    catch
    {
        await Windows.System.Launcher.LaunchUriAsync
            (new Uri("ms-settings:privacy-microphone"));
    }

    return isMicAvailable;
}

The lines 3-5 must be completed with the Speech subscription key that we can find on Azure portal, the name of the region in which the service has been created (eastasia, northeurope or westus at this time) and the language of the speech. At the moment, the service supports Arabic, Italian, German, Japanes, English, Portoguese, Spanish, Russian, France and Chinese.

At lines 7-11 we check whether the microphone is available. The CheckEnableMicrophoneAsync methods (43-65) tries to initialize a MediaCapture object for audio: in this way, if necessary, the app will prompt the user to consent the microphone usage.

After that, we can finally start the real recognition process. We instantiate a SpeechFactory at lines 17-18 and use it for creating the Cognitive Speech recognizer at lines 22-23. The RecognizeAsync method (line 25) actually starts speech recognition, and stops after the first utterance is recognized.

If the RecognitionStatus property is equal to Recognized (line 28), it means that the recognition succeeded, so we can read the Text property to access the recognized text.

You can download the sample app using the link below:

Integrating Cognitive Service Speech in UWP apps

As said before, RecognizeAsync returns when the first utterance has been recognized, so it is suitable only for single shot recognition like command or query. For long-running recognition, we can use the StartContinuousRecognitionAsync method instead.