Skip to content

Instantly share code, notes, and snippets.

View wsky's full-sized avatar
🏠
Working from home

Harry wsky

🏠
Working from home
View GitHub Profile
@wsky
wsky / gist:1815043
Created February 13, 2012 08:32
clr event
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var f = new EventFactory();
f.Raised += (o, e) => { Thread.Sleep(100); Trace.WriteLine(1, Thread.CurrentThread.Name); };
f.Raised += (o, e) => { Thread.Sleep(200); Trace.WriteLine(2, Thread.CurrentThread.Name); };
f.Raised += (o, e) => { Thread.Sleep(300); Trace.WriteLine(3, Thread.CurrentThread.Name); };
@wsky
wsky / AbstractComponentActivator.cs
Created July 18, 2012 15:40
How to implement Castle.MicroKernel.ComponentActivator.AbstractComponentActivator
public virtual object Create(CreationContext context, Burden burden)
{
var configuration = Model.ExtendedProperties[Constants.SessionFactoryConfiguration] as Configuration;
var f= configuration.BuildSessionFactory();
burden.SetRootInstance(f);//new for 3.x
//onCreation(model, instance);
return f;
}
@wsky
wsky / CookieAwareWebClient.cs
Created July 28, 2012 07:25
CookieAwareWebClient
/// <summary>
/// 支持cookie的webclient
/// <remarks>
/// 请求完成后会自动将响应cookie填充至CookieContainer
/// </remarks>
/// </summary>
internal class CookieAwareWebClient : WebClient
{
internal CookieContainer _cookieContainer = new CookieContainer();
@wsky
wsky / Application_Error.cs
Created July 30, 2012 03:05
Application_Error Ajax
var header = context.Request.Headers["X-Requested-With"];
if (!string.IsNullOrEmpty(header) && header.ToLower().IndexOf("xmlhttp") >= 0)
{
Server.ClearError();
Response.StatusCode = 400;
Response.Clear();
Response.Write(exception.Message);
Response.End();
return;
}
@wsky
wsky / branch.sh
Last active November 12, 2015 02:05
usual git cmd and svn
# https://github.com/Kunena/Kunena-2.0/wiki/Create-a-new-branch-with-git-and-manage-branches
git branch net20
git push origin net20
git checkout net20
git branch -d [branch]
git push origin --delete [branch]
@wsky
wsky / gist:3205844
Created July 30, 2012 09:26
nhibernate sqlce config
<item key="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</item>
<item key="dialect">NHibernate.Dialect.MsSqlCeDialect</item>
<!--create|create-drop-->
<item key="hbm2ddl.auto">create-drop</item>
//解决部分服务器证书问题
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });
//以下可选,强制协议
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.Expect100Continue = true;
@wsky
wsky / gist:3225045
Created August 1, 2012 08:34
IsXmlHttpRequest
public static bool IsXmlHttpRequest(this HttpRequestBase request)
{
var header = request.Headers["X-Requested-With"];
return !string.IsNullOrEmpty(header) && header.ToLower().IndexOf("xmlhttp") >= 0;
}
@wsky
wsky / gist:3336510
Created August 13, 2012 02:46
isMobileRegex
Regex isMobileRegex = new Regex(
@"(iemobile|iphone|ipod|android|nokia|sonyericsson|blackberry|samsung|sec\-|windows ce|motorola|mot\-|up.b|midp\-)"
,RegexOptions.IgnoreCase | RegexOptions.Compiled);
bool isMobileDevice = !string.IsNullOrEmpty(Request.UserAgent) && isMobileRegex.IsMatch(Request.UserAgent);
@wsky
wsky / gist:3521554
Created August 30, 2012 01:47
checksum MD5
using (var md5 = new MD5CryptoServiceProvider())
{
var retVal = md5.ComputeHash(file);
var sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
sb.Append(retVal[i].ToString("x2"));
return Json(sb.ToString());
}