OpenTelemetry .NET HttpClient not propagating TraceId - asp.net-core

I am trying to get Tracing using OpenTelemetry to work with HttpClient in an integration test for an Asp.NET API.
Tracing works for everything else, we get traces from the API controllers and all other instrumented libraries.
Configuration looks like this:
webApplicationBuilder.Services.AddOpenTelemetryTracing(b =>
b.SetResourceBuilder(resourceBuilder)
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddProtoActorInstrumentation()
.AddRedisInstrumentation()
.AddOtlpExporter(options =>
{
ConfigureOpenTelemetry(webApplicationBuilder, options);
})
);
But when calling the API using HttpClient. the current TraceId is not propagated.
The integration test uses a MyAppFactory : WebApplicationFactory<Program> for the test.
And the HttpClient is constructed in the tests using the factory.CreateClient() of the MyAppFactory.
If I check the Activity.Current.TraceId inside my integration test. I get one value.
Then directly after, when I call the API using the HttpClient, the API controller reports a different TraceId.
There are also no w3c trace context headers in the Request inside the controller method.
What am I missing here?

Related

How to update options in Restsharp v107 (RestClientOptoins)

I'm not finding any RestClient method to update its options, what I need to do is for example disable FollowRedirects for certain requests.
How do I do the following but with v107?
client.FollowRedirects = false;
Background: maybe a separate issue but current problem is that RestSharp is not following a redirect URL to Okta from a Location header of a response, it goes to the main Client URL instead. That is why I've decided to disable redirects to try following the redirect manually.
Most if not all of the properties in RestClientOptions are used to configure the HttpMessageHandler instance wrapped by RestClient. As each RestClient instance wraps a single HttpClient (and its handler), those options cannot be changed. It works the same way as configuring HttpClient, where you cannot change the AllowAutoRedirects property of the handler once it's configured.
That's why the documentation suggests using a specifically configured RestClient instance per remote API. Normally, the API uses a single convention and configuration requirement.
I have seen that some authentication endpoints require redirects, but most of the time the RestClient instance used to get the authorization token is not the one used to access the API itself with the retrieved token. So, the solution would be to have a separate instance for that purpose. Normally, it's only used once to get the token, then you can dispose it and reuse the token.
I keep posting the authenticator example from the docs https://restsharp.dev/usage.html#authenticator
Concerning RestSharp not following redirects properly, it's not what RestSharp does as it doesn't compose or execute HTTP calls physically. It just wraps HttpClient.

How can I call a SignalR Hub via Postman?

I know it seems a duplicate question from this thread Is it possible to call a SignalR Hub from Postman, but I already know that it is possible, after Postman version > 8.0 using WebSocket Request block. But I can not find any good example of how to do it.
I can connect to my hub via Postman, just by passing the hub Url like this:
But I don't know how can I call the hub method and pass the parameters. Currently, my client program.cs code calls
await hubConnection.InvokeAsync("GetTrades", _username);
and my connection.On:
hubConnection.On<Trade>("ReceiveTrades", (trade) =>
{
var tradeAsJson = JsonConvert.SerializeObject(trade);
Console.WriteLine($"Trade received: {tradeAsJson}");
});
How it would be to represent this calls from postman?
I create a sample project with signalR, you can download it from github. We can open site with https://localhost:44381.
Tips: please replace SignalR_ConnectionString in appsettings.json.
And if we want connect to signalR hub, we can use postman, you can follow the steps like below picture.
use wss://localhost:44381/chatHub.

How to ignore ResponseWrapper in ASP.NET Core Web API response

I have an web api project in .net core and in Startup there is configured to use a response wrapper "
app.UseResponseWrapper();"
But this format is applied for all the api methods in my project...
I want an api method in my solution that sends another format response , for example a simple xml. I want to know how to ignore that Response wrapper that is applied for all methods? Is there any decorator for that method ?
I solved my issue.
I had built a custom decorator for methods that is able to modify header response.
In the Response wrapper I succeded to manage and to modify the response

I am getting net error ERR_CONNECTION_RESET when calling an API method using ajax

We have a IIS 8 web server on which Web API is deployed. We have a front end application written in extJS. When the application accessed, it makes sever API calls to the server and many of these calls return success except one specific API call. Chrome says request status as cancelled (ERR_CONNECTION_RESET), and I am not able to figure out how to troubleshoot and fix this error. I have checked many places online but I could not get much help.
Please help.
Are you using Entity Framework Core? If so, you may have an object graph loop in your many-to-many relationships as EFCore does not support lazy loading at this time.
If so, you can configure your serializer to not follow graph loops.
In your Startup.cs file, replace the services.AddMvc() line with this:
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Web Api documentation with swashbuckle

We are currently trying to start writing WebApi services for our products switching from traditional WCF SOAP based services. The challenge we have got is how to provide the api documentation. I came across the SwaggerUi/swash buckle.
One limitation we have is we do not want to host the WebApi services in IIS but in a Windows Service. I am new to Web Api so I might be doing things the wrong way.
So for testing, I am hosting the web api in a console application. I can use the HttpClient to invoke the Get method on the Web Api but I can't access the same if I type the url in a web browser (is this normal for self hosted web api?).
So I installed the Swashbuckle.core nuget package and included the following code in the Startup class (Owin selfhosted).
var config = new HttpConfiguration();
config
.EnableSwagger(c =>
{
c.IncludeXmlComments(GetXmlCommentsPath());
c.SingleApiVersion("v1", "WebApi");
c.ResolveConflictingActions(x => x.First());
})
.EnableSwaggerUi();
private static string GetXmlCommentsPath()
{
var path = $#"{AppDomain.CurrentDomain.BaseDirectory}\WebApiHost.XML";
return path;
}
When I browse to the following location
http://localhost:5000/swagger/ui/index
I get "page cannot be displayed" in IE. Similar for chrome.
Is there anything special that needs to be done when hosting a WebApi in a console/windows service application to get the documentation automatically?
(I have enabled Xml documentation for the project)
I have now attached a test project. Please follow the link below:
Test project
Regards,
Nas
Your problem is not with Swashbuckle, which is configured correctly. Instead it is with the fact that your OWin web app has closed by the time the browser navigates to the swagger URI. Your using statement means that the web app is shut down at the end of it - well before Chrome has opened and navigated to the swagger path. You need to ensure the web app is still running - then your path will be valid (although in your source code you have different ports 9000 and 5000 in your url variables).