![](/style/images/good.png)
![](/style/images/bad.png)
Chaos Monkey - Part 4 - Creating an Asp.Net 6 Application that Caches an Error -...
source link: https://www.pmichaels.net/2022/01/30/chaos-monkey-part-4-creating-an-asp-net-6-application-that-caches-an-error/?utm_campaign=chaos-monkey-part-4-creating-an-asp-net-6-application-that-caches-an-error
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Chaos Monkey – Part 4 – Creating an Asp.Net 6 Application that Caches an Error
This is a really strange post, but it’s a line up for a different post; however, I felt it made sense to be a post in its own right – it follows on from a trend I have of creating things that break on purpose. For example, here’s a post from a few years ago where I discussed how you might force a machine to run out of memory.
In this case, I’m creating a simple application that runs fine, but at a random point, it generates an error, which it caches, and then is broken until the application is restarted.
I’m working on some alerting and resilience experiments at the minute, and having an unstable application is useful for those tests. Also, this is not an unusual scenario – I mean, obviously, writing an application that purposes crashes after it’s broken, and from then on, is unusual; but having an application that does this somewhere in your estate may not be so unusual.
I’ve set-up a bog standard Asp.Net MVC 6 application. I then installed the following package:
Install-Package System.Runtime.Caching
Finally, I changed the default Privacy controller action to potentially crash:
public
IActionResult Privacy()
{
string
result = Crash();
return
View(model: result);
}
Here, I’m feeding a string into the privacy view as its model. The Crash method has a 1 in 10 chance of caching an error:
private
string
Crash()
{
if
(!_memoryCache.TryGetValue(
"Error"
,
out
string
errorCache))
{
if
(_random.Next(10) == 1)
{
_memoryCache.Set(
"Error"
,
"Now broken!"
);
return
"Now broken"
;
}
}
else
{
throw
new
Exception(
"Some exception"
);
}
return
"Working fine"
;
}
I then just display the model in the view (privacy.cshtml):
@model
string
@{
ViewData[
"Title"
] =
"Privacy Policy"
;
}
<h1>@ViewData[
"Title"
]</h1>
<h1>@Model</h1>
<p>Use
this
page to detail your site's privacy policy.</p>
Now, if you run it, somewhere between 2 and 15 times, you’re likely to see it break, and need to restart to fix.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK