Skip to content

Instantly share code, notes, and snippets.

public class DisableMultipleQueuedItemsFilter : JobFilterAttribute, IClientFilter, IServerFilter
{
private static readonly TimeSpan LockTimeout = TimeSpan.FromSeconds(5);
private static readonly TimeSpan FingerprintTimeout = TimeSpan.FromHours(1);
public void OnCreating(CreatingContext filterContext)
{
if (!AddFingerprintIfNotExists(filterContext.Connection, filterContext.Job))
{
filterContext.Canceled = true;
@benhysell
benhysell / AdamAnalysisJob.cs
Created November 25, 2015 14:28
TopShelf Service with Hangfire, Enterprise Library IOC, and SignalR reporting back to a website using Redis
namespace V.AdamAnalysis.BusinessLogic.Jobs
{
public class AdamAnalysisJob
{
//none of the real work is shown here...the interesting parts are the constructors that take in objects to be used
//and the journal mechansim to report progress back to the website.
private AdamAnalysisManager adamAnalysisManager;
private IHubContext hubContext;
@benhysell
benhysell / HangfireService.cs
Created October 23, 2015 18:30
TopShelf - Create a Windows Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using V.RemoteDecodeProcessConsole.Properties;
@benhysell
benhysell / retry.cs
Created October 23, 2015 18:27
Polly - Back Off and Retry on Error http client
//Retry http with a back off on failure
await Policy.Handle<HttpRequestException>().WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.ExecuteAsync(async () =>
{
var result = await httpClient.GetAsync("api/CallToServer/" + varibleToSendToServer, cancellationToken);
if (result.IsSuccessStatusCode)
{
returnValue = await result.Content.ReadAsAsync<List<Results>>(token);
}
@benhysell
benhysell / WebApiPostWithProgress.cs
Last active February 13, 2024 16:51
c# web api post with progress reporting
//in setup define client and progress reporter
var httpProgressHandler = new ProgressMessageHandler();
httpProgressHandler.InnerHandler = new HttpClientHandler();
var client = new HttpClient(httpProgressHandler) { BaseAddress = new Uri(Settings.Default.ServerUrl) };
//register to use elsewhere in the application, note it is better to resuse for lifetime of application than create for every call
Mvx.RegisterSingleton(client);
Mvx.RegisterSingleton(httpProgressHandler);
@benhysell
benhysell / BaseRepository.cs
Created July 6, 2015 14:40
c# Entity Framework Query With Cancellation
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@benhysell
benhysell / Upload.cs
Created July 2, 2015 14:06
c# httpClient webapi post user created class object with zip file
//build zip file
using (var zip = new ZipFile(zipFilePath))
{
zip.AddDirectory("Zip files go here");
zip.Save(zipFilePath);
}
//create upload package
var uploadData = new NewObjectThatGoesAlongWithTheFile(SomeDataToCreateObject);
uploadData.ResultFileMd5 = CalculateMd5(zipFilePath); //calculate an md5 to compare on the other side
@mkropat
mkropat / ui-router-logging.js
Last active July 31, 2016 19:38
Automatic trace logging of Angular.js events and ui-router state transitions
angular.module('your-module').config(['$provide', function ($provide) {
$provide.decorator('$rootScope', ['$delegate', function ($delegate) {
wrapMethod($delegate, '$broadcast', function (method, args) {
if (isNonSystemEvent(args[0]))
logCall('$broadcast', args);
return method.apply(this, args);
});
wrapMethod($delegate, '$emit', function (method, args) {
@bmancini55
bmancini55 / CookieTempDataProvider.cs
Created July 24, 2013 18:35
ASP.NET MVC CookieTempDataProvider
public class CookieTempDataProvider : ITempDataProvider
{
// Change cookie name to whatever you want it to be
const string TempDataCookieKey = "__TempData";
HttpContextBase httpContext;
#region Constructors
public CookieTempDataProvider(HttpContextBase httpContext)
{
@afsharm
afsharm / gist:5660844
Last active December 8, 2021 17:22
How to add a simple file upload functionality to CKEditor in ASP.NET MVC. Use CKEditor as CKEDITOR.replace('Description', { filebrowserImageUploadUrl: '/ControllerName/UploadImage' }); It is an updated version of http://arturito.net/2010/11/03/file-and-image-upload-with-asp-net-mvc2-with-ckeditor-wysiwyg-rich-text-editor/ For more information see
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode)
{
//http://stackoverflow.com/a/4088194/167670
//http://arturito.net/2010/11/03/file-and-image-upload-with-asp-net-mvc2-with-ckeditor-wysiwyg-rich-text-editor/
//http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
if (upload.ContentLength <= 0)
return null;