Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASP.NET Core - MVC Sample #2387

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ samples/js/**/microsoft.cognitiveservices.speech.sdk.bundle*.js*

# iOS framwork
samples/objective-c/ios/MicrosoftCognitiveServicesSpeech.framework/

# ASP.NET Core
/samples/csharp/dotnetcore/mvc/samples/appsettings.Development.json
/samples/csharp/dotnetcore/mvc/samples/wwwroot/lib
73 changes: 73 additions & 0 deletions samples/csharp/dotnetcore/mvc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# C# ASP.NET Core MVC

This sample demonstrates various forms of speech recognition using the Speech SDK for C#.
It runs under .NET 8 or later on Windows or Linux (see the list of [supported Linux distributions and target architectures](https://docs.microsoft.com/azure/cognitive-services/speech-service/speech-sdk?tabs=linux)).

## Prerequisites

* A subscription key for the Speech service. See [Try the speech service for free](https://docs.microsoft.com/azure/cognitive-services/speech-service/get-started).
* Either one of the following:
* On Windows:
* [Microsoft Visual Studio 2019](https://www.visualstudio.com/), Community Edition or higher.
* On Windows or Linux:
* [.NET 8](https://dotnet.microsoft.com/download/dotnet/8.0)
* On Ubuntu or Debian, run the following commands for the installation of required packages:

```sh
sudo apt-get update
sudo apt-get install libssl-dev libasound2
```

* On **Ubuntu 22.04 LTS** it is also required to download and install the latest **libssl1.1** package e.g. from http://security.ubuntu.com/ubuntu/pool/main/o/openssl/.

* On RHEL or CentOS, run the following commands for the installation of required packages:

```sh
sudo yum update
sudo yum install alsa-lib dotnet-sdk-3.1 openssl
```

* See also [how to configure RHEL/CentOS 7 for Speech SDK](https://docs.microsoft.com/azure/cognitive-services/speech-service/how-to-configure-rhel-centos-7).

## Build the sample

* **By building this sample you will download the Microsoft Cognitive Services Speech SDK. By downloading you acknowledge its license, see [Speech SDK license agreement](https://aka.ms/csspeech/license).**
* [Download the sample code to your development PC.](/README.md#get-the-samples)

* If you are using Microsoft Visual Studio 2019 on Windows:
* Start Microsoft Visual Studio 2019 and select **File** \> **Open** \> **Project/Solution**.
* Navigate to the folder containing this sample, and select the solution file contained within it.
* To tailor the sample to your configuration, use search and replace across the whole solution (for example, in Visual Studio, via **Edit** \> **Find and Replace** \> **Quick Replace**) to update the following strings:

* `YourSubscriptionKey`: replace with your subscription key.
* `YourServiceRegion`: replace with the [region](https://aka.ms/csspeech/region) your subscription is associated with.

```bash
dotnet build samples/samples.csproj
```

## Run the sample

### Using Visual Studio 2019

To debug the app and then run it, press <kbd>F5</kbd> or use **Debug** \> **Start Debugging**. To run the app without debugging, press <kbd>Ctrl+F5</kbd> or use **Debug** \> **Start Without Debugging**.

The app displays a menu that you can navigate. Choose the scenario that you're interested in.

### Using the .NET Core CLI

Run the following command below from the directory that contains this sample.
(We assume you performed a Debug build earlier)

```bash
cd samples/bin/Debug/net8.0
dotnet samples.dll
```

The app displays a menu that you can navigate using your keyboard.
Choose the scenarios that you're interested in.

## References

* [.NET Framework on Windows version of this sample](../../dotnet-windows/console)
* [Speech SDK API reference for C#](https://aka.ms/csspeech/csharpref)
29 changes: 29 additions & 0 deletions samples/csharp/dotnetcore/mvc/samples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "samples", "samples\samples.csproj", "{322B2C73-C3D6-4B5A-A3F7-821CBEE6CD01}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{43E6BFC5-AA57-42E6-B903-1AABF2E48A71}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{322B2C73-C3D6-4B5A-A3F7-821CBEE6CD01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{322B2C73-C3D6-4B5A-A3F7-821CBEE6CD01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{322B2C73-C3D6-4B5A-A3F7-821CBEE6CD01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{322B2C73-C3D6-4B5A-A3F7-821CBEE6CD01}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94E353B7-11A9-414B-9CF0-4CDDC3693944}
EndGlobalSection
EndGlobal
45 changes: 45 additions & 0 deletions samples/csharp/dotnetcore/mvc/samples/ConfigurationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.CognitiveServices.Speech;

namespace samples;

public static class ConfigurationExtension
{
public static SpeechConfig GetSpeechConfig(this IConfiguration configuration)
{
var section = configuration.GetSection("SpeechService");
var key = section.GetValue<string>("Key");
var region = section.GetValue<string>("Region");

if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(region))
{
return null;
}

var config = SpeechConfig.FromSubscription(key, region);

var enableLogging = configuration.GetValue<bool>("EnableSDKLogging");

if (enableLogging)
{
var contentRoot = configuration.GetValue<string>(WebHostDefaults.ContentRootKey);

var directory = Path.Combine(contentRoot, "logs");
var fileFullname = Path.Combine(directory, $"log-{DateTime.Now:yyyy-MM-dd}.log");

if (!Path.Exists(fileFullname))
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

using var logFile = new FileStream(fileFullname, FileMode.Create);
}

config.EnableAudioLogging();
config.SetProperty(PropertyId.Speech_LogFilename, fileFullname);
}

return config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;

namespace samples.Controllers;

public sealed class SpeechController : Controller
{
public IActionResult SpeechToText()
{
return View();
}

public IActionResult TextToSpeech()
{
return View();
}
}
Loading
Loading