Extension method:
namespace PantallaHornosApp
{
public static class ExceptionHelper
{
public static string GetErrorDetails(this Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Error: {0}", ex.InnerException);
Exception innerExc = ex.InnerException;
int level = 1;
while (innerExc != null)
{
if (!string.IsNullOrWhiteSpace(innerExc.Message))
{
sb.AppendFormat("Inner Exception {0}: {1}", new[] { level.ToString(), innerExc.Message });
}
innerExc = innerExc.InnerException;
level++;
}
sb.AppendFormat("Stack trace: {0}", ex.StackTrace);
sb.AppendFormat("Target site: {0}", ex.TargetSite);
sb.AppendFormat("Source: {0}", ex.Source);
return sb.ToString();
}
}
}
I want to call it like this:
namespace PantallaHornosApp
{
public class PartesController : ApiController
{
public IHttpActionResult Post(Parte parte)
{
try
{
using (HornosContext db = new HornosContext())
{
db.Partes.Add(parte);
int res = db.SaveChanges();
}
return Ok();
}
catch (Exception ex)
{
//ERROR
//Error 1 'System.Exception' does not contain a definition for 'GetErrorDetails' and no extension method 'GetErrorDetails' accepting a first argument of type 'System.Exception' could be found (are you missing a using directive or an assembly reference?) c:\users\atorres\proyectos\pantallashornos\pantallahornosapp\api\partescontroller.cs 51 37 PantallaHornosApp
Trace.TraceError(ex.GetErrorDetails());
return InternalServerError(ex);
}
}
}
}
- Intellisence doesn't identify it for me.
- They are in the same namespace
- The extension class and method are static
- I am using the word "this"
- Everything is in the same project
- I already restarted visual studio
- I already removed and re-added the project in the solution
- I already gave it clean - rebuild
The problem has to do with the namespace and not with the Exception type, I made an extension method for String and it doesn't find it either
What's going on?
Bonus: I re-enabled Web Essentials and the problem did not recur.
Put the corresponding using in the class where you want to invoke it.
In the help ( https://msdn.microsoft.com/es-co/library/bb383977.aspx ) it says the following: