PayPal Transaction Search API: PERMISSION_DENIED, No Permission for the requested operation

You need the scope .. if it’s not there in the oauth2 response, double check your REST App’s permissions. Refreshing an access token Existing access tokens are cached for 9 hours–so if you already requested an API token and then just added this permission to your app, it can take up to 9 hours … Read more

ASP .NET Core default language is always English

You are setting “arabic” as DefaultRequestCulture but DefaultRequestCulture is used if none of the built-in providers can determine the request culture. The default providers are: QueryStringRequestCultureProvider CookieRequestCultureProvider AcceptLanguageHeaderRequestCultureProvider Most likely the culture is determined from the Accept-Language HTTP header that the browser is sending. You have to remove the AcceptLanguageHeaderRequestCultureProvider in order to fallback to … Read more

Store / Retrieve ConnectionString from appSettings.json in Core 2 MVC app

Define your connection string(s) in appsettings.json { “connectionStrings”: { “appDbConnection”: “…” } } Read its value on Startup If you follow the convention and define your connection string(s) under connectionStrings, you can use the extension method GetConnectionString() to read its value. public class Startup { public IConfiguration Configuration { get; private set; } public Startup(IConfiguration … Read more

How to inject a reference to a specific IHostedService implementation?

Turns out there’s an easy way to do this (thanks for the pointer, Steven). If you need to be able to inject / get a reference to some service, go ahead and register the service normally (without worrying about any IHostedService stuff): services.AddSingleton<ServiceBusListener>(); Now we can register a separate hosted service whose only responsibility is … Read more

How to start Quartz in ASP.NET Core?

TL;DR (full answer can be found below) Assumed tooling: Visual Studio 2017 RTM, .NET Core 1.1, .NET Core SDK 1.0, SQL Server Express 2016 LocalDB. In web application .csproj: <Project Sdk=”Microsoft.NET.Sdk.Web”> <!– …. existing contents …. –> <!– add the following ItemGroup element, it adds required packages –> <ItemGroup> <PackageReference Include=”Quartz” Version=”3.0.0-alpha2″ /> <PackageReference Include=”Quartz.Serialization.Json” … Read more

Modify middleware response

.NET Core 3+ solution with proper resource handling: Replace response stream by MemoryStream to prevent its sending. Return the original stream after the response is modified: public async Task Invoke(HttpContext context) { var response = context.Response; //uncomment this line to re-read context.Request.Body stream //context.Request.EnableBuffering(); var originBody = response.Body; using var newBody = new MemoryStream(); response.Body … Read more

Read request body twice

If you’re using application/x-www-form-urlencoded or multipart/form-data, you can safely call context.Request.ReadFormAsync() multiple times as it returns a cached instance on subsequent calls. If you’re using a different content type, you’ll have to manually buffer the request and replace the request body by a rewindable stream like MemoryStream. Here’s how you could do using an inline … Read more

Create Custom HTML Helper in ASP.Net Core

For me I thought my HTML helpers weren’t working until I spotted that the extension method is now on IHtmlHelper not HtmlHelper. So for .net core: public static IHtmlContent CheckboxListFor<TModel>(this IHtmlHelper<TModel> html, Expression<Func<TModel, List<CheckboxListItem>>> expression) … Instead of for .net: public static HtmlString CheckboxListFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, List<CheckboxListItem>>> expression) … EDIT: I’ve also updated the … Read more