Skip to content

Instantly share code, notes, and snippets.

@tonysneed
tonysneed / gist:d7f6533e12e5f7e1c0fc
Last active March 29, 2024 14:27
Serialization Helper for ASP.NET Web API
using System;
using System.Net.Http.Formatting;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using WebApiContrib.Formatting;
namespace WebApiContrib
{
public static class SerializationHelper
{
@tonysneed
tonysneed / Mac OS X: Open in Visual Studio Code
Last active March 27, 2024 10:02
Add a command to Finder services in Mac OSX to open a folder in VS Code
- Open Automator
- File -> New -> Service
- Change "Service Receives" to "files or folders" in "Finder"
- Add a "Run Shell Script" action
- Change "Pass input" to "as arguments"
- Paste the following in the shell script box: open -n -b "com.microsoft.VSCode" --args "$*"
- Save it as something like "Open in Visual Studio Code"
@tonysneed
tonysneed / vs-code-ts-version.md
Created October 23, 2016 07:09
Use Specific Version of TypeScript with VS Code

NOTE: These steps are only required if you want to use a version of TypeScript that is not the same as the version that is bundled with Visual Studio Code.

  • Install the latest version of TypeScript

    npm install -g typescript@version
    
  • Configure VS Code to use installed version of TypeScript by opening Preferences, User Settings.

@tonysneed
tonysneed / ProductsDbContextFactory3.cs
Last active September 4, 2023 22:11
ProductsDbContextFactory3
public class ProductsDbContextFactory : IDesignTimeDbContextFactory<ProductsDbContext>
{
public ProductsDbContext CreateDbContext(string[] args)
{
// Get environment
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
// Build config
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../EfDesignDemo.Web"))
[Binding]
public class WeatherHooks
{
private readonly IObjectContainer _objectContainer;
private const string AppSettingsFile = "appsettings.json";
public WeatherHooks(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}
private async Task ClearData(
WebApplicationFactory<Program> factory)
{
if (factory.Services.GetService(typeof(IWeatherRepository))
is not IWeatherRepository repository) return;
var entities = await repository.GetAsync();
foreach (var entity in entities)
await repository.RemoveAsync(entity.Id);
}
private WebApplicationFactory<Program> GetWebApplicationFactory() =>
new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
IConfigurationSection? configSection = null;
builder.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), AppSettingsFile));
configSection = context.Configuration.GetSection(nameof(WeatherDatabaseSettings));
});
{
"WeatherDatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "WeathersTestDb",
"CollectionName": "Weathers"
}
}
{
"id": 1,
"date": "2022-01-01T06:00:00Z",
"temperatureC": 32,
"temperatureF": 89,
"summary": "Freezing",
"eTag": "6e9eff61-a3ed-4339-93fb-24151149b46c"
}
[Binding]
public class WeatherWebApiStepDefinitions
{
private const string BaseAddress = "http://localhost/";
public WebApplicationFactory<Program> Factory { get; }
public IWeatherRepository Repository { get; }
public HttpClient Client { get; set; } = null!;
private HttpResponseMessage Response { get; set; } = null!;
public JsonFilesRepository JsonFilesRepo { get; }
private WeatherForecast? Entity { get; set; }