How to set tower middleware dynamically

tower middleware injection, the following is normal

use tower_http::trace::TraceLayer;
let layer = tower::ServiceBuilder::new()
       .layer(TraceLayer::new_for_grpc())
       .into_inner();

In practice, however, my layer is set dynamically, so changing it to this will result in an error

let mut layers = Vec::new();
layers.push(TraceLayer::new_for_grpc());

let mut layer = tower::ServiceBuilder::new();
for item_layer in layers {
    layer = layer.layer(item_layer);
}
layer = layer.into_inner();

Error reported as follows:

expected struct ServiceBuilder<Identity> found struct ServiceBuilder<Stack<TraceLayer<SharedClassifier<GrpcErrorsAsFailures>>, Identity>>

This is a duplicate of your previous post. Could you please delete it?

I wanted to delete the old one, but I couldn't. I wanted to keep this one

I removed it at your request, but it appears to be exactly the same. In the future it would be better to edit your post, if you have minor changes.

Note that you won't get more attention by reposting or asking twice. The reason you haven't gotten an answer is not because people aren't paying attention. It is probably because not very many people who use tonic are reading this forum. Have you tried the tonic discord chat as I suggested in your earlier post?:

Tonic is a little specialized, you might have better luck with their discord "Chat" link here, near the top of the readme under the badges:
GitHub - hyperium/tonic: A native gRPC client & server implementation with async/await support.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.