Did you run this script (Startup.cs) when testing? If so, can you print the validation-token to the console?
app.Run( async (context) =>
{
context.Request.Headers.TryGetValue("Validation-Token", out StringValues validationToken);
context.Response.Headers.Add("Validation-Token", validationToken);
if (context.Request.Path == "/webhook" && context.Request.Method == "POST")
{
using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
}
});
I've ran both the server and the setup projects as listed in the quick start guide using the command line prompts given within the guide. This is what I get from the server (script above) project:
I don't see a validation token anywhere, and I'm not able to do anything with the console except exit out of running it. Is there a component that I'm missing somewhere? I've put my full Startup.cs for the server down below as reference (it should be the same as the guide unless I've missed something?)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
app.Run(async (context) =>
{
context.Request.Headers.TryGetValue("Validation-Token", out StringValues validationToken);
context.Response.Headers.Add("Validation-Token", validationToken);
if (context.Request.Path == "/webhook" && context.Request.Method == "POST")
{
using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8))
{
var str = reader.ReadToEnd();
Console.WriteLine(str);
}
}
});
}
}
It appears that it was a problem on the server side. My project targets .NET 7.0 and it was missing calls to configuration! Here's the relevant code added if anyone else is having this issue:
In Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
In Program.cs
using webhook_server;
var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app, app.Environment);
app.Run();
Thank you for pointing me in the right direction for looking at the server. Would it be possible for this information be added to the documentation regarding target frameworks for C# Webhooks?
I ma using old .NET 5.0. Let me check if adding the configuration to old .NET would also work then I will update. Otherwise, I can add a note to the dev guide. Thanks for sharing your finding!