ServiceStack - Dealing with 'Parameter is never used' warning

c#, code-analysis, resharper, servicestack

Solution

GC.KeepAlive(request);

`GC.KeepAlive` documentation.

Importantly:

The KeepAlive method performs no operation and produces no side effects other than extending the lifetime of the object passed in as a parameter.

While esker's solution (+1) is useable it's a little cumbersome, as mentioned, it's like using the code comments. I think MarcGravell's suggestion is a simple fix, that is ultimately code-analysis system independent too. Unfortunately he never posted an answer, just a comment.

Problem

I was running Resharper code analysis on my ServiceStack project and it warns about parameters on certain service actions not being used. Which is true. The dilemma I am facing is that on routes where there are no parameters needed, such as a simple GET request to return a list of courses: ``` [Route("/Courses","GET")] public class ListCoursesRequest : IReturn<List<CourseResult>> {} ``` Then the action won't use the request object (`ListCoursesRequest`), causing this warning. ``` public List<CourseResult> Get(ListCoursesRequest request) { ... } ``` Warning: Parameter 'request' is never used. I could have Resharper ignore the warning by using an ignore comment, but I don't like littering my code this way. But because ServiceStack routes to the action based on the parameter `ListCoursesRequest` it can't be removed, unless there is a different way to handle the parameterless route scenario. Perhaps an attribute on the action? I am trying to employ best practises and keep clean code, I know it's cosmetic and I suspect it's just something I'll have to live with, but I thought I would ask.

Original source