Skip to content

Instantly share code, notes, and snippets.

@vendettamit
vendettamit / WebHttpBehaviorExtension.cs
Created October 8, 2015 22:44
The class contains a solution to use Types signature of methods in WebHttpBinding in WCF.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Web;
/// <summary>
/// Custom function to perform looping operation.
/// </summary>
/// <typeparam name="T">type of ienumerable</typeparam>
/// <param name="list">The list.</param>
/// <param name="action">The action.</param>
/// <returns>index value</returns>
/// <exception cref="System.ArgumentNullException"> if the supplied action is null</exception>
public static int ForEach<T>(this IEnumerable<T> list, Action<int, T> action)
{
@vendettamit
vendettamit / reflection.cs
Created May 24, 2013 13:34
Reflection miscelleneous
/// <summary>
/// Gets the error message attribute.
/// </summary>
/// <param name="memberName">Name of the member.</param>
/// <returns>
/// value of attribute from class
/// </returns>
public static string GetErrorMessageAttributeValue(string memberName)
{
string message = string.Empty;
@vendettamit
vendettamit / EventHandlerFetcher.cs
Created June 6, 2013 14:33
Check if the event is hooked with the Handler
public static void Usage()
{
var instance = new Timer();
// check if the event is assigned a handler
if (GetEventHandler(instance, "Elapsed", "ElapsedEventHandler") == null)
{
throw new InvalidOperationException(string.Format("The Timer elapsed event must be assigned an event handler. Source: {0}", type.FullName));
}
1. Stub() defines the behavior for stubbed object.
2. Expect() defines the behavior and the expectation for mocked object.
3. Follow the principle of "Test only one thing per test".
4. mockrepositoryObject doesn't stop recording until you call ReplayAll. Try using repositoryInstance.PlayBack();
sample:
using (repository.Playback())
{
}
5. Call repository.VerfiyAll() in the end.
@vendettamit
vendettamit / gist:5774450
Created June 13, 2013 15:07
Http client Downloader for Retry(Palmer lib)/Resume download requests enabled.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
@vendettamit
vendettamit / AsyncMethodTemplate.cs
Created June 13, 2013 15:13
Async delegate template using tasks
/// <summary>
/// DoAsync is a method template to perform any task asynchronously.
/// </summary>
/// <typeparam name="T">type of argument</typeparam>
/// <param name="action">method</param>
/// <param name="argument">value of argument of type T</param>
public static void DoAsync<T>(Action<T> action, T argument)
{
// Invoke the task to execute the item
Task.Factory.StartNew(() => action.Invoke(argument),
@vendettamit
vendettamit / regex collections.cs
Created June 17, 2013 15:30
My Regex Collections
Testing websites:
http://www.regex101.com/r/yM3vL0
http://www.regexplanet.com/advanced/dotnet/index.html
UrlSourceOrLocalDriveWithRegex = @"(?i)\b^(https?|s?ftp)+:\/\/[aA-zZ]*[0-9]*|([aA-zZ]{1}:{1}\\{1}\b).*$";
Matches: Https://www.something.com, Ftp://somesource.com, sftp://securesource.com, http://moreurl.com, C:\path etc.
Unmatch: httpss://something.com, abcd://, c:, C,
SystemDriveOrUNCPathRegex = @"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))+$"
Matches: c:\folder, \\server\folder
public static class HtmlHelperExtensions
{
private static readonly char Separator = '/';
private static readonly string TemplateFolder = HttpContext.Current.Server.MapPath("~/App/templates");
private static readonly IViewEngine ViewEngine = new HandlebarsRazorViewEngine();
public static MvcHtmlString RenderEmber(this HtmlHelper helper, string path = "", bool noTemplateName = false)
{
if (HttpRuntime.Cache[path] == null)
{
@vendettamit
vendettamit / Create T object and invoking method.cs
Created September 24, 2013 15:45
Creating a generic object via reflection and invoking a method on it.
/// <summary>
/// The deserialize to generic list.
/// </summary>
/// <param name="jsonString">
/// The json string.
/// </param>
/// <typeparam name="TReturnType">Type of a list of type TItemType
/// </typeparam>
/// <typeparam name="TItemType">Type of generic argument to list
/// </typeparam>