Skip to content

Instantly share code, notes, and snippets.

@bellons91
Created February 18, 2021 16:05
Show Gist options
  • Save bellons91/c84c552da6185af6821b0155091e4874 to your computer and use it in GitHub Desktop.
Save bellons91/c84c552da6185af6821b0155091e4874 to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using System;
using System.Net.Http;
async Task Main()
{
var isAvailable = await IsArticleAvailable();
Console.WriteLine(isAvailable);
}
Task<bool> IsArticleAvailable()
{
var articlePath = "/blog/clean-code-error-handling";
return IsPathAvailable(articlePath);
}
Task<bool> IsPathAvailable(string articlePath)
{
var baseUrl = "https://www.code4it.dev/";
return IsResourceAvailable(baseUrl, articlePath);
}
async Task<bool> IsResourceAvailable(string baseUrl, string articlePath)
{
using (HttpClient client = new HttpClient() { BaseAddress = new Uri(baseUrl) })
{
HttpResponseMessage response = await client.GetAsync(articlePath);
return response.IsSuccessStatusCode;
}
}
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using System.Threading.Tasks;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[CompilerGenerated]
internal static class <Program>$
{
private sealed class <<<Main>$>g__Main|0_0>d : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder <>t__builder;
private bool <isAvailable>5__1;
private bool <>s__2;
private TaskAwaiter<bool> <>u__1;
private void MoveNext()
{
int num = <>1__state;
try
{
TaskAwaiter<bool> awaiter;
if (num != 0)
{
awaiter = <<Main>$>g__IsArticleAvailable|0_1().GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter;
<<<Main>$>g__Main|0_0>d stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter<bool>);
num = (<>1__state = -1);
}
<>s__2 = awaiter.GetResult();
<isAvailable>5__1 = <>s__2;
Console.WriteLine(<isAvailable>5__1);
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult();
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
private sealed class <<<Main>$>g__IsResourceAvailable|0_3>d : IAsyncStateMachine
{
public int <>1__state;
public AsyncTaskMethodBuilder<bool> <>t__builder;
public string baseUrl;
public string articlePath;
private HttpClient <client>5__1;
private HttpResponseMessage <response>5__2;
private HttpResponseMessage <>s__3;
private TaskAwaiter<HttpResponseMessage> <>u__1;
private void MoveNext()
{
int num = <>1__state;
bool isSuccessStatusCode;
try
{
if (num != 0)
{
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(baseUrl);
<client>5__1 = httpClient;
}
try
{
TaskAwaiter<HttpResponseMessage> awaiter;
if (num != 0)
{
awaiter = <client>5__1.GetAsync(articlePath).GetAwaiter();
if (!awaiter.IsCompleted)
{
num = (<>1__state = 0);
<>u__1 = awaiter;
<<<Main>$>g__IsResourceAvailable|0_3>d stateMachine = this;
<>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = <>u__1;
<>u__1 = default(TaskAwaiter<HttpResponseMessage>);
num = (<>1__state = -1);
}
<>s__3 = awaiter.GetResult();
<response>5__2 = <>s__3;
<>s__3 = null;
isSuccessStatusCode = <response>5__2.IsSuccessStatusCode;
}
finally
{
if (num < 0 && <client>5__1 != null)
{
((IDisposable)<client>5__1).Dispose();
}
}
}
catch (Exception exception)
{
<>1__state = -2;
<>t__builder.SetException(exception);
return;
}
<>1__state = -2;
<>t__builder.SetResult(isSuccessStatusCode);
}
void IAsyncStateMachine.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
this.MoveNext();
}
[DebuggerHidden]
private void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
{
//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
this.SetStateMachine(stateMachine);
}
}
private static void <Main>$(string[] args)
{
}
[AsyncStateMachine(typeof(<<<Main>$>g__Main|0_0>d))]
[DebuggerStepThrough]
internal static Task <<Main>$>g__Main|0_0()
{
<<<Main>$>g__Main|0_0>d stateMachine = new <<<Main>$>g__Main|0_0>d();
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
internal static Task<bool> <<Main>$>g__IsArticleAvailable|0_1()
{
string articlePath = "/blog/clean-code-error-handling";
return <<Main>$>g__IsPathAvailable|0_2(articlePath);
}
internal static Task<bool> <<Main>$>g__IsPathAvailable|0_2(string articlePath)
{
string baseUrl = "https://www.code4it.dev/";
return <<Main>$>g__IsResourceAvailable|0_3(baseUrl, articlePath);
}
[AsyncStateMachine(typeof(<<<Main>$>g__IsResourceAvailable|0_3>d))]
[DebuggerStepThrough]
internal static Task<bool> <<Main>$>g__IsResourceAvailable|0_3(string baseUrl, string articlePath)
{
<<<Main>$>g__IsResourceAvailable|0_3>d stateMachine = new <<<Main>$>g__IsResourceAvailable|0_3>d();
stateMachine.<>t__builder = AsyncTaskMethodBuilder<bool>.Create();
stateMachine.baseUrl = baseUrl;
stateMachine.articlePath = articlePath;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
{
"version": 1,
"target": "C#",
"mode": "Debug"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment