This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Net; | |
| namespace Urls | |
| { | |
| public record QueryParameter | |
| { | |
| private string? fieldValue; | |
| public string FieldName { get; init; } | |
| public string? Value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Dependency Injection abstraction for Rest Clients. Use the CreateClient delegate to create an IClient when more than one is needed for an application. | |
| /// </summary> | |
| public interface IClient | |
| { | |
| /// <summary> | |
| /// Sends a strongly typed request to the server and waits for a strongly typed response | |
| /// </summary> | |
| /// <typeparam name="TResponseBody">The expected type of the response body</typeparam> | |
| /// <param name="request">The request that will be translated to a HTTP request</param> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var serviceCollection = new ServiceCollection() | |
| //Add a service which has an IClient dependency | |
| .AddSingleton<IGetString, GetString1>() | |
| //Add RestClient.Net with a default Base Url of http://www.test.com | |
| .AddRestClient((o) => o.BaseUrl = "http://www.test.com".ToAbsoluteUrl()); | |
| //Use HttpClient dependency injection | |
| _ = serviceCollection.AddHttpClient(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [<TestMethod>] | |
| member this.TestComposition () = | |
| let uri = | |
| "host.com".ToHttpUrlFromHost(5000) | |
| .AddQueryParameter("fieldname1", "field<>Value1") | |
| .WithCredentials("username", "password") | |
| .AddQueryParameter("FieldName2", "field<>Value2") | |
| .WithFragment("frag") | |
| .WithPath("pathpart1", "pathpart2") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var client = new Client( | |
| new NewtonsoftSerializationAdapter(), | |
| baseUrl: testServerBaseUri, | |
| defaultRequestHeaders: | |
| HeadersExtensions | |
| .FromJsonContentType() | |
| //Non-destructive mutation to create a new headers collection | |
| .WithBearerTokenAuthentication(bearerToken)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Immutable; | |
| namespace JsonDataStructure | |
| { | |
| public record JsonValue | |
| { | |
| public ValueType ValueType { get; } | |
| public string? StringValue { get; init; } | |
| public bool? BooleanValue { get; init; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Linq; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Collections.Immutable; | |
| #pragma warning disable format | |
| #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. | |
| #pragma warning disable CS8604 // Possible null reference argument. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #nullable enable | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| namespace TestProject4 | |
| { | |
| public interface IDependency { void DoSomething(); } | |
| public class NullDependency : IDependency | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <TargetFrameworks>net6.0, net45, netstandard2_0</TargetFrameworks> | |
| <ImplicitUsings>enable</ImplicitUsings> | |
| <Nullable>enable</Nullable> | |
| </PropertyGroup> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFramework>net6.0</TargetFramework> | |
| <ImplicitUsings>enable</ImplicitUsings> | |
| <Nullable>enable</Nullable> | |
| <AnalysisMode>AllEnabledByDefault</AnalysisMode> | |
| <AnalysisLevel>latest</AnalysisLevel> | |
| <TreatWarningsAsErrors>true</TreatWarningsAsErrors> |