Skip to content

Instantly share code, notes, and snippets.

View darrelmiller's full-sized avatar

Darrel darrelmiller

View GitHub Profile
CREATE FUNCTION GetProgressionReviewHours(@AsOfDate DateTime)
RETURNS @Hours TABLE (Employee_Id int,
EmployeeContract_Id int,
ProgressionReviewPeriod int,
WorkedHours numeric(18,2),
TotalHours numeric(18,2))
BEGIN
INSERT INTO @Hours
@darrelmiller
darrelmiller / gist:4711532
Last active December 12, 2015 04:08
Convert attributes to elements throughout a document
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*">
<xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>
</xsl:template>
<xsl:template match="/ | @* |node()">
<xsl:copy>
<xsl:apply-templates select="* | @* | node()"/>
CookieContainer cookieJar = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = true
};
handler.UseCookies = true;
handler.UseDefaultCredentials = false;
[[ch_the_application]]
== The Application ==
Up to this point we have discussed the tools that you can use to build Web APIs. We have discussed the fundamentals of the HTTP protocol, the basics of using _ASP.NET Web API_ and how the architectural pieces fit together. This is essential knowledge, but not the primary objective of this book. This book is about how to build _evolvable_ Web APIs. This chapter is the where we begin to talk about how to create a Web API that can evolve over period of years. A long enough period of time that business concerns will change and technology will change.
Rather than discuss the issues in abstract scenarios, we decided that we must walk the walk and build an API that demonstrates the concepts we wish to convey. This API will concern a domain that should be familar to every developer and we believe is sufficiently realistic that it could be adpoted in real world scenarios.
Before delving into the details of the domain, we will ensure that evolvability is something we
public class RequestPreprocessor : DelegatingHandler {
public RequestPreprocessor(HttpMessageHandler handler) {
this.InnerHandler = handler;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
request.Content.LoadIntoBufferAsync().RunSynchronously();
// Do your stuff!
public class UnitTests {
[Fact]
public void RoundTripUserAgent() {
var request = new HttpRequestMessage();
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("foo", "1.2"));
var userAgent = request.Headers.UserAgent.ToString();
var strongUserAgent = ProductInfoHeaderValue.Parse(userAgent);
Assert.Equal("foo", strongUserAgent.Product.Name);
//Web API using WCF to Self-host
//Time to create init Server and Start HttpListener: 0
//Time for roundtrip: 302 body = {"Foo":"Bar"}
//Time for 2nd roundtrip (new WebRequest): 0 Body = {"Foo":"Bar"}
//Servicestack
//Time to create init AppHost and Start HttpListener: 129
//Time for roundtrip: 176 body = {"Foo":"Bar"}
//Time for 2nd roundtrip (new WebRequest): 0 Body = {"Foo":"Bar"}
@darrelmiller
darrelmiller / gist:3698325
Created September 11, 2012 13:07
Some Web API techniques
using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace WebApiSelfHost
{
class Program
{
@darrelmiller
darrelmiller / gist:3698177
Created September 11, 2012 12:47
A replacement for the old HttpResponseMessage<T>
public class HttpResponseMessage<T> : HttpResponseMessage
{
public HttpResponseMessage(HttpRequestMessage request, T value)
{
var config = request.GetConfiguration();
var contentNegotiator = config.Services.GetContentNegotiator();
var connegResult = contentNegotiator.Negotiate(
typeof(T), request, config.Formatters
);
request.CreateResponse(HttpStatusCode.Accepted, value);
@darrelmiller
darrelmiller / gist:3629815
Created September 5, 2012 03:21
Alternative handler
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<HttpResponseMessage>();
Interlocked.Increment(ref count);
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("HELLO WORLD")
};