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 router = new ApiRouter("api").DispatchTo<RootController>() | |
.Add(new ApiRouter("Contacts").DispatchTo<ContactsController>()) | |
.Add(new ApiRouter("Contact").WithHandler(new LoggingHandler()) | |
.Add(new ApiRouter("{contactid}") | |
.WithConstraint("contactid", @"\d+") | |
.DispatchTo<ContactController>() | |
.Add(new ApiRouter("Address") | |
.Add(new ApiRouter("{addressid}") |
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
GET POST /gists | |
GET /gists/public | |
GET /gists/starred | |
GET PATCH DELETE /gists/:id | |
GET /gists/:gist_id/comments | |
PUT /gists/:id/star | |
POST /gists/:id/fork | |
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
public interface IHttpService<T> { | |
HttpResponseMessage<T> Get(HttpRequestMessage<T> request); | |
HttpResponseMessage<T> Put(HttpRequestMessage<T> request); | |
HttpResponseMessage<T> Post(HttpRequestMessage<T> request); | |
HttpResponseMessage<T> Delete(HttpRequestMessage<T> request); | |
} |
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
<resource href="/ErrorLog/2323234"> | |
<resource rel="http://example.com/probs/out-of-credit" ref="/ErrorLog/2323234/details"> | |
<title>"You do not have enough credits."</title> | |
<detail>"Your current balance is 30, but that costs 50."</title> | |
<balance>30</balance> | |
<account>http://api.example.com/account/12345</account> | |
</resource> | |
</resource> |
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
// Server | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; |
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
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") | |
}; |
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
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); |
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; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using System.Web.Http.SelfHost; | |
namespace WebApiSelfHost | |
{ | |
class Program | |
{ |
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
//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"} |
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
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); |