Skip to content

Instantly share code, notes, and snippets.

View mishrsud's full-sized avatar
🎯
Focusing

Sudhanshu Mishra mishrsud

🎯
Focusing
View GitHub Profile
@mishrsud
mishrsud / GetExecutingDirectoryName.cs
Last active March 10, 2016 02:49
Get Directory of Executing assembly
/// Use most of the time
public static string GetExecutingDirectoryName()
{
var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
return new FileInfo(location.AbsolutePath).Directory.FullName;
}
/// In case of tests or when we know what we're doing
/// This works in ASP.NET Web API
public static string GetExecutingDirectoryNameForTests()
@mishrsud
mishrsud / GetEmbeddedResource.cs
Last active August 29, 2015 14:16
Gets an Embedded Resource (text) from currently executing assembly
/// <summary>
/// Reads text that is embedded into an assembly
/// </summary>
/// <remarks>
/// fullyQualifiedFileName is fully qualified, e.g. if the file abc.config was in assembly with namespace MyCompany.MyLib inside a
/// folder Config then fullyQualifiedFileName should read MyCompany.MyLib.abc.config
/// </remarks>
private static string GetTextFromEmbededResource(string fullyQualifiedFileName)
{
var assembly = Assembly.GetExecutingAssembly();
@mishrsud
mishrsud / trycatchtran.sql
Last active December 20, 2022 19:00
SQL TRY-CATCH WITH TRANSACTION
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[pr_ins_test]
@CompanyID INT
AS
SET NOCOUNT ON
@mishrsud
mishrsud / GetIisVersion.cs
Last active August 29, 2015 14:25
Reads IIS Version of the registry
/// <summary>
/// Gets the IIS version from registry.
/// </summary>
/// <remarks>
/// From SO: http://stackoverflow.com/a/1699947/190476
/// </remarks>
public Version GetIisVersion()
{
using (RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp", false))
{
@mishrsud
mishrsud / HttpRequestMessageHelper.cs
Last active August 29, 2015 14:25 — forked from MikeJansen/HttpRequestMessageHelper.cs
HttpRequestMessage.GetClientIpAddress for ASP.NET MVC 4 / Web API
// Original idea: http://stackoverflow.com/questions/9565889/get-the-ip-address-of-the-remote-host
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
namespace CrowSoftware.Api
{
public static class HttpRequestMessageHelper
{
void Main()
{
var fileName = @"C:\OfficeDocs\somfile.js";
var hash = GetFileHash(fileName);
hash.Dump("Hash");
}
// Define other methods and classes here
public static string GetFileHash(string filePath)
{
/// <summary>
/// Gets the first header value or default.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestMessage">The <see cref="HttpRequestMessage"/> message.</param>
/// <param name="headerKey">The header key i.e. the header to read.</param>
/// <param name="defaultValueSelector">The <see cref="Func{T, TResult}"/> default value selector.</param>
/// <param name="valueTransform">The <see cref="Func{T, TResult}"/> value transform.</param>
/// <returns></returns>
private T GetFirstHeaderValueOrDefault<T>(
# Initialize repo
git init
# add everything (except ignored files)
git add .
# add a remote origin
git remote add origin https://{url}
# push to remote repo - push to the master
@mishrsud
mishrsud / adlog.snippet
Created March 28, 2016 06:19
Add NLog Logger
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Add NLog Logger</Title>
<Shortcut>adlog</Shortcut>
<Description>Code snippet for adding NLog Logger</Description>
<Author>Sudhanshu Mishra</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
// METHOD 1 No receive endpoint (this is meant for Publish in a pub-sub scenario)
public IBusControl CreateBusControl()
{
var bus = Bus.Factory.CreateUsingRabbitMq(configurator =>
{
IRabbitMqHost rabbitMqHost = configurator.Host(new Uri("rabbitmq://localhost/test"), hostConfigurator =>
{
hostConfigurator.Username("guest");
hostConfigurator.Password("guest");
});