0

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!

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • For sure that code won't remotely work :) Where / how you set `isDebugging?` – Claudio Redi Feb 12 '16 at 17:17
  • @ClaudioRedi just the normal **isDebugging = false;** and that works just fine if i'm within the **ServiceController : ApiController** class. However, once I'm in another created class file and try to reference that public variable then that's what I can do that I would like to do. – StealthRT Feb 12 '16 at 17:19
  • Read more about how to create instances of classes in C# http://www.dotnetperls.com/new – Regfor Feb 12 '16 at 17:19
  • AH there it is, @Regfor. Thanks! Feel free to make that the official answer. – StealthRT Feb 12 '16 at 17:21

3 Answers3

2

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.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

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.

Aamir Masood
  • 321
  • 2
  • 9
0

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
        }
    }   
}
oleksii
  • 35,458
  • 16
  • 93
  • 163