Skip to content

Instantly share code, notes, and snippets.

@benerdin
benerdin / ReplicateStaticIndexes.cs
Last active December 14, 2015 02:49
Creates a Raven DocumentStore and creates static indexes on the primary and all secondary servers (static indexes are not replicated by design).
/// <summary>
/// Gets the document store.
/// </summary>
/// <returns>The document store.</returns>
private static IDocumentStore GetDocumentStore()
{
// Create the DocumentStore (expensive operation).
IDocumentStore documentStore = new DocumentStore
{
ConnectionStringName = "RavenDB",
@benerdin
benerdin / RavenDocumentStoreProvider.cs
Created March 14, 2013 13:27
This class attempts to instantiate a RavenDB DocumentStore when the "DocumentStore" getter is called. The DocumentStore is a thread-safe singleton that will only block other threads for a maximum of 30 seconds when the store is unavailable.
using NLog;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Exceptions;
using System;
using System.Threading.Tasks;
namespace Beyond.Services
{
/// <summary>
@benerdin
benerdin / FtpDataPublisher.cs
Last active December 16, 2015 20:28
.NET FTP Code- List files- Delete files- Upload a file
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace Brian.Federici
{
@benerdin
benerdin / UploadImage
Last active December 17, 2015 05:08
Demonstrates how to upload a file using a controller action in ASP.NET MVC.
/// <summary>
/// POST: /UploadImage
/// Saves an image to the server, resizing it appropriately.
/// </summary>
/// <returns>A JSON response.</returns>
[HttpPost]
public ActionResult UploadImage(int id)
{
// Validate we have a file being posted
if (Request.Files.Count == 0)
@benerdin
benerdin / nlog-config.xml
Created May 28, 2013 19:34
NLog Configuration
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="Assembly.Namespace" />
</extensions>
<targets>
<target xsi:type="Debugger" name="Debugger" />
<target xsi:type="Mail" name="Mailer" html="true" addNewLines="true" to="[email protected]" bcc="" cc="" from="&quot;Brian Federici&quot; &lt;[email protected]&gt;" smtpUserName="smtpUser" enableSsl="false" smtpPassword="smtpPassword" smtpAuthentication="Basic" smtpServer="192.168.1.100" smtpPort="25" />
<target xsi:type="File" name="file" layout="${longdate} ${logger} ${message}" fileName="${basedir}/${level}.log" />
</targets>
<rules>
@benerdin
benerdin / PropertyReflection.cs
Created May 30, 2013 14:20
Code snippet for reflecting on a class and appending properties to a StringBuilder. For System types, this will ignore appending properties (e.g. the Length property won't be appended for System.String).
private string GetModelDescription()
{
var sb = new StringBuilder();
var type = typeof(YourType);
sb.AppendFormat("{0} ({1})\n", type.Name, type.UnderlyingSystemType.ToString());
AppendPropertiesDescription(type, sb, " ");
return sb.ToString();
}
private void AppendPropertiesDescription(Type type, StringBuilder sb, string prefix)
@benerdin
benerdin / IISTaskManager.cs
Last active June 1, 2020 03:43
Static class that allows you to run a background task that IIS won't re-purpose until its execution is complete.
using NLog;
using System;
using System.Threading.Tasks;
using System.Web.Hosting;
namespace Web.Models
{
/// <summary>
/// Static class for running background tasks in IIS.
/// Any spawned threads are registered properly with IIS
@benerdin
benerdin / nant-foreach-xmlpoke.xml
Last active December 29, 2015 00:59
Example of how to use nAnt's <foreach> and <xmlpoke> tasks.
<!--
Given the following files in C:\
* app.1.config
* app.2.config
* app.3.config
* file.txt
This task will perform 3 iterations and set ${filename} to the following:
* C:\app.1.config
* C:\app.2.config
@benerdin
benerdin / DownloadAndResizeStream.cs
Last active January 1, 2016 18:59
Download and resize an image to a Bitmap using the ImageResizer nuget package. The resulting Bitmap is copied to a MemoryStream and returned.
/*
Make sure to wrap a using() around any calls to DownloadAndResizeStream:
using (var stream = DownloadAndResizeStream(uri, 100, 100, true)) {
// Do what you need to with "stream" here
}
*/
private Stream DownloadAndResizeStream(Uri sourceImageUrl, int width, int height, bool crop)
{
@benerdin
benerdin / CompressionExtensions.cs
Created January 14, 2014 22:27
Extension methods for compressing and decompressing a string using GZipStream.
/// <summary>
/// Encapsulates extension methods related to compression.
/// </summary>
public static class CompressionExtensions
{
/// <summary>
/// Compresses <paramref name="uncompressedString"/> and returns the result.
/// </summary>
/// <param name="uncompressedString">An uncompressed string.</param>
/// <returns>A base-64 string.</returns>