def _compose_middleware(method, middleware):
    """Apply the given ServiceMiddleware to the provided method and return
    an InvocationHandler.

    Args:
        method: instance method
        middleware: list of ServiceMiddleware

    Returns:
        InvocationHandler
    """

    def base_handler(method, args):
        return method(*args)

    handler = base_handler
    if middleware:
        for m in middleware:
            handler = m(handler)
    return handler