Skip to content

Instantly share code, notes, and snippets.

@pmhsfelix
pmhsfelix / wcfasync.cs
Created December 7, 2010 01:06
Service-side WCF asynchronous operation using Task<T>
// Note the OperationHasAsyncVersion attribute
[ServiceContract(Name = "IService")]
interface IService
{
[OperationContract]
[OperationHasAsyncVersion]
string Echo(string req);
Task<string> EchoAsync(string req);
}
@pmhsfelix
pmhsfelix / gist:732630
Created December 7, 2010 23:26
Accessing the request input stream ...
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Drafts
{
@pmhsfelix
pmhsfelix / gist:740236
Created December 14, 2010 10:17
Example C# code for generating an HTML table
"html"._(
"body"._(
"table"._(
"tr"._(
from colName in _colNames
select "th"._(colName)),
from e in _seq
select "tr"._(
from f in _colFuncs
select "td"._( f(e))))))
@pmhsfelix
pmhsfelix / gist:842495
Created February 24, 2011 17:25
Experimentations with WCF HTTP configuration DSLs (take 2)
static HttpHostConfiguration GetConfig2()
{
var provider = new ProcessorProviderOf<TheService>()
.RemoveDefaultMediaTypeProcessors()
.ForAllOperations()
.UseRequestProcessors(
(o,l,m) => new DataValidationProcessor(o),
(o,l,m) => new RequestLoggingProcessor(o)
)
.And()
@pmhsfelix
pmhsfelix / gist:980675
Created May 19, 2011 12:50
Using DbC preconditions: useful both as documentation and as integration checks
void epz_addMod(epz_ptr res, cepz_ptr op1, cepz_ptr op2, cepz_ptr mod){
ZREQUIRE1(STANDARD_RESIDUE(op1, mod) && STANDARD_RESIDUE(op2, mod));
ZREQUIRE0(POSITIVE(mod));
ZREQUIRE0(res != mod);
...
}
@pmhsfelix
pmhsfelix / gist:1133027
Created August 8, 2011 23:22
HTTP intermediary using WCF Web API message handlers
namespace WcfWebApi.Preview4.Explorations.MessageHandlers
{
class ProxyMessageHandler : DelegatingChannel
{
private readonly HttpClient _client = new HttpClient();
public ProxyMessageHandler(HttpMessageChannel innerChannel)
: base(innerChannel)
{
}
@pmhsfelix
pmhsfelix / gist:1140101
Created August 11, 2011 16:32
Operation URI resolution
[ServiceContract]
class TheResolverTestService
{
[WebGet(UriTemplate="op1/{prm1}/{prm2}")]
public void Oper1(string prm1, int prm2)
{
}
[WebGet(UriTemplate = "op2/{prm1}/middle/{prm2}")]
[OperationContract(Name="op2")]
namespace MultipleServicesForTheSameBinding
{
[ServiceContract]
interface IAServiceContract
{
[OperationContract]
string Get();
}
class AService : IAServiceContract{
[ServiceContract]
internal class TheService
{
[WebInvoke(UriTemplate = "*", Method = "*")]
public void TheOperation(Stream s)
{
byte[] buf = new byte[4*1024];
int blen;
int len = 0;
while ((blen = s.Read(buf, 0, buf.Length))>0)
@pmhsfelix
pmhsfelix / gist:3615356
Created September 4, 2012 00:45
Playing with claims and boolean operator overloading
[Fact]
public void Fact1()
{
var namedAlice = new UserNameClaim("Alice");
var teacher = new RoleClaim("Teacher");
var student = new RoleClaim("Student");
var alice = Claims.Identity(new UserNameClaim("Alice"), student);
var bob = Claims.Identity(new UserNameClaim("Bob"), teacher);
var anotherAlice = Claims.Identity(new UserNameClaim("Alice"));