Skip to content

Instantly share code, notes, and snippets.

View samoshkin's full-sized avatar

Alexey Samoshkin samoshkin

View GitHub Profile
@samoshkin
samoshkin / gist:1334279
Created November 2, 2011 17:21
Call context with child threads
CallContext.SetData("key", new Number { N = 1 });
var task1 = Task.Factory.StartNew(() =>
{
var n = CallContext.GetData("key") as Number;
n.N++;
Thread.Sleep(1000);
});
var task2 = Task.Factory.StartNew(() =>
{
@samoshkin
samoshkin / gist:1333273
Created November 2, 2011 09:43
Adding operations to WCF service on runtime
public static OperationDescription[] AddHelpOperations(ContractDescription contractDescription, DispatchRuntime dispatchRuntime)
{
Fx.Assert(contractDescription != null, "The 'contractDescription' parameter should not be null.");
Fx.Assert(dispatchRuntime != null, "The 'dispatchRuntime' parameter should not be null.");
Uri baseAddress = dispatchRuntime.EndpointDispatcher.EndpointAddress.Uri;
HelpPage helpPage = new HelpPage(baseAddress, null, contractDescription);
HttpOperationDescription helpPageOperation = new HttpOperationDescription(HelpPage.HelpMethodName, contractDescription);
helpPageOperation.Behaviors.Add(new WebGetAttribute() { UriTemplate = HelpPage.OperationListHelpPageUriTemplate });
@samoshkin
samoshkin / gist:1329102
Created October 31, 2011 21:50
WCF-Web-API/SendAsync-nightmare
protected internal sealed override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request", SR.net_http_handler_norequest);
}
// ProcessRequest() and ProcessResponse() are supposed to be fast, so we call ProcessRequest() on the same
// thread SendAsync() was invoked to avoid context switches. However, if ProcessRequest() throws, we have