Skip to content

Instantly share code, notes, and snippets.

View sdurandeu's full-sized avatar
💭
In London

Sebastian Durandeu sdurandeu

💭
In London
View GitHub Profile
@sdurandeu
sdurandeu / Program.cs
Last active June 30, 2017 15:07
.NET Core: Get Embedded File in Assembly
// NOTE: if the file is embedded in the executing assembly, this code can be simplified
// NOTE: this works even if the file is not embedded in the executing assembly
private async Task<string> GetEmbeddedFile(string fileName)
{
// NOTE: this code is not great but allows us to have the stored procedure js in the TryAzureCdn.Data project
// Resources (.resx file) are not working with embedded files
var assembly = typeof(DocumentDBRepository).GetTypeInfo().Assembly;
var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(m => m.Contains(fileName));
Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
string fileContents = string.Empty;
@sdurandeu
sdurandeu / DocumentDbRepository.cs
Last active June 30, 2017 15:04
Cosmos DB: Create Database, Collection and Stored Procedure
public async Task CreateDatabaseIfNotExistsAsync()
{
var databaseLink = UriFactory.CreateDatabaseUri(this.databaseSettings.DatabaseId);
// create database
await this.databaseClient.CreateDatabaseIfNotExistsAsync(new Database { Id = this.databaseSettings.DatabaseId });
// create collection
var collectionDefinition = new DocumentCollection { Id = this.databaseSettings.CollectionId };
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
@sdurandeu
sdurandeu / gist:3721ea454d4994e2ad4ba45804b62165
Created June 29, 2017 15:12
Deploy Web Job using MsBuild
msbuild MySite.csproj
/p:Configuration=Release
/p:DeployOnBuild=true
/p:DeployTarget=MsDeployPublish
/p:MsDeployServiceUrl=my-site.scm.azurewebsites.net:443
/p:UserName=$my-site
/p:Password=XXX
/p:DeployIisAppPath=my-site
/p:SkipExtraFilesOnServer=true
@sdurandeu
sdurandeu / gist:2971d83d194203ac571f6c25cd2ff2fa
Created April 26, 2016 19:34
Validate credit card expiration with moment.js
// this code assumes a credit card is valid in the current month
var validateDate = function () {
var creditCardDate = moment($scope.data.expirationYear+$scope.data.expirationMonth, "YYMM");
var today = moment();
return creditCardDate.isValid() && (today < creditCardDate.add(1, 'months'));
}
@sdurandeu
sdurandeu / index.html
Last active April 16, 2016 19:08
Use SVGs
<!-- Definition -->
<svg style="display: none" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="bank-icon">
<path d="M512 480v-32h-32v-192h32v-32h-96v32h32v192h-96v-192h32v-32h-96v32h32v192h-96v-192h32v-32h-96v32h32v192h-96v-192h32v-32h-96v32h32v192h-32v32h-32v32h544v-32h-32z"></path>
<path d="M256 0h32l256 160v32h-544v-32l256-160z"></path>
</symbol>
</svg>
<!-- Usage -->
<div class="col col-20">
@sdurandeu
sdurandeu / gist:76bb51627e4fd0ea5d37
Created March 4, 2016 21:08
Get current domain from request
private string GetDomainFromRequest()
{
return this.Request.Url.Scheme + Uri.SchemeDelimiter + this.Request.Url.Host;
}
@sdurandeu
sdurandeu / gist:7b47aa632cc90ee09089
Created March 4, 2016 15:31
Unit Test: check attributes of controllers
private static readonly string[] IgnoreMethods =
{
"Web.Controllers.CaseStudiesController.CaseStudiesApi",
"Web.Controllers.DocumentationController.DocArticlesApi",
"Web.Controllers.VideosController.AzureFridayResults",
"Web.Controllers.ChannelCalculatorController.PostEstimate",
"Web.Controllers.ChannelCalculatorController.GetExcelExportedFileFromCalculator"
};
[TestMethod]
@sdurandeu
sdurandeu / gist:80b286cd0c3971576d39
Last active June 13, 2022 13:55
MsBuild: Add custom files in web deploy package
<Target Name="AdditionalFilesForPackage" AfterTargets="CopyAllFilesToSingleFolderForPackage;CopyAllFilesToSingleFolderForMsDeploy">
<ItemGroup>
<AppDataFiles Include="$(MSBuildProjectDirectory)\App_Data\**\*" />
</ItemGroup>
<Copy SourceFiles="@(AppDataFiles)" DestinationFiles="@(AppDataFiles->'$(_PackageTempDir)\App_Data\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
@sdurandeu
sdurandeu / gist:281849d26705c30fc323
Last active December 20, 2015 20:51
Open a File for Reading
using System.IO;
using (var streamReader = File.OpenText("myfile.txt"))
{
string line = streamReader.ReadLine();
while (line != null)
{
Console.WriteLine(line);
line = streamReader.ReadLine();
}
@sdurandeu
sdurandeu / gist:e9f88bb5c4d12e538ff9
Created November 30, 2015 11:21
Reverse a string
public static string Reverse( string s )
{
char[] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string( charArray );
}