0

I currently have a controller in my WebAPI that is returning a list of some items in my database:

As below:

public ICollection<TherapyGroup> OffUnit()
{                
      return _context.TherapyGroups.OrderBy(g => g.Name)
                     .Where(x => x.GroupTypeOffUnitOnUnit == TherapyGroup.OffUnit)
                     .ToList();               
}

Everything works fine when I am testing against my local database; however, when I switch out the database string for a remote database it throws a System.AccessViolationException. Based upon breakpoints, the data is being returned from the database fine, and it seems the problem is happening when the method returns to the WebApi in MVC. This has become a very frustrating issue, and I would love any help to navigate it.

The error window that is popping up: enter image description here

Cody Short
  • 47
  • 7
  • 1
    Can you please provide the stack trace from the exception you're seeing? – Eduard Malakhov Nov 16 '16 at 20:36
  • I don't see a stack trace... Note that the violation is happening in an unknown module. – Cody Short Nov 16 '16 at 20:42
  • Please add try catch block in your code. Then put more details. – Bigeyes Nov 16 '16 at 20:43
  • In .net whenever you get an exception, you have the stack trace. It is very unlikely that in your case it is much different. – Eduard Malakhov Nov 16 '16 at 20:45
  • [Here](http://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception) may be providing some help. – Bigeyes Nov 16 '16 at 20:46
  • I added an image of the actual error message. There is no stack trace viewable there. Also, I will check out that link and see if that helps any. I believe the problem has something to with my database and the QA database being different versions. – Cody Short Nov 16 '16 at 20:53
  • Have you tried enabling the "Break when this exception type is thrown" option? It forces the debugger to break when the exception is triggered, not when it escapes the handler. – Eduard Malakhov Nov 16 '16 at 20:54
  • I just did that. Thanks. I was able to exit the window above and navigate out. to visual studio. There is nothing in the call stack window. Any recommendations as to where to go from here? – Cody Short Nov 16 '16 at 20:58

1 Answers1

0

Decorate the methods you want to catch these exceptions in with the HandleProcessCorruptedStateExceptions attribute. See https://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 for details.

[HandleProcessCorruptedStateExceptions] 
[SecurityCritical]
public ICollection<TherapyGroup> OffUnit()
{
      try
      {
          return _context.TherapyGroups.OrderBy(g => g.Name).Where(x => x.GroupTypeOffUnitOnUnit == TherapyGroup.OffUnit).ToList();
      }
      catch (Exception e)
      {
          // set break point to see the stack trace.
          Debug.WriteLine(e.InnnerException);
          return null;
      }
}
Bigeyes
  • 1,508
  • 2
  • 23
  • 42
  • I have done that. It isn't reaching the break point it seems. Which suggests the problem is happening after the return. – Cody Short Nov 16 '16 at 21:07
  • @CodyShort, you could add `` in your configuration file. Also please to look at [here](http://stackoverflow.com/questions/2950130/how-to-simulate-a-corrupt-state-exception-in-net-4). – Bigeyes Nov 16 '16 at 21:17
  • I think I'm getting to issue.. I believe the way data types in sql server v10 (remote) and v9 (local) are different in some way (perhaps date times?) It seems if i take the object and use to string to prep a return object things are working. – Cody Short Nov 16 '16 at 21:25