Method finishes its execution unexpectedly when calling another method

Asked

Viewed 114 times

0

I am implementing a method that performs a call to another method that should return one IEnumerable<TEntidade>. However, when returning from this list the method that started the call simply ends its execution without continuing to read the following code.

Debugging, I can access the query variable value within the scope of the List method.

Follow below example code. After the line var x = Listar(); the code ends its execution.

public void DoSomething()
{
      //Some code here
      var x = Listar();
      //Some code here
}


public IEnumerable<ModelEntities> Listar()
{
    var _c = new SMSEntities();
    var query = //query Linq
               select new ModelEntities
               {
                     //inicializando campos
               };
    return query;
}
  • Have you tried debugging using breakpoints?

  • @mutlei, the purification that I mentioned in the code is using breakpoints.

  • Ever tried to make printfs (or the equivalent in C#, which I don’t know) in parts of the code to know where it goes?

  • Yes. When finishing the execution of the List method, it finishes the execution of the Dosomething method

  • Are there any exceptions to be released? already seen in windows Event Viewer?

  • Explicit exception is not released. I can even view the query data. If I give a tolist inside the List method I have the list of objects from the database. I will check now in the Viewer Event

  • I returned EF6 to EF5 and the code worked without any problems. I will keep the question because I have not yet found the explanation for such, perhaps incompatibility with the Windowsservice.

  • Downgrading the EF version doesn’t make any sense... probably there is some other problem, which you can’t see, outside the code posted.

  • 1

    When working with asynchronous methods (i.e. async/await) this type of mysterious code interruption is very common. for example, when it is not used await to call a method async, ai you are debugging the body of the async method, and after a call await, nothing of the execution continue... hehe... type was already right!

  • @Miguelangelo, thanks for the information, very useful. However, was not using the resource of asymptomatic methods in this case. I still can’t connect the EF downgrade to solve the problem.

  • Ever tried to return as an anonymous type?

Show 6 more comments

1 answer

2

This doesn’t return one IEnumerable<ModelEntities>:

var query = //query Linq
           select new ModelEntities
           {
                 //inicializando campos
           };
return query;

This returns a IQueryable. The return nor even execute the query itself, which is why you don’t see any errors.

To return IEnumerable you need to change the return:

return query.ToList();
  • Gypsy, I posted the original question, but before I asked it I tried it in several ways, including using Tolist(), and still the error persisted.

  • @Vinícius In fact I did not understand the purpose of this code. It is returned where?

  • In the Dosomething method, it executes the List() method which returns a list of clients to which SMS will be sent in the Dosomething method itself. As I commented above, returning the EF version to 5 the same code compiled and ran normally.

  • @Vinicius, are you using some kind of Lazyloading in your application? If you are, you may be firing this Lazyload and the Entity is misconfigured.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.