I've got a WCF service that uses a LinqToSql DataContext to fetch some information out of a database. The return type of the operation is IEnumerable<DomainObject
>, and I have a helper method that converts from the Table-derived LINQ object to a WCF data contract like so:
[OperationContract]
public IEnumerable<DomainObjectDTO> RetrieveDomainObjects()
{
var context = CreateDataContext();
return from domainObject in context.DomainObjects
select ConvertDomainObject(domainObject);
}
private DomainObjectDTO ConvertDomainObject(DomainObject obj)
{
// etc...
}
This code exhibits a strange behaviour if I pass an invalid connection string to the DataContext. Being unable to find the correct database, presumably the above code throws a SqlException when enumerating the IEnumerable<DomainObjectDTO
> when serialization is happening. However, when I run this code in my debugger, I see no first-chance exception on the server side at all! I told the debugger in the Exceptions tab to break on all thrown CLR exceptions, and it simply doesn't. I also don't see the characteristic "First chance exception" message in the Output window.
On the client side, I get a CommunicationException with an error message along the lines of "The socket connection terminated unexpectedly". None of the inner exceptions provides any hint as to the underlying cause of the problem.
The only way I could figure this out was to rewrite the LINQ code in such a way that the query expression is evaluated inside of the OperationContract method. Incidentally, I get the same result if there is a permissions problem, or if I wrap the DataContext in a using statement, so this is not just isolated to SqlExceptions.
Disregarding the inadvisability of making the return type an IEnumerable<T
> and only enumerating the query somewhere in the depths of the serializer, is WCF suppressing or somehow preventing exceptions from being thrown in this case? And if so, why?