1

Setup

I have a method with attribute on it that I created. I have packed the attribute into a nuget package (debug symbol mode) like I do when I want to debug into nuget packages.

Question

How can I step into this attribute? Do I need to put some other "step into this" attribute into my attribute code? I have only found the attribute that stops my from debugging DebuggerStepThroughAttribute but nothing that will allow me to explicitly stop when in debug (witch you of course don´t need normally).

I hope I have explained this well enough.

Edit (more info)

Jordan suggested calling GetCustomAttributes in the method but that is not an option (at the least in my case). My attribute is doing a token validation on the API call so you are not allowed (401) into the method if the code in the attribute denies it access. And also I can´t put a brake point into the nuget packgage, I need to be able to step into that code.

Edit 2 (finally... code)

So finally there is code.. I did´t think I needed one but here we are :-).

I have implemented my own attribute (as you can do) where there is code I would love to be able to debug into.

public class TokenAuthenticate : ActionFilterAttribute, IAuthenticationFilter
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {

        base.OnActionExecuting(actionContext);
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        base.OnActionExecuted(actionExecutedContext);
    }

    public bool AllowMultiple => true;

    public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
      //... where I want to debug into
    }
}

This attribute then just goes on the method or class like this [TokenAuthenticate ]

Sturla
  • 3,446
  • 3
  • 39
  • 56
  • Attributes are decorations and do not alter execution without reflection. What attribute are you using and are you using it correctly? If you just try to instantiate the decorated class without the related frameworks nothing will ever happen. – Matthew Whited Jul 19 '17 at 14:19
  • @MatthewWhited I updated the question with code. I´m fairly sure I'm using this correctly. Been doing this long time but have never had my attributes in a nuget package and needed to debug into the code. I can debug fine if I just import the nuget project into my solution. – Sturla Jul 19 '17 at 14:54

2 Answers2

0

According to MSDN, Attributes are instantiated lazily:

It's important to note that these Attribute objects are instantiated lazily. That is, they won't be instantiated until you use GetCustomAttribute or GetCustomAttributes. They are also instantiated each time. Calling GetCustomAttributes twice in a row will return two different instances of ObsoleteAttribute.

If you need to step into them you should be able to do this by calling GetCustomAttributes on the Type in question and placing a break point inside the Attribute.

Jordan
  • 639
  • 1
  • 13
  • 30
  • I´m not quite following. Where would you like me to call GetCustomAttributes? If i do that in the method that has the attribute its to late. I´ll update my question to reflect that. – Sturla Jul 19 '17 at 14:09
  • @Sturla Looking at the code above, is there a controller you can put a break point at and then attempt to step through from there? – Jordan Jul 19 '17 at 15:54
  • There is a controller and it has this attribute on top and even if I F11 from the constructor I never hit the attribute code. And I made doubly sure the nuget package was build in debug. – Sturla Jul 19 '17 at 16:03
  • On top of the controller class? Can you put the breakpoint at the end of an action on the controller, then step through the action that is executed? – Jordan Jul 19 '17 at 16:21
0

In my case I had to debug a custom attribute that I created. I came to an peculiar solution, but it worked for me.

You can add a constructor to your TokenAuthenticate class and invoke it from your Startup.cs for example, if you are working with WebApi.

        public void Configure(...)
        {
            
#if DEBUG
            // Hack to debug your custom attribute, put a breakpoint here
            var myVar = new TokenAuthenticate ();
#endif

Then you can step into the constructor when your web project is starting... and when the code of your TokenAuthenticate class appears, you can put a breakpoint where you want it the debugger to stop in future.

In case you have your custom attribute in your own nuget package. you can include the PDB file into your generated nuget package, with this settings at the *.csproj file used to generate de nupkg.

<IncludeSymbols>true</IncludeSymbols>

I hope this works for you and purity detractors not to be angry with me.

Reference: Including pdb files into nupkg

Aquiles
  • 857
  • 1
  • 10
  • 19