1

I am trying to implement a WebHook for the BuildCompleted event. I am stuck with the error: Bad Request (400) when testing the service hook from the web interface. There seems to be no detailed information about the error in the Event Log or the IIS log files. The Register function in WebApiConfig is hit but the constructor or ExecuteAsync in my WebHook is not.

Any help would be highly appreciated! I am sure that it is some trivial that I just can seem to see myself.

My setup consists of on-prem TFS 2018 Update 2 server and a seperate Windwos Server 2012 with IIS for the the WebHook. I deploy Global.asax and web.config to wwwroot and the assemblies to wwwroot/bin.

Url: http://MYSERVER/api/webhooks/incoming/genericjson?code=83699ec7c1d794c0c780e49a5c72972590571fd8

web.config:

...
<appSettings>
    <add key="MS_WebHookReceiverSecret_GenericJson" value="83699ec7c1d794c0c780e49a5c72972590571fd8" />
</appSettings>
...

WebHookHandler:

public class GenericJsonWebHookHandler : WebHookHandler
{
    public GenericJsonWebHookHandler()
    {
        this.Receiver = GenericJsonWebHookReceiver.ReceiverName;
    }

    public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
    {
        return Task.FromResult(true);
    }
}

WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
        );

        // Initialize GenericJson WebHook receiver
        config.InitializeReceiveGenericJsonWebHooks();
    }
}

Global.asax.cs

public class Global : HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}
Tore Østergaard
  • 4,362
  • 3
  • 27
  • 43
  • Does your service work properly when tested in isolation? – Daniel Mann Jun 04 '18 at 14:30
  • Isolation? I have not added anything meaningfull to the WebHook yet. – Tore Østergaard Jun 04 '18 at 14:33
  • You've added enough to it that you've deployed it and you're getting an error calling it from VSTS. Do you get that same error when you call it locally? Have you tried to attach a debugger and step through when POSTing data to it? Basic debugging steps. – Daniel Mann Jun 04 '18 at 14:51
  • I've done some simple writing to a log file and the Register function is called but the constructor and ExecuteAsync in my WebHook class isn't. I will try to setup the WebHook locally so that I can attach a debugger. I was sure that I had just missed a log file somewhere with a detailed error message to help me along. – Tore Østergaard Jun 04 '18 at 15:29
  • Running the service locally works from my test app. Same app get 'Bad Request' when targeting the same server service af TFS. I'll try to setup a debug session directly on the server. – Tore Østergaard Jun 04 '18 at 20:48
  • @ToreØstergaard Do you get any useful information when you setup a debug session directly on the server? – Cece Dong - MSFT Jun 05 '18 at 09:44
  • Right now I am trying a few things to avoid installing debugging tools directly on the server, but if you have any ideas, then I am all ears. – Tore Østergaard Jun 06 '18 at 13:32
  • Thank you for you help both of you. And don't worry @DanielMann, the red hand shaped mark on my cheek after your "... Basic debgging steps." comment is slowly fading away :-). – Tore Østergaard Jun 07 '18 at 06:16

1 Answers1

1

This was simply a matter of the GenericJsonWebHookReceiver requiring https, which is actually specified by the documentation.

GenericJsonWebHookReceiver class decleration

I was thrown off by the TFS Service Hook add screen only recommending https: enter image description here

This answer led me to look closer at the response in the exception from the GetResponse call to the service.

{"Message":"The WebHook receiver 'GenericJsonWebHookReceiver' requires HTTPS in order to be secure. Please register a WebHook URI of type 'https'."}

Tore Østergaard
  • 4,362
  • 3
  • 27
  • 43