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!

4
  • For sure that code won't remotely work :) Where / how you set isDebugging? Commented Feb 12, 2016 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. Commented Feb 12, 2016 at 17:19
  • Read more about how to create instances of classes in C# dotnetperls.com/new Commented Feb 12, 2016 at 17:19
  • AH there it is, @Regfor. Thanks! Feel free to make that the official answer. Commented Feb 12, 2016 at 17:21

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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
        }
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.