Skip to content

Instantly share code, notes, and snippets.

View samoshkin's full-sized avatar

Alexey Samoshkin samoshkin

View GitHub Profile
@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
@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: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:1422969
Created December 2, 2011 11:48
Find common root node for given nodes in a tree
public class TreeAnalyzer
{
public Node FindCommonRoot(IEnumerable<Node> nodes)
{
var walkedNodes = new HashSet<Node>();
var branches = nodes.ToList();
int nextIterationBranchRangeStartIndex = 0;
do
{
for (int i = nextIterationBranchRangeStartIndex; i < branches.Count; i++)
@samoshkin
samoshkin / gist:1446254
Created December 8, 2011 06:08
tdd-time-to-live-class-tests
[TestFixture]
public class TimeToLiveTests
{
[Test]
public void never_expiring_ttl_should_not_expire_at_relative_time()
{
TimeToLive.CreateNeverExpiring().ExpiresAtRelativeTime.Should().BeFalse();
}
[Test]
@samoshkin
samoshkin / JS and jQuery learning resources
Created February 16, 2012 22:24
JS and jQuery learning resources
JavaScript learning resources.
Starting point for those, who have already learned JS, but forgotten. Good article. Quite compact.
https://developer.mozilla.org/en/JavaScript/A_re-introduction_to_JavaScript
Question: what's the best learning source on JS. Some answers.
http://stackoverflow.com/questions/11246/best-resources-to-learn-javascript
http://stackoverflow.com/questions/2687566/learning-javascript-in-one-weekend
http://net.tutsplus.com/tutorials/javascript-ajax/the-best-way-to-learn-javascript/
@samoshkin
samoshkin / gist:2704886
Created May 15, 2012 20:29
Creating method overloads in JS using Function.length
function addMethod(object, name, fn){
// Save a reference to the old method
var old = object[ name ];
// Overwrite the method with our new one
object[ name ] = function(){
// Check the number of incoming arguments,
// compared to our overloaded function
if ( fn.length == arguments.length )
// If there was a match, run the function
@samoshkin
samoshkin / prototype_as_class.js
Created December 27, 2012 01:01
Prototype as class in Javascript.
// type declaration
var A = makeType({
init: function(x, y){
this.x = x;
this.y = y;
},
hello: function(){
console.log(this.x, this.y)
}
@samoshkin
samoshkin / constructor_function_as_class.js
Created December 27, 2012 01:03
Constructor function as class in JavaScript.
// type declaration
var A = makeType({
init: function(x, y){
this.x = x;
this.y = y;
},
hello: function(){
console.log(this.x, this.y)
}
// inspired by this http://stackoverflow.com/a/12506613
// + emit SIGINT, and handle "exit" event
var stdin = process.stdin;
// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)