How can I capture audio in Blazor Server? - azure

My app:
https://vcat.azurewebsites.net/voice
uses azure voice to text service on a blazor server side. It works fine locally, but when published at azure it does not work.
What is wrong?
As I have understood it is not only me who has this problem. This is another one:
CognitiveServices.Speech Blazor Server not working when Published to Azure
here is the code:
private async Task record()
{
try
{
_isRecording = true;
_question = string.Empty;
_answer = string.Empty;
using var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
using var speechRecognizer = new SpeechRecognizer(_speechConfig, audioConfig);
var speechRecognitionResult = await speechRecognizer.RecognizeOnceAsync();
_question = speechRecognitionResult.Text;
_isRecording = false;
if (string.IsNullOrEmpty(_question))
{
_question = "Please try again!";
}
else
{
//_isThinking = true;
await answer();
//_isThinking = false;
await speak();
}
}
catch (Exception ex)
{
Error?.ProcessError(ex);
}
}

Related

Request body goes missing when application is deployed to Azure

I have a net6.0 Razor pages app that is running fine locally, but doesn't work when deployed to Azure. It seems that POST bodies are going missing.
Here's some middleware...
public async Task Invoke(HttpContext context)
{
_logger.LogInformation("BodyLogger");
if (!context.Request.Body.CanSeek)
{
context.Request.EnableBuffering();
}
if (context.Request.Method == "POST")
{
_logger.LogInformation("POST cl=" + context.Request.ContentLength.ToString());
try
{
_logger.LogInformation("BodyLogger try");
string body = "";
context.Request.Body.Position = 0;
using (var reader = new StreamReader(context.Request.Body, leaveOpen: true))
{
_logger.LogInformation("BodyLogger using");
body = await reader.ReadToEndAsync();
}
context.Request.Body.Position = 0;
_logger.LogInformation($"BODY ok (length {body?.Length ?? 0})" + (body ?? "(null)"));
}
catch (Exception e)
{
_logger.LogError("BODY error " + e.Message);
throw;
}
}
await _next.Invoke(context);
}
...and what appears in ApplicationInsights.
What's going on?!
Updage
Even more strangely: it still doesn't work if it's deployed to a different Azure Web App resource, and this middleware
app.Use(async (HttpContext context, RequestDelegate next) =>
{
if (context.Request.Path == "/post")
{
if (!context.Request.Body.CanSeek)
{
context.Request.EnableBuffering();
}
//context.Request.Body.Position = 0;
context.Request.Body.Seek(0, SeekOrigin.Begin);
string body = "";
using (var reader = new StreamReader(context.Request.Body, leaveOpen: true))
{
body = await reader.ReadToEndAsync();
}
//context.Request.Body.Position = 0;
context.Request.Body.Seek(0, SeekOrigin.Begin);
await context.Response.WriteAsJsonAsync("The body was: " + body);
return;
}
await next.Invoke(context);
});
works both as the first and the last middleware.
I've noticed that absent from the ApplicationInsights logs are the Kestrel messages
Connection id "0HMOAORGGN3ES", Request id "0HMOAORGGN3ES:00000003": started reading request body.
Connection id "0HMOAORGGN3ES", Request id "0HMOAORGGN3ES:00000003": done reading request body.
It turns out that in some cases my custom ITelemetryInitializer for ApplicationInsights (turned off in development) was reading the request body.

Receive messages from Azure IotHub in xamarin .netstandard

I am developing the xamarin app in .netStandard 2.0 using visual studio 2019.I have a problem to received data from iothub.I able to send data from xamarin app to iot hub but unable to received data from azure iothub.
Liberies
Microsoft.Azure.Device (1.20.0)
Microsoft.Azure.Device.Client (1.24.0)
private const string DeviceConnectionString = "HostName=[your hostname];DeviceId=[your DeviceId];SharedAccessKey=[your shared key]";
public async Task Start()
{
try
{
DeviceClient deviceClient =
DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.http1);
await SendEvent(deviceClient);
await ReceiveCommands(deviceClient);
}
catch (Exception ex)
{
Debug.WriteLine("Error in sample: {0}", ex.Message);
}
}
async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
dataBuffer = "Hello Iot";
Message eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
await deviceClient.SendEventAsync(eventMessage);
}
async Task ReceiveCommands(DeviceClient deviceClient)
{
Message receivedMessage;
string messageData;
while (true)
{
receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
txtblkMessages.Text = messageData + "\n" + txtblkMessages.Text;
await deviceClient.CompleteAsync(receivedMessage);
}
await Task.Delay(TimeSpan.FromSeconds(1));
}
}

How do I use the rabbitmq in my .net core web

As the title suggests,Examples on the official website are console project but is it really impossible to apply to asp.net core web?
My web project as a consumer or producer but does not output received information,But the rabbitmq page console displays the queue messages sent,so What are consumers or producer in the actual production environment?windows server?console?
this is my api code:
[HttpGet]
public ActionResult sendMgs()
{
string message = string.Empty;
//var uri = new Uri("amqp://192.168.150.129:5672");
var factory = new ConnectionFactory()
{
UserName = "admin",
Password = "admin",
Port=5672,
HostName= "192.168.150.129",
RequestedHeartbeat = 0,
VirtualHost= "/vhost_mmr"
//Endpoint = new AmqpTcpEndpoint(uri)
};
using (var connection=factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue:"hello",
durable:false,
exclusive:false,
autoDelete:false,
arguments:null);
message = "Hello World";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey:"hello",
basicProperties:null,
body:body);
}
}
return Json(new {message = message });
}
and this my mvc web code:
public IActionResult MqTest()
{
System.Diagnostics.Debug.Write("test begin:");
GetQueueMsg();
return View();
}
public void GetQueueMsg()
{
var factory = new ConnectionFactory()
{
UserName = "admin",
Password = "admin",
Port = 5672,
HostName = "192.168.150.129",
RequestedHeartbeat = 0,
VirtualHost = "/vhost_mmr"
};
using (var connection = factory.CreateConnection())
{
using (var channel =connection.CreateModel())
{
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var msg = Encoding.UTF8.GetString(body);
ViewBag.msg = msg;
System.Diagnostics.Debug.Write("test:" + msg);
};
var ret = channel.BasicConsume(queue: "hello",
autoAck: true,
consumer: consumer);
}
}
}

Dialogflow Webhook Response c# gives error on invoke

I'm trying to create a webhook for Dialogflow in C# (on Azure). Everytime I see the same example but my DialogFlows keeps geting an error with this response"
Here's what I did:
Created a new ASP.Net Web Project (WebAPI)
installed NuGet Google.Cloud.DialogFlow V2 (v1.0.0.beta02)
updated System.Net.Http to 4.3.3
Created a new controller
[System.Web.Http.HttpPost]
public dynamic DialogAction([FromBody] WebhookRequest dialogflowRequest)
{
var intentName = dialogflowRequest.QueryResult.Intent.DisplayName;
var actualQuestion = dialogflowRequest.QueryResult.QueryText;
var testAnswer = $"Dialogflow Request for intent {intentName} and question {actualQuestion}";
var parameters = dialogflowRequest.QueryResult.Parameters;
var dialogflowResponse = new WebhookResponse
{
FulfillmentText = testAnswer,
FulfillmentMessages =
{ new Intent.Types.Message
{ SimpleResponses = new Intent.Types.Message.Types.SimpleResponses
{ SimpleResponses_ =
{ new Intent.Types.Message.Types.SimpleResponse
{
DisplayText = testAnswer,
TextToSpeech = testAnswer,
}
}
}
}
}
};
var jsonResponse = dialogflowResponse.ToString();
return new ContentResult
{
Content = jsonResponse,
ContentType = "application/json"
};
Published the app to Azure so there's a webhook URl.
Now, when I test it in dialogflow, the response is:
"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: Content in message google.cloud.dialogflow.v2.WebhookResponse."
Which I do not understand.....what am I missing here?
(here's the screenshot of the response:)
The solution to this problem is to return JsonResult instead of the ContentResult.
[System.Web.Http.HttpPost]
public JsonResult DialogAction([FromBody] WebhookRequest dialogflowRequest)
{
var intentName = dialogflowRequest.QueryResult.Intent.DisplayName;
var actualQuestion = dialogflowRequest.QueryResult.QueryText;
var testAnswer = $"Dialogflow Request for intent {intentName} and question {actualQuestion}";
var parameters = dialogflowRequest.QueryResult.Parameters;
var dialogflowResponse = new WebhookResponse
{
FulfillmentText = testAnswer,
FulfillmentMessages =
{ new Intent.Types.Message
{ SimpleResponses = new Intent.Types.Message.Types.SimpleResponses
{ SimpleResponses_ =
{ new Intent.Types.Message.Types.SimpleResponse
{
DisplayText = testAnswer,
TextToSpeech = testAnswer,
}
}
}
}
}
};
var jsonResponse = dialogflowResponse.ToString();
return Json(jsonResponse);

Subscribing to Azure Push Notification service in Xamarin Forms

I tried to integrate with Push Notifications to my forms application. Azure messaging component is used for achieving this.
Below is the code i am using. I am getting the trigger to RegisteredForRemoteNotifications method. But RegisterNativeAsync method doesn't seem to be doing the job.
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var push = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(push);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
const UIRemoteNotificationType not = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(not);
}
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Hub = new SBNotificationHub(conStirng, NotifHubPath);
Hub.UnregisterAllAsync(deviceToken, (error) =>
{
//Get device token
var id = deviceToken.ToString();
var tag = "username";
var tags = new List<string> { tag };
Hub.RegisterNativeAsync(id, new NSSet(tags.ToArray()), (errorCallback) =>
{
if (errorCallback != null)
{
//Log to output
}
});
});
}
What am i doing wrong here? How can i confirm if the Register function is success or failure.?
You need to check if the error from the response of the register method is null or not. if it is null means the it is a success.
var hub = new SBNotificationHub (cs, "your-hub-name");
hub.RegisterNativeAsync (deviceToken, null, err => {
if (err != null)
Console.WriteLine("Error: " + err.Description);
else
Console.WriteLine("Success");
});
In the case of windows universal apps we can check the registrationId property of the response.
private async void InitNotificationsAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var hub = new NotificationHub("<hub name>", "<connection string with listen access>");
var result = await hub.RegisterNativeAsync(channel.Uri);
// Displays the registration ID so you know it was successful
if (result.RegistrationId != null)
{
var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
}

Resources