Using .NET Interactive NoteBooks for writing interactive document(s)

LAI TOCA
3 min readApr 16, 2021
Photo from: https://github.com/dotnet/interactive/blob/main/docs/kernels-overview.md

Introduce

Supporting operation usually as daily task(s) that IT guys running DevOps. How to guide your teams’ member for the routine operation(s) while system drop into accident or troubleshooting?

Traditional way we could write down in document(s) and with well-format manual. But for now, we got better solution for it. Thanks Microsoft that based on the ideal of Jupyter NoteBook and created the product of .NET Interactive NoteBooks. The notebook enable people to have markdown section and embed code (C#/F#/SQL/PowerShell…) block together that enrich the powerful not merely the static document(s) as before.

Image that we have 3 steps of routine operation, we could just open the notebook, according the situation and following the guide of documents then execute the corresponds code’s sector. Everything could be done under single page (per notebook), how sweet day was:).

Environment & Simply example

Though the .NET Interactive NoteBooks still in preview. We could also download it under the marketplace through Visual Studio Code.

Install step by step then we could start developing our first .NET Interactive NoteBooks.

Suppose we have below scenario: Networking team inform that some authentication API service(s) mater server(s) was temporary unavailable due to network issue. The customer service team ask us to switch backup authentication API service(s) up (should be locate in different network area).

That’s see how to handle this operation on notebook:

Step1, we might need to to found out where authentication server(s) located.

#!csharp // c# as interpreter// restore the required package
#r "nuget:Microsoft.DotNet.Interactive.SqlServer,*-*"

Suppose that our server available list all stored in the SQL database.

#!csharp
// connect to db start and create sub kernel
#!connect mssql --kernel-name mydatabase "Persist Security Info=False;User ID=USER_ID;Password=PASSWORD;Initial Catalog=DATABASE_NAME;Server=YOUR_DB_SERVER_URI"

If everything went fine, you could see below cell output:

Next we could use this sub-kernel for interactive with our target database.

#!sql #!sql-mydatabase
SELECT
URI
FROM
[dbo].[API_Service]
WHERE Resource_Name = 'authentication'
AND Role = 'Slave'

Step2, switch on those slave server(s), via command.

Then we could copy the output result from step1 and execute the below block.

#! powershelldocker -H <uri> run -t -d -p port:port --rm <image-name>

Step3, fire API to make sure the health of the API service.

If slave authentication API beginning service after step2. We could ping the service to make sure service alive healthy.

#! powershelltry {
$Uri = "https://xxxxx/api/token/welcome"
$Response = Invoke-WebRequest -Uri $EndPoint -Method GET
Write-Output "StatusCode: $($Response.StatusCode)"
Write-Output $Response.Content | ConvertTo-Json
}
catch {
Write-Output $Error[0]
}

If you see output area with http status code 200. Then you could inform your customer team that switch were all set.

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)