Go to the profile of  Leo
why do i need a bio? they are painful to write.
1 min read

How to use Microsoft Bot Direct Line

How to use Microsoft Bot Direct Line

In this blog post, I'll show you what was my setup when creating a simple .NET console application with Microsoft Bot Direct Line Framework.

Platform Versions Used

  1. Azure Bot Framework SDK v4 (C#)
  2. Visual Studio 2017

Nuget Packages

  1. WebSocketSharp 1.0.3-rc11
  2. Microsoft.Bot.Connector.DirectLine 3.0.2
private static async Task Main()
    var tokenResponse = new DirectLineClient(Settings.BotSecret).Tokens

    var conversation = await InitializeDirectLine(tokenResponse);
    var webSocketClient = new WebSocket(conversation.StreamUrl)
        SslConfiguration = {EnabledSslProtocols = SslProtocols.Tls12}

    webSocketClient.OnOpen += (sender, args) => WriteLine("Connected to direct line.");
    webSocketClient.OnMessage += async (eventArgs) => { await WebSocketClientOnOnMessage(eventArgs); };

private static async Task WebSocketClientOnOnMessage(MessageEventArgs e)
        if (e.Data == null) return;
        var activitySet = JsonConvert.DeserializeObject<ActivitySet>(e.Data);

        if (activitySet == null) return;
        foreach (var activity in activitySet.Activities)
            if (activity.Type != ActivityTypes.Message || string.IsNullOrEmpty(activity.Text)) continue;
    catch (Exception exception)

You might get excited and start running the application and just be greeted with this error:

An error has occured during TSL Handshake

The reason for this is the default protocol that is being used by the websocket is not meeting the requirements for the Bot Service.

According to the post

On December 4th, 2018, the Azure Bot Service will require all connections to be secured using Transport Layer Security (TLS) 1.2.

But I am actually telling the web socket to use TLS 1.2 and yet I'm still getting errors.

I did not investigate further whether it's regarding to the library that I'm using that's why it's not using TLS 1.2. A quick workaround was to specify the following before creating the direct line connection

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Run the application and you should be able to communicate now to your bot!