accessing httpclient service from within another service in .net 5
I am adding a httpclient as a service (within ConfigureServices(IServiceCollection services)) as follows:
services.AddHttpClient<RequestsClient>(c =>
{
c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler()
{
UseDefaultCredentials = true
};
});
the RequestsClient class is constructed as:
public RequestsClient (HttpClient client, IHttpContextAccessor hca, ILogger<RequestsClient > log, Configuration config)
to use the RequestsClient service in any class/component needing it i'm injecting it as:
[Inject]
protected RequestsClient requestsClient { get; set; }
all this works great.
I'm now in need of creating a second service, lets call it "TimedService". How can I use my RequestsClient service from within my second service, TimedService?
injecting it like i do with components won't work as the RequestsClient always is null. is there a way to give TimedService service access to my RequestsClient service?
I'm sorry if this is a stupid question, I'm fairly new to this
1 answer
-
answered 2021-01-19 10:53
Zsolt Bendes
You need to use constructor injection in
TimedService
.public class TimedService { private RequestsClient _requestClient; public TimedService(RequestsClient client) { _requestClient = client; } }
I would recommend to go into typed client version. It is a bit more clearer in separation of code wise. See the official docs how to do this.
See also questions close to this topic
-
Switching profiles AWS Secret Manager - .Net Core
I am trying to switch profiles in code using appsettings.json for a .net core web service. If I new up an instance of the client in code eg:
using var client = new AmazonSecretsManagerClient(); client.ListSecretsAsync(listRequest, default) .GetAwaiter() .GetResult();
this takes the default profile that was set up and works fine.
Instead of using the default profile, I want to control the profile from json file. I am trying to follow this:
https://docs.aws.amazon.com/sdk-for-net/latest/developer-guide/net-dg-config-netcore.html
Instead of newing up the client, I am using dependency injection
public SecretsService(IAmazonSecretsManager secretsManagerClient) { _secretsManagerClient = secretsManagerClient; }
Here is the config:
"AWS": { "Profile": "default", "Region": "us-east-2" }
When I make a call using,
_secretsManagerClient.ListSecretsAsync(listRequest, default) .GetAwaiter() .GetResult();
it gives an exception
No such host is known. (secretsmanager.defaultregion.amazonaws.com:443)
What am I missing?
-
Getting client IP from SignalR Connection
I am aware this question has been asked before. But in the ~8 years since, SignalR has changed rather a lot.
So does anyone know how to get the IP of a client from the SignalR hub?
I am using SignalR to communicate between two .net core applications on different servers, so there is no HTTP request or serving a website or anything like that.
-
.Net Core Plugin for an application, problem with dll missing
I migrated a plugin i made from .Net Framework to .Net Core and have some problem because some dll are not find by the application (it's not mine). It seems this application is made with .net Core too. If i place the missing dll in the same folder than my plugins, windows say it locked this dll for security reasons. I know there is a few months i had this problem and successed resolve it but i don't remember how. In my mind it was to add Drawing.common and add an itemgroup with it in my csproj file, but this time it refuses everything.
It's the first time i try to make a plugin with .net Core, i made some application, and this plugin works well if i launch it with a debug custom mode as an application. I ask to myself if it's not an error to think making a plugin with .net Core, before to back to .net Framework i wanted to ask if there was not a solution, according to the fact this plugin must work for each people, without make a big manipulation.
edit: To be clear, Dll denied are of course not those i did but those like system.Drawing.Common ...
part of csproj
<ItemGroup> <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" /> <PackageReference Include="System.Drawing.Common" Version="5.0.0" /> </ItemGroup>
-
How to know what data an android service expects when using binder?
I'm confused about how to determin what to write into the data parcel in
binder.transct(code, data, reply, flag)
. Is it predetermined by the service and the client side needs to follow the agreement? I'm now trying to work with a list of services. If it's determined by the service, how can I find out what a service expects fromdata
? Thanks! -
Inject service into service on Laravel 8 using reflection
I'm trying to inject a service (model repository) inside another service (when I place controller logic).
I would like to inject it with reflection, but I got an error:
class UserService { private $repository; public function __construct(UserRepository $repository) { $this->repository = $repository; }
Error:
Too few arguments to function App\Services\UserService::__construct(), 0 passed in .../app/Providers/UserServiceProvider.php on line 18 and exactly 1 expected
I found this way to inject, but I would prefer to do it through reflection:
private $repository; public function __construct() { $this->repository = app(UserRepository::class); }
There is some elegant way to do this better without using Service Container?
-
Process Data in foreground service
I am building an app in which I have 3 video filters.
And I have built the app to apply those filters to files in big video files (>500 MB) in a queue.
I am using an
AsyncTask
to do the video processing in the background.But using
AsyncTask
is not the best practice so what I want to do instead is to use a foreground service with a notification to do the processing in the background.These are the questions I have trouble solving:
- How to pass the path and filter to be applied on video for each video to the foreground service.
- Then Process the files in background.
- And save those files from service to a particular app directory.
Please let me know if my approach is wrong and how to proceed if am right.
I tried looking for examples of foreground services but I couldn't find any code samples written in Java (as I am most familiar with it).
https://github.com/JimSeker/service/tree/master/ForegroundServiceDemo
-
How to display data in a reusable Table component in Blazor
I'm trying to create a reusable MasterTable component in Blazor.
So far, I've defined the MasterTable as
@using AntDesign @using MyNamespace.Blazor.ViewModels @typeparam TItem <Table TItem="TItem" DataSource="@Data"> @{ foreach (var col in Columns) { <Column Title="@col.Label" @bind-Field="@col.Key" /> } } </Table> @code { private List<TItem> _data; [Parameter] public List<TItem> Data { get => _data; set => _data = value ?? new List<TItem>(); } [Parameter] public TableColumnViewModel[] Columns { get; set; } }
where TableColumnViewModel is defined simply as
public class TableColumnViewModel { public string Key { get; set; } public string Label { get; set; } }
I would like to create an instance of the MasterTable in a page for Daily Tasks but so far I'm only able to get it to display like this:
My attempt to implement MasterTable is as follows:
@page "/Tasks/Daily"; @using MyNamespace.Blazor.Services; @using MyNamespace.Blazor.ViewModels; @using MyNamespace.Api.Client.Model; @inject ITasksService _tasksService; <h1>Daily Tasks</h1> <MasterTable TItem="TaskStatus" Data="_tasks" Columns="cols"> </MasterTable> @code { private List<TaskStatus> _tasks = new List<TaskStatus>(); protected override async Task OnInitializedAsync() { _tasks = await _tasksService.GetTaskStatusAsync(); } TableColumnViewModel[] cols = { new TableColumnViewModel { Key = "id", Label = "ID" }, new TableColumnViewModel { Key = "description", Label = "ID" }, new c { Key = "type", Label = "Type" } }; }
With TaskStatus defined as
public class TaskStatus { public TaskStatus(int taskStatusId = default(int), string statusDescription = default(string)) { this.TaskStatusId = taskStatusId; this.StatusDescription = statusDescription; } public int TaskStatusId { get; set; } public string StatusDescription { get; set; } }
What do I need to do to get the MasterTable template to display the list of TaskStatus objects instead of the keys from TableColumnViewModel?
To be clear - instead of just using the component without wrapping it, the issue is that I want to isolate the CSS in the context of the 3rd party component, so that only the necessary CSS is loaded.
-
Blazor - app.UseIdentityServer(); with .pfx key file - Unexpected character encountered while parsing number
I have created a new Blazor WebAssembly App with Individual User Accounts, Store user accounts in-app and ASP.NET Core hosted in .NET 5. When deploying my app to Azure App Service I get the following error:
Object reference not set to an instance of an object.at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions
Reading these links I have to provide my own certificate in production for IdentityServer:
Blazor Web Assembly App .Net Core Hosted: publish runtime error
https://stackoverflow.com/a/56904000/3850405
I then created a
.pfx
file like this and I have verified that it works and my password is correct.https://stackoverflow.com/a/48790088/3850405
I then placed the
.pfx
file in myServer
projects root folder and markedCopy to Output Directory
asCopy Always
.I then updated
appsettings.json
to look like this:"IdentityServer": { "Clients": { "BlazorTest.Client": { "Profile": "IdentityServerSPA" } }, "Key": { "Type": "File", "FilePath": "localhost.pfx", "Password": "MySercurePassword123?" } },
Now the project does not work neither locally or on Azure. It fails on
app.UseIdentityServer();
inStartup.cs
with the following error:Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing number: �. Path '', line 1, position 1.'
According to Microsoft docs my certificate should be valid:
A production certificate to use for signing tokens.
- There are no specific requirements for this certificate; it can be a self-signed certificate or a certificate provisioned through a CA authority.
- It can be generated through standard tools like PowerShell or OpenSSL.
- It can be installed into the certificate store on the target machines or deployed as a .pfx file with a strong password.
If I load the key like this it works:
"Key": { "Type": "Store", "StoreName": "My", "StoreLocation": "CurrentUser", "Name": "CN=blazortest" }
-
Is there some how to navigate to a page and pass parameters without use the address bar in Blazor?
This is present in many modern SPA libraries/frameworks...
I will supply an example using React (But it could be Angular or Vue), you can do something like...
this.props.router.push({ pathname: '/login-successfully', state: { userId: 'john', name: 'John Doe } })
and then on the initialization of the "other-page" you will have:
const {state} = useLocation(); const { userId, name } = state;
and you can render things like
<p>Welcome Back, {name}!</p>
Such feature is very useful in many scenarios, but by reading the documentation of routing in Blazor at https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-5.0 I cannot find anything. The NavigationManager just have those parameters:
public void NavigateTo (string uri, bool forceLoad = false);
Is there some equivalent approach that I can use ? I know a workaround by creating a singleton class, store the data over there and display on the login-successfully page, but I really hope to find something better as solution.
-
What is the difference between request headers and content headers for HttpClient
Can anybody explain what is the difference between the request header and content header? In this particular case I'm talking about UWP HttpClient object. First you create HttpClient, then you create HttpRequestMessage and then you assign, in my case HttpStreamContent to the Content property of the HttpRequest message. There is Headers property on the HttpRequestMessage and there is Headers property on the HttpStreamContent. When should I use one or another? Where exactly the headers will appear in one or another case? Here is a code snippet to explain what I mean
using(var objProtocolFilter = new HttpBaseProtocolFilter()) { objProtocolFilter.AllowUI = false; objProtocolFilter.CacheControl.ReadBehavior = HttpCacheReadBehavior.NoCache; objProtocolFilter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache; using(var objClient = new HttpClient(objProtocolFilter)) { HttpMethod eMethod = Method switch { HttpUploadMethod.Post => HttpMethod.Post, HttpUploadMethod.Put => HttpMethod.Put, _ => throw new ValueOutOfRangeException(nameof(Method)) }; using(var objRequest = new HttpRequestMessage(eMethod, RemoteUri)) { _Headers.Cast<string>().Execute(item => objRequest.Headers.TryAppendWithoutValidation(item, _Headers[item])); objRequest.Content = new HttpStreamContent(objInputStream.AsInputStream()); _Headers.Cast<string>().Execute(item => objRequest.Content.Headers.TryAppendWithoutValidation(item, _Headers[item])); objRequest.Content.Headers.ContentLength = (ulong)objInputStream.Length;
Here I just add the same list of headers to HttpRequestMessage and to HttStreamContent. I guess it's wrong unless those objects are smart enough to apply only permitted headers in one or the other case. So, which headers should go where? Are they interchangeable?
-
HttpClient send XML PostAsync Malformed request: Premature end of file?
I am trying to send a xml to a server and it always throw an exception
Malformed request: Premature end of file..
. Using the Postman it works well but usingHttpClient
it doesn't, I think the problem is withheaders
what server needs isContent-Type", "application/xml
and I cannot do it worksHow could I fix it ?
trying
//HttpClient HttpClient hClient = new HttpClient(); hClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml"); //post var contentString = new StringContent(xml, Encoding.UTF8, "application/xml"); HttpResponseMessage response = hClient.PostAsync(URL_FINAL, contentString).GetAwaiter().GetResult(); var resultContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
xml
<?xml version="1.0" encoding="utf-16"?> <payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <mode>default</mode> <method>creditCard</method> <sender> <name>Meu Nome</name> <email>email@gmail.com</email> <phone> <areaCode>17</areaCode> <number>9999999999</number> </phone> <documents> <document> <type>CPF</type> <value>9999999999</value> </document> </documents> <hash>5e5240axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxea8a</hash> </sender> <currency>BRL</currency> <notificationURL>https://dominio.com/StatusPagamentoTransaction</notificationURL> <items> <item> <id>2</id> <description>produto</description> <quantity>2</quantity> <amount>2.00</amount> </item> </items> <extraAmount>0.00</extraAmount> <reference>R748</reference> <shipping> <addressRequired>false</addressRequired> </shipping> <creditCard> <token>91999999999999999999b0f</token> <installment> <quantity>1</quantity> <value>2.00</value> </installment> <holder> <name>nome proprietario cartao</name> <documents> <document> <type>CPF</type> <value>99999999999</value> </document> </documents> <birthDate>18/12/1964</birthDate> <phone> <areaCode>17</areaCode> <number>99999999</number> </phone> </holder> <billingAddress> <street>rua A</street> <number>3333</number> <district>bairro</district> <city>cidade</city> <state>SP</state> <country>BRA</country> <postalCode>05407002</postalCode> </billingAddress> </creditCard> </payment>
Exception
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><errors><error><code>11213</code><message>Malformed request: Premature end of file..</message></error></errors>
-
How do I prevent HttpClient TaskCancelled Exception that happens 1 in around 1000 requests?
I've got this issue where when a form is submitted from our client facing application to the API occasionally it hangs. The API feeds quite a few things to our application and none of the other requests ever hang. It only seems to happen when forms are being submitted. These forms typically have small files attached (we have a 25Mb limit set, most of the time they're in the Kb). It only seems to happen with certain users with slower connection speeds. Here is the original implementation I inherited.
protected string ExecuteSecuredEndpoint(HttpMethod httpMethod, string endpointUrl, string accessToken, object body = null) { var request = new HttpRequestMessage(httpMethod, $"{_baseUrl}{endpointUrl}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); if (body != null) request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); var response = Startup.HttpClient.SendAsync(request).Result; if (response.IsSuccessStatusCode == false) throw new HttpRequestException($"{response.ReasonPhrase} - {response.RequestMessage}"); return response.Content.ReadAsStringAsync().Result; }
This version failed with
AggregateException
. This makes sense because the original coder was running Async tasks Sync. So originally I though it maybe a dead lock situation created by using .Result . Since then I wrote thisprotected async Task<string> ExecuteSecuredEndpointAsync(HttpMethod httpMethod, string endpointUrl, string accessToken, object body = null) { var request = new HttpRequestMessage(httpMethod, $"{_baseUrl}{endpointUrl}"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); if (body != null) request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); var response = await Startup.HttpClient.SendAsync(request); if (response.IsSuccessStatusCode == false) throw new HttpRequestException($"{response.ReasonPhrase} - {response.RequestMessage}"); return await response.Content.ReadAsStringAsync(); }
The specific line it hangs on is
var response = await Startup.HttpClient.SendAsync(request);
. I only use the Async version to submit forms (very few forms are submitted from this application). However the same issue still happens only withTaskCancelled
instead. Its not with all or even most requests. However I can't seem to recreate the issue either locally, on test servers, or production. Finally if I pass a cancellation token in with say a 30 second timeout, its ignored and still only times out at around 2-3 mins. Our apps are published in App (customer facing and API) Services within Azure. Any help would be appreciated. Let me know if you all need anymore info.Edit 1: Here is the full stack trace from Application Insights
System.Threading.Tasks.TaskCanceledException: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at *Redacted*.Portal.Presentation.Customer.Api.Endpoints.BaseEndpoint+<ExecuteSecuredEndpointAsync>d__3.MoveNext (*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: Path\Redacted\\Agent\_work\1\s\*Redacted*.Portal.Presentation.Customer\Api\Endpoints\BaseEndpoint.cs*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 55) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at *Redacted*.Portal.Presentation.Customer.Api.Endpoints.Company+<AddSupplierAsync>d__9.MoveNext (*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: Path\Redacted\\Agent\_work\1\s\*Redacted*.Portal.Presentation.Customer\Api\Endpoints\Company.cs*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 78) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at *Redacted*.Portal.Presentation.Customer.Controllers.SupplierController+<Submit>d__3.MoveNext (*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: Path\Redacted\\Agent\_work\1\s\*Redacted*.Portal.Presentation.Customer\Controllers\SupplierController.cs*Redacted*.Portal.Presentation.Customer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 58)
-
Error: Failed to complete negotiation with the server: Error: SignalR
I am using Visual Studio 2019 and implementing SignalR 5.0.0.0 in MVC project of .NET 5 framework. but unfortunately, I always get the connection error. can you please help me out? BTW it works for .net 5 with SignalR version 3.1.0.
SignalR version snippet:
SignalR conneciton script is as follows:
var connection = new signalR.HubConnectionBuilder().withUrl("/pushNotificationHub").build(); connection.on("Recive", function (notification) { $("#modal-title").text(notification.title); $("#modal-content").text(notification.content); $('#myModal').modal('show'); }); connection.on("Connect", function (connectionId) { $("#CallerConnectionId").val(connectionId); }); connection.start().catch(function (err) { return console.error(err.toString()); });
-
.Net 5 project complaining about netcoreapp3.1 compatibility
I'm getting this error in visual studio
Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 5.0.3 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 5.0.3 supports: net5.0 (.NETCoreApp,Version=v5.0)
But the application and ALL of it's dependencies are .net 5.0. Where would the netcoreapp3.1 stuff be coming from?
-
.NET 5 + Microsoft.Data.SqlClient - Received an unexpected EOF or 0 bytes from the transport stream
I updated my app from .NET Core 3.1 to .NET 5 and now I cant open a connection to my SQL Server database. The innermost exception error message is
Received an unexpected EOF or 0 bytes from the transport stream.
The top level error message is
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed)
Other than the version of the .NET 5, I only updated the base image, from
3.1-bionic
to5.0.3-focal-amd64
Is there anything I'm also supposed to do?
EDIT 1:
I found this article that seems closely related to what im going by. But after altering myCipherString
to the values suggested, I got no change on the error. Same thing. Perhaps there's aCipherString = ANY
?