Azure SDK for .NET provides very useful features to deal with Azure services and resources including Application Insights. With this SDK, we can easily create or update resources for Application Insights and alerts. However, surprisingly this SDK doesn’t provide creating Web Tests resources to check availability. In this post, we will walk through how to create web tests programmatically using C# codes.

Web Tests in Azure Portal

Once an Application Insights instance is created in Azure Portal, we can easily create web tests by following this post, Monitor availability and responsiveness of any web site. This is pretty straightforward, so we’re not going to do here.

Web Tests in PowerShell

The problem using the approach above is that we should manually create or update web tests through Azure Portal. With PowerShell script, we can easily create or update web tests by following this post, Creating an Application Insights Web Test and Alert Programmatically. This is also quite clear to understand, so we’re not dealing it with here.

There’s a question still left. How can I create or update web tests and alerts using C# codes? This part has not been yet supported. Of course we can, but not that as easy as others. Let’s sort this out.

Code used in this post can be found at https://github.com/aliencube/Application-Insights-WebTests-SDK.

Analysing and Structuring Web Test Resources

Azure provides a website called Azure Resource Explorer. After logging in, we’re able to see a similar screen to this:

This is how a web test resource looks like. From this JSON object, we need to create strongly-typed object classes. Let’s say this is called WebTestResource that inherits GenericResource.

This is a basic structure. It needs to implement the Properties property. Currently the web test resource supports both URL ping test and multi-step test. We’re only dealing with URL ping test in this post.

Web Test Properties – URL Ping

So, we’re creating the PingWebTestProperties class inheriting the WebTestProperties class.

Note that WebTestProperties has an implicit operator that automatically convert the object to serialised JSON string. This is because GenericResource.Properties takes serialised JSON string value.

If you’re not familiar with implicit operator, this MSDN document will give you an insight.

Similarly, PingWebTestProperties has a property Configuration returning PingWebTestConfiguration. This has a WebTest property returning a serialised XML string like:

This time, we just use an extension method, ToXml(), but we can of course implement the same implicit operator for XML serialisation.

We’ve now got a strongly-typed object to create or update web test. Let’s move onto the next step.

Create or Update Web Test Resource

Azure SDK has ResourceManagementClient to handle Azure resources. With this, we can simply call the method to create a web test resource.

If the resource has been successfully created or updated, the result will return HttpStatusCode.Ok (200) or HttpStatusCode.Created (201). If you have a question around credentials, it’s worth noting the following part.

ClientCredential or UserCredential

It’s entirely up to the business decision that we should use either ClientCredential or UserCredential to access to the ResourceManagementClient object. If we need users to provide their username and password, we should use UserCredential; otherwise we should use ClientCredential. In either case, we should register an application to Azure Active Directory (AAD).

ClientCredential

Using ClientCredential requires ClientId and ClientSecret. Therefore, when we register an application to AAD, we should choose the WEB APPLICATION AND/OR WEB API option.

Then, add an application to the Permissions to other applications section like:

By doing so, we can access to Azure resources using ADAL without providing username and password like:

UserCredential

This time, let’s consider the UserCredential object. The ClientId value is only necessary for this scenario. But, if we use the existing application registered to AAD for ClientCredential, the following exception will be thrown and authentication fails:

Additional information: AADSTS90014: The request body must contain the following parameter: ‘client_secret or client_assertion’.

Hence, we need to register another application to AAD with the option of NATIVE CLIENT APPLICATION.

Then, add an application to the Permissions to other applications section like:

By doing so, we can access to Azure resoures using ADAL with username and password like:

So far, we’ve looked through how we can create a web test resource for Application Insights programmatically, using C# codes. Even though this post only dealt with a URL ping test, same approach can be used for multi-step test resources. If this is something you are interested in, this NuGet package is for you.

Category:
Application Development and Integration
Tags:
, ,

Join the conversation! 3 Comments

  1. This is a very nice article explaning about webtests. Do you have any examples which integrates with Multi Factor authentications

  2. How can I use certificates with App Insight?

  3. Please confirm if I can create C# multi-step web tests for sites hosted on https. I tried web tests from Visual studio, but fails in app sight as could not load custom test web plugin for updating security protocol.

    Thanks,

Comments are closed.