views:

210

answers:

2

I have a LINQ to SQL/Unity/ASP.NET MVC/SQL Server application hosted on Azure. I am using Lazy Evaluation throughout the whole site. The Application is using a TCP connection to SQL Server and every so often I get a "A transport-level error has occurred when sending the request to the server." SqlException.

Well since the the query is getting materialized when it is called later in code, I can't just wrap a specific piece of code with a try/catch.

Is there a way that I can handle this exception by implementing an interface or attaching a delegate to the DataContext?

A: 

I think that the only way to fix this is to wrap the code that is triggering (materializing) the call to the SQL server in a try catch.

Shiraz Bhaiji
+3  A: 

If you use the results from linq as-is, then you have to handle the error in the code that materializes the data. However, instead of simply returning the linq query results directly, you could wrap it in an IEnumerable implementation of your own design, which is free to implement whatever error handling you like. This way the code that consumes the IEnumerable doesn't have to handle the exception.

Xoltar
I didn't try this out but it seems like the most logical choice. If I run into this problem again, on a different project, I will try it out. As it stands I had to move onto other projects.
zznq