LINQ-to-SQL DataContext is null on certain server
I have a very unusual problem. I have an ASP.NET application that uses LINQ-to-SQL a lot, and works fine on most servers. However the application exists on another server on the same network, and after a recent update, whenever I open a new DataContext like this:
LINQDataContext dc = new LINQDataContext();
The dc object is null, so I get object reference errors trying to use it. I cannot replicate this on my development machine, or on the other server which the application exists on, so I'm baffled about why this could be. Any ideas开发者_如何学C?
Since you are using default constructor of your DataContext, then connection string is read from web.config. So we could make a suggestion that application on that server has different config without proper connection string. But there is one more thing that we've faced once in our project. In a new version of ASP location of connection string storage in config file has changed. I don't have code at my fingertips, but you could test my suggestion by using constructor that takes connection string as a parameter. Read connection string from config manually or just hardcode it for testing purpose.
Something like this can't happen. When you call the new
operator on a standard .Net type, there are two possible outcomes:
- the constructor returns normally and the value is assigned
- the constructor throws an exception, the value is not assigned
Unless you catch
the exception right away, and ignore it, you can be sure that the variable has been assigned. This means your problem probably lies somewhere else.
(Technically, there is a third option – the constructor never returns and loops infinitely, but that's not relevant here.)
精彩评论