Skip to content

Instantly share code, notes, and snippets.

@dschenkelman
dschenkelman / InitialTest.cs
Last active January 1, 2016 09:19
IntegrationTests
public class NugetPackageInstallationTests : IDisposable
{
private readonly string _originalWorkingDirectory;
private readonly string _filesDirectory;
private string _localDirectory;
public NugetPackageInstallationTests()
{
_originalWorkingDirectory = Environment.CurrentDirectory;
_filesDirectory = Path.Combine(AcceptanceTestHelpers.GetExecutingAssemblyDirectory(), "Files");
@dschenkelman
dschenkelman / Async.cs
Last active January 1, 2016 09:49
Async XBehave Steps
[Trait("AcceptanceTest", "RunningScriptsTests")]
[Trait("Requires", "Internet Connection")]
[Trait("Requires", "Administrator Privileges")]
public void RunningWebApiHostSampleEnablesWebAccessInLocalhostXBehave(string originalWorkingDirectory, Task<int> scriptTask)
{
const string MethodName = "RunningWebApiHostSampleEnablesWebAccessInLocalhostXBehave";
"Given an existing Web API host sample (similar to https://github.com/scriptcs/scriptcs-samples/tree/master/webapihost)"
._(() =>
{
using Roslyn.Scripting.CSharp;
using System;
namespace RoslynTests
{
class Program
{
static void Main(string[] args)
{
var script = "using System; public class Test{ public void Do<T>() where T : IDisposable{ } }";
@dschenkelman
dschenkelman / APM.cs
Created May 9, 2014 04:10
Asynchronous I/O in C#: Why tasks (a.k.a. promises, futures)?
class ProgramWithAPM : IRunnable
{
private int pending;
private readonly object lockObject;
public ProgramWithAPM()
{
this.pending = 0;
this.lockObject = new object();
@dschenkelman
dschenkelman / CreateFile.cs
Created May 9, 2014 04:24
Asynchronous I/O in C#: I/O Completion Ports
const uint Flags = 128 | (uint)1 << 30;
var fileHandle = Interop.CreateFile("test.txt", (uint)1 << 31, 0, IntPtr.Zero, 3,
/*FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED */ Flags,
IntPtr.Zero);
Interop.CreateIoCompletionPort(
fileHandle,
completionPortHandle,
(uint)fileHandle.ToInt64(),
@dschenkelman
dschenkelman / FakeNavigationService.cs
Created May 9, 2014 04:29
fakewin8: Easy fakes for Windows Store apps unit tests
public class FakeNavigationService : INavigationService
{
public FakeAction<string> NavigateAction { get; set; }
public FakeAction GoBackAction { get; set; }
public void Navigate(string viewName)
{
this.NavigateAction.Invoke(viewName);
}
@dschenkelman
dschenkelman / AsyncMain.cs
Created May 9, 2014 04:35
Asynchronous I/O in C#: Introduction
static void Main()
{
var fs = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
var buffer = new byte[1024];
fs.BeginRead(buffer, 0, 1024, ReadCallback, new State { Buffer = buffer, FileStream = fs });
Console.ReadLine();
}
@dschenkelman
dschenkelman / Extensions.cs
Created May 9, 2014 04:38
Intro to OWIN talk and a simple IP filtering middleware sample
namespace Owin.IpFilter
{
using System;
using System.Net;
public static class Extensions
{
public static IAppBuilder UseIpFiltering(this IAppBuilder appBuilder, Func<IPAddress, bool> rejectRequest)
{
appBuilder.Use(typeof(IpFilterMiddleware), rejectRequest);
@dschenkelman
dschenkelman / sample.csx
Created May 9, 2014 04:40
scriptcs TCP client and server Script Pack: ScriptCs.Net
var net = Require<Net>();
var server = net.CreateServer(socket =>
{
Console.WriteLine("New connection");
socket.On(
data: bytes => Console.Write(bytes.AsString()),
close: () => Console.WriteLine("Connection closed"),
error: e => Console.WriteLine("Error: {0}\r\nStackTrace: {1}", e.Message, e.StackTrace));