Skip to content

Instantly share code, notes, and snippets.

@tomasjurasek
tomasjurasek / cs
Created August 9, 2022 08:23 — forked from gdvalishvili1/cs
Result C#
public class Result
{
public bool IsSuccess { get; }
public string Error { get; }
public bool IsFailure { get { return !IsSuccess; } }
protected Result(bool isSuccess, string error)
{
if (isSuccess && error != string.Empty)
throw new InvalidOperationException();
services.AddTransient<TService, TServiceImplementation>();
services.AddSingleton<Func<TService>>(s => s.GetRequiredService<TService>);
public class CustomGrantType : ICustomGrantValidator
{
public string GrantType => "wechat_credentials";
public Task<CustomGrantValidationResult> ValidateAsync(ValidatedTokenRequest request)
{
var wechatToken = request.Raw.Get("wechat_token");
var wechatUnionId = request.Raw.Get("wechat_unionId");
if (string.IsNullOrWhiteSpace(wechatToken))
@tomasjurasek
tomasjurasek / HostedService.cs
Created March 22, 2018 09:40 — forked from davidfowl/HostedService.cs
A base class that allows writing a long running background task in ASP.NET Core 2.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace WebApplication24
{
public abstract class HostedService : IHostedService
@tomasjurasek
tomasjurasek / HostedService.cs
Created March 22, 2018 09:40 — forked from davidfowl/HostedService.cs
A base class that allows writing a long running background task in ASP.NET Core 2.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace WebApplication24
{
public abstract class HostedService : IHostedService
public static string ToQueryString(this NameValueCollection nvc)
{
StringBuilder sb = new StringBuilder();
foreach (string key in nvc.Keys)
{
if (string.IsNullOrEmpty(key)) continue;
string[] values = nvc.GetValues(key);
if (values == null) continue;
<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="%(Content.Identity)"
DestinationFiles="%(Content.Link)"
SkipUnchangedFiles='true'
OverwriteReadOnlyFiles='true'
Condition="'%(Content.Link)' != ''" />
@tomasjurasek
tomasjurasek / reflection.cs
Created May 6, 2016 21:29
reflection class with list
class Program
{
static void Main(string[] args)
{
var person = new Person()
{
FirstName = "Tomas",
Surname = "Jurasek",
Persons = new List<Person>() { new Person { FirstName = "Test", Surname = "Test", Persons = null } }
};
@tomasjurasek
tomasjurasek / gist:aa7ec6889f15e2beb5806dd8b3c37b6b
Created March 31, 2016 20:52 — forked from jonathanmoore/gist:2640302
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@tomasjurasek
tomasjurasek / app.cs
Created March 11, 2016 12:45
caller member name
public static string WEBSITE_SLOT_NAME => GetAppSettingsValue();
private static string GetAppSettingsValue([CallerMemberName]string propertyName = null)
{
return WebConfigurationManager.AppSettings[propertyName];
}