Using multiple delegating handlers in a web API call

时间:2016-06-06 14:16:22

标签: c# asp.net-mvc-4 asp.net-web-api .net-4.5 delegatinghandler

I have a web API call that needs to make use of 2 delegating handlers, but I never seem to get in to the second handler. The route is registered with the handlers. I am not allowed to set the handlers in the global config.

NSArray *permissions = [NSArray arrayWithObjects:LISDK_BASIC_PROFILE_PERMISSION,LISDK_W_SHARE_PERMISSION, nil];

    [LISDKSessionManager createSessionWithAuth:permissions state:nil showGoToAppStoreDialog:YES successBlock:^(NSString *returnState){
        NSLog(@"%s","success called!");
        LISDKSession *session = [[LISDKSessionManager sharedInstance] session];
        NSLog(@"Session  : %@", session.description);

        [[LISDKAPIHelper sharedInstance] getRequest:@"https://api.linkedin.com/v1/people/~"
                                            success:^(LISDKAPIResponse *response) {

                                                NSData* data = [response.data dataUsingEncoding:NSUTF8StringEncoding];
                                                NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

                                                NSString *authUsername = [NSString stringWithFormat: @"%@ %@", [dictResponse valueForKey: @"firstName"], [dictResponse valueForKey: @"lastName"]];
                                                NSLog(@"Authenticated user name  : %@", authUsername);


                                            } error:^(LISDKAPIError *apiError) {
                                                NSLog(@"Error  : %@", apiError);
                                            }];
    } errorBlock:^(NSError *error) {
        NSLog(@"Error called  : %@", error);
    }];

HandlerA triggers a breakpoint without further problems.

 DelegatingHandler[] handlers = new DelegatingHandler[2];
        handlers[0] = new HandlerA();
        handlers[1] = new HandlerB();

        RouteTable.Routes.MapHttpRoute(
            name: "TestAPI",
            routeTemplate: "api/{controller}",
            handler: HttpClientFactory.CreatePipeline(
                innerHandler: new HttpClientHandler(),
                handlers: handlers
            ),
            defaults: new { controller = "test"},
            constraints: null
        );

I'd expected to arrive in the HandlerB but it never does, it goes straight to the API controller. Handler A doesn't do anything to the request.

1 个答案:

答案 0 :(得分:1)

我在这篇博文中找到了我的问题的答案:http://byterot.blogspot.nl/2012/05/aspnet-web-api-series-messagehandler.html

第一个处理程序将第二个处理程序设置为内部处理程序,而不是为API路由提供处理程序数组。

DelegatingHandler[] handlers = new DelegatingHandler[2];
handlers[0] = new HandlerA();
handlers[1] = new HandlerB();
handlers[0].InnerHandler = handlers[1];

RouteTable.Routes.MapHttpRoute(
        name: "TestAPI",
        routeTemplate: "api/{controller}",
        handler: handlers[0],
        defaults: new { controller = "test"},
        constraints: null
    );