I want to set a variable on my service instance before each request in that service is executed. Can this be done with an attribute? If not by an attribute, is there another way to get the service instance and call a method on it or set a variable before the request executes?
Sample code to illustrate my point:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class SetRequestNameAttribute : RequestFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object requestDto)
{
//how to set the requestName variable on the TestService instance from here?
}
}
[SetRequestName]
public class TestServices : Service
{
public string requestName = "";
public object Get(Hello request)
{
requestName = request.Name; //want to remove this, and use attribute instead
return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
}
public object Post(Hello request)
{
requestName = request.Name; //want to remove this, and use attribute instead
return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
}
}