简体   繁体   English

某些服务器上的LINQ-to-SQL DataContext为null

[英]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. 我有一个ASP.NET应用程序,它使用LINQ-to-SQL很多,并且在大多数服务器上运行良好。 However the application exists on another server on the same network, and after a recent update, whenever I open a new DataContext like this: 但是,应用程序存在于同一网络上的另一台服务器上,并且在最近更新后,每当我打开一个新的DataContext时,如下所示:

LINQDataContext dc = new LINQDataContext();

The dc object is null, so I get object reference errors trying to use it. dc对象为null,因此我尝试使用它时出现对象引用错误。 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? 有任何想法吗?

Something like this can't happen. 这样的事情不可能发生。 When you call the new operator on a standard .Net type, there are two possible outcomes: 当您在标准.Net类型上调用new运算符时,有两种可能的结果:

  1. the constructor returns normally and the value is assigned 构造函数正常返回并分配值
  2. 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. 除非您立即catch异常并忽略它,否则您可以确定已分配变量。 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.) (从技术上讲,还有第三种选择 - 构造函数永远不会返回并无限循环,但这与此无关。)

Since you are using default constructor of your DataContext, then connection string is read from web.config. 由于您使用的是DataContext的默认构造函数,因此从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. 在新版本的ASP中,配置文件中的连接字符串存储位置已更改。 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. 手动从配置读取连接字符串或只是硬编码以进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM