Create .NET Client to Connect to Direct Line App Service extension
APPLIES TO: SDK v4
This article describes how to create a .NET client in C# which connects to the Direct Line App Service extension. Please, also read this companion article Configure .NET bot for extension.
Get Direct Line secret key
In your browser, navigate to the Azure portal
In the Azure portal, locate your Azure Bot Service resource
Click on Channels to configure the bot's channels
If it is not already enabled, click on the Direct Line channel to enable it.
If it is already enabled, in the Connect to channels table click on the Edit link on the Direct Line row.
Scroll to the Sites section. There is typically a Default Site unless you have deleted or renamed it.
Click on the Show link to reveal one of the keys, then copy and save its value. You will use this value in the section Create a C# Direct Line client.

Note
This value is your Direct Line client secret used to connect to Direct Line App Service extension. You can create additional sites if you'd like and use those secret values as well.
Create a C# Direct Line client
Interactions with the Direct Line App Service extension happen differently than traditional Direct Line because most communication happens over a WebSocket. The updated Direct Line client includes helper classes for opening and closing a WebSocket, sending commands through the WebSocket, and receiving Activities back from the bot. This section describes how to create a simple C# client to interact with a bot.
In Visual Studio, create a new .NET Core console application project.
Clone the Direct Line client from GitHub repository and include it in your project.
Create a client and generate a token using a secret. This step is the same as building any other C# Direct Line client except the endpoint you need use in your bot, appended with the .bot/ path as shown next. Do not forget the ending /.
string endpoint = "https://<your_bot_name>.azurewebsites.net/.bot/"; string secret = "<your_bot_direct_line_secret_key>"; var tokenClient = new DirectLineClient( new Uri(endpoint), new DirectLineClientCredentials(secret)); var conversation = await tokenClient.Tokens.GenerateTokenForNewConversationAsync();Notice the following:
- The endpoint value is the bot URL you obtained when you deployed the bot to Azure. For more information, see Configure .NET bot for extension.
- The secret value shown as YOUR_BOT_SECRET is the value you saved earlier from the sites section.
Once you have a conversation reference from generating a token, you can use this conversation ID to open a WebSocket with the new
StreamingConversationsproperty on theDirectLineClient. To do this you need to create a callback that will be invoked when the bot wants to sendActivitySetsto the client:public static void ReceiveActivities(ActivitySet activitySet) { if (activitySet != null) { foreach (var a in activitySet.Activities) { if (a.Type == ActivityTypes.Message && a.From.Id.Contains("bot")) { Console.WriteLine($"<Bot>: {a.Text}"); } } } }Now you are ready to open the WebSocket on the
StreamingConversationsproperty using the conversation's token,conversationId, and yourReceiveActivitiescallback:var client = new DirectLineClient( new Uri(endpoint), new DirectLineClientCredentials(conversation.Token)); await client.StreamingConversations.ConnectAsync( conversation.ConversationId, ReceiveActivities);The client can now be used to start a conversation and send
Activitiesto the bot:var startConversation = await client.StreamingConversations.StartConversationAsync(); var from = new ChannelAccount() { Id = "123", Name = "Fred" }; var message = Console.ReadLine(); while (message != "end") { try { var response = await client.StreamingConversations.PostActivityAsync( startConversation.ConversationId, new Activity() { Type = "message", Text = message, From = from }); } catch (OperationException ex) { Console.WriteLine( $"OperationException when calling PostActivityAsync: ({ex.StatusCode})"); } message = Console.ReadLine(); }