Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The SpeechRecognizer
and other objects in the Speech SDK automatically connect to the Speech service when it's appropriate. Sometimes, you either want extra control over when connections begin and end or you want more information about when the Speech SDK establishes or loses its connection. The supporting Connection
class provides this capability.
A Connection
can be obtained from most top-level Speech SDK objects via a static From...
factory method, for example, Connection::FromRecognizer(recognizer)
for SpeechRecognizer
.
var connection = Connection.FromRecognizer(recognizer);
auto connection = Connection::FromRecognizer(recognizer);
Connection connection = Connection.fromRecognizer(recognizer);
A Connection
raises Connected
and Disconnected
events when the corresponding status change happens in the Speech SDK's connection to the Speech service. You can listen to these events to know the latest connection state.
connection.Connected += (sender, connectedEventArgs) =>
{
Console.WriteLine($"Successfully connected, sessionId: {connectedEventArgs.SessionId}");
};
connection.Disconnected += (sender, disconnectedEventArgs) =>
{
Console.WriteLine($"Disconnected, sessionId: {disconnectedEventArgs.SessionId}");
};
connection->Connected += [&](ConnectionEventArgs& args)
{
std::cout << "Successfully connected, sessionId: " << args.SessionId << std::endl;
};
connection->Disconnected += [&](ConnectionEventArgs& args)
{
std::cout << "Disconnected, sessionId: " << args.SessionId << std::endl;
};
connection.connected.addEventListener((s, connectionEventArgs) -> {
System.out.println("Successfully connected, sessionId: " + connectionEventArgs.getSessionId());
});
connection.disconnected.addEventListener((s, connectionEventArgs) -> {
System.out.println("Disconnected, sessionId: " + connectionEventArgs.getSessionId());
});
Connection
has explicit methods to start or end a connection to the Speech service. Reasons you might want to control the connection include:
- Preconnecting to the Speech service to allow the first interaction to start as quickly as possible.
- Establishing connection at a specific time in your application's logic to gracefully and predictably handle initial connection failures.
- Disconnecting to clear an idle connection when you don't expect immediate reconnection but also don't want to destroy the object.
Some important notes on the behavior when manually modifying connection state:
- Trying to connect when already connected doesn't generate an error. Monitor the
Connected
andDisconnected
events if you want to know the current state of the connection. - A failure to connect that originates from a problem that has no involvement with the Speech service--such as attempting to do so from an invalid state--results in an error as appropriate to the programming language. Failures that require network resolution--such as authentication failures--doesn't result in an error but instead generate a
Canceled
event on the top-level object theConnection
was created from. - Manually disconnecting from the Speech service during an ongoing interaction results in a connection error and loss of data for that interaction. Connection errors are surfaced on the appropriate top-level object's
Canceled
event.
try
{
connection.Open(forContinuousRecognition: false);
}
catch (ApplicationException ex)
{
Console.WriteLine($"Couldn't pre-connect. Details: {ex.Message}");
}
// ... Use the SpeechRecognizer
connection.Close();
try
{
connection->Open(false);
}
catch (std::runtime_error&)
{
std::cout << "Unable to pre-connect." << std::endl;
}
// ... Use the SpeechRecognizer
connection->Close();
try {
connection.openConnection(false);
} catch {
System.out.println("Unable to pre-connect.");
}
// ... Use the SpeechRecognizer
connection.closeConnection();