Skip to content

Instantly share code, notes, and snippets.

View matthewrwilton's full-sized avatar

Matthew Wilton matthewrwilton

View GitHub Profile
@matthewrwilton
matthewrwilton / view-compilation-with-Exec.csproj
Created March 12, 2017 12:49
Example of Exec task usage to compile ASP.NET razor views and exclude the node_modules folder.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<Exec Command="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v temp -p $(WebProjectOutputDir) -x node_modules"/>
</Target>
...
</Project>
@matthewrwilton
matthewrwilton / view-compilation-with-AspNetCompiler.csproj
Created March 12, 2017 12:45
Example of AspNetCompiler task usage.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
...
</Project>
[CollectionDefinition(nameof(WebAppFixture))]
public class WebAppFixtureTestCollection : ICollectionFixture<WebAppFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}
/// <summary>
/// Starts an IIS Express instance to run the ExampleWebApp.
///
/// Code heavily borrowed from Michael Whelan's blog post and Seleno (MIT license).
/// - Michael Whelan's blog post http://www.michael-whelan.net/testing-mvc-application-with-iis-express-webdriver/
/// - Seleno https://github.com/TestStack/TestStack.Seleno
///
/// This is an xUnit fixture class. Refer to https://xunit.github.io/docs/shared-context.html.
/// </summary>
public class WebAppFixture : IDisposable
[Collection(nameof(WebAppFixture))]
public class _2_Running_A_Web_App
{
/*
* This is a bare minimum example of starting IIS Express to run a web application for testing.
* It uses xUnit collection fixtures to start the web application before running the test.
* Only a single instance of the application is started and is shared between all tests in the collection.
*/
[Fact]
@matthewrwilton
matthewrwilton / partial_web.config
Created June 28, 2016 13:21
Elastic Load Balancer IIS HTTP to HTTPs Redirect
<rule name="HTTP to HTTPs Redirect" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HEADER_X-Forwarded-Proto}" pattern="https" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent"/>
</rule>
public Task<T> ExecuteReaderAsync<T>(string sql, Func<DbDataReader, Task<T>> readAction)
{
...
}
public async Task<T> ExecuteReaderAsync<T>(string sql, Func<DbDataReader, T> readAction)
{
return await _pool.ExecuteAsync(_connectionString, async connection =>
{
using (var command = new OdbcCommand(sql, connection))
{
var dataReader = await command.ExecuteReaderAsync();
return readAction(dataReader);
}
});
public Task ExecuteReaderAsync(string sql, Action<DbDataReader> readAction)
{
...
}
public Task<T> ExecuteReaderAsync<T>(string sql, Func<DbDataReader, T> readAction)
{
...
}
public async Task<List<Data>> ExecuteQuery(string sql)
{
var results = new List<Data>();
await _querier.ExecuteReaderAsync(sql, async reader =>
{
while (await reader.ReadAsync())
{
results.Add(ReadData(reader));
}
});