StealthRT
StealthRT

Reputation: 10542

Web API 2 accessing ServiceController : ApiController public variable from class

Hey all I am trying to figure out how to go about access a variable from the ServiceController : ApiController like so:

namespace WebApi.App.Controllers
{
    public class ServiceController : ApiController
    {
        string outputFile = "F:\\debugData\\debug.txt";
        public bool isDebuging = false;
        ...etc etc

What I am trying to get is the isDebuging value but within my Class file here:

namespace WebApi.App.Models
{
    public class checkEnviroment
    {
        public string checkEnviroment()
        {
            WebApi.App.Controllers.ServiceController["isDebuging"] = true;
            etc etc...

Is this possible to do? I can't seem to find the correct syntax in order to get or set the value from the ServiceController : ApiController.

Any help would be great!

Upvotes: 0

Views: 856

Answers (3)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

That check environment should be an ActionFilterAttribute:

public class CheckEnvironmentFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(HttpActionContext actionContext)
     {
          // Use the "as" cast to don't throw an invalid cast exception
          // if this attribute is applied to the wrong controller...
          ServiceController serviceController =
                   actionContext.ControllerContext.Controller as ServiceController;

          if(serviceController != null)
          {
                serviceController.IsDebugging = true;
          }
     }
}

Now add the whole filter attribute as regular C# attribute to your ServiceController:

[CheckEnvironmentFilter]
public class ServiceController : ApiController
...

...and the so-called filter method will be hit before any action has been executed from the whole API controller.

BTW, I would design an interface IDebuggable as follows:

public interface IDebuggable
{
     bool IsDebugging { get; set; }
}

...and I would implement it on any controller that might require the whole action filter to work:

[CheckEnvironmentFilter]
public class ServiceController : ApiController, IDebuggable
{
     public bool IsDebugging { get; set; }
}

...and finally I would refactor the so-called filter to cast controllers to IDebuggable:

public class CheckEnvironmentFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(HttpActionContext actionContext)
     {
          // Use the "as" cast to don't throw an invalid cast exception
          // if this attribute is applied to the wrong controller...
          IDebuggable debuggableController =
                   actionContext.ControllerContext.Controller as IDebuggable;

          if(debuggableController != null)
          {
                debuggableController.IsDebugging = true;
          }
     }
}

This is better than #1 approach, because now the CheckEnvironmentFilterAttribute will support any controller which implements IDebuggable.

Upvotes: 2

oleksii
oleksii

Reputation: 35905

You are probably doing this wrong. These few alternatives should get you started. The last two options are good fit for unit-testing.

#ifdef DEBUG

If you want to have some debug code that is only visible in debug version you can use DEBUG symbol. This works only if you have a checkbox "checked" in a Visual Studio project to define DEBUG symbol, it's checked by default. Sample code

#ifdef DEBUG
    // your code
#endif

Inject value into constructor

This is useful when you want to pass different values for the parameter. Sample code

public class EnvSettings
{
    public bool IsDebug {get; private set;}
    public EnvSettings(bool isDebug)
    {
        IsDebug = isDebug;
    }   
}

// then  elsewhere

public void Foo()
{
    var settings = EnvSettings(false);
    if(settings.IsDebug)
    {
        // this is debug
    }
    else
    {
        // this is something else
    }
}

Pass value as parameter to method

public class Foo
{   
    public void DoFoo
    {
         bool isDebug = false;
         var bar = new Bar();
         bar.DoBar(isDebug)
    }   
}

public class Bar
{   
    public void DoBar(bool isDebug)
    {
        if(isDebug)
        {
            // this is debug set
        }
        else
        {
            // this is something else
        }
    }   
}

Upvotes: 0

Aamir Masood
Aamir Masood

Reputation: 321

Making the property isDebugging static might help ServiceController.isDebugging = true; but then the simple question is why would you need that. If you need a global property you can use Session.

Upvotes: 1

Related Questions