如何使用 Linq to Sql 组件部署 ASP.NET 网站

发布于 2024-10-05 21:05:44 字数 1832 浏览 6 评论 0原文

我有一个 .NET 3.5 Web 应用程序,最近添加了一些 Linq to Sql 功能——一个 dbml 文件等。在本地,它工作得很好。

但是,当我尝试部署它时,我收到一个空引用异常,显然是在它尝试调用上下文对象的构造函数时。

为了增加复杂性,我使用 Web 部署项目将其编译为单个 DLL。我假设 Linq to Sql 的东西会和其他东西一起编译。然而,现在我想我需要将 dbml 文件与 DLL 一起向上移动。

我的 dbml 文件位于 App_Code 目录中,因此我尝试在远程服务器上重新创建该目录结构。但 .NET 不允许我在预编译的应用程序上拥有 App_Code 目录。所以我只是将 dbml 文件移到根目录中 - 但我仍然收到错误。

帮助!

这是我收到的错误的堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例。] codeCS.SarcStateDataDataContext..ctor() +28 DB_Interface.SarcStateDataDB..ctor() +26 eSARC_Basic..ctor() +56 ASP.esarc_basic_aspx..ctor() +14 __ASP.FastObjectFactory_sarcwriting.Create_ASP_esarc_basic_aspx() +20 System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32 System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath,类型 requiredBaseType,HttpContext 上下文,布尔值allowCrossApp,布尔值noAssert)+119 System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext上下文,字符串requestType,VirtualPath virtualPath,字符串physicalPath)+33 System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext上下文,字符串requestType,VirtualPath virtualPath,字符串physicalPath)+40 System.Web.HttpApplication.MapHttpHandler(HttpContext上下文,字符串requestType,VirtualPath路径,字符串pathTranslated,布尔useAppConfig)+160 System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +155

DB_Interface.SarcStateDataDB是我编写的一个类,它调用上下文对象的构造函数:

codeCS.SarcStateDataDataContext context = new SarcStateDataDataContext();

我不向其发送连接字符串或连接对象,因为程序的本地版本和部署版本都使用相同的远程数据库。但这会是问题所在吗?本地应用程序可以访问 dbml 文件中的连接字符串,但部署的应用程序不能...?

更新:我查看了 Reflector 中的代码,它从 web.config 获取连接字符串,并且在这个问题上一切看起来都是一致的......

I have a .NET 3.5 web application that I recently added some Linq to Sql functionality to -- a dbml file, etc. Locally, it works fine.

However, when I try to deploy it, I get a null reference exception, apparently when it's trying to call the constructor for the context object.

To add to the complexity, I use a Web Deployment Project which compiles it into a single DLL. I assumed the Linq to Sql stuff would get compiled along with everything else. However, now I'm thinking that I need to move the dbml file up along with the DLL.

I had the dbml file in the App_Code directory, so I tried recreating that directory structure on the remote server. But .NET will not let me have an App_Code directory on a precompiled application. So I just moved the dbml file into the root directory -- but I still get the error.

Help!

Here's the stack trace for the error I'm getting:

[NullReferenceException: Object reference not set to an instance of an object.]
codeCS.SarcStateDataDataContext..ctor() +28
DB_Interface.SarcStateDataDB..ctor() +26
eSARC_Basic..ctor() +56
ASP.esarc_basic_aspx..ctor() +14
__ASP.FastObjectFactory_sarcwriting.Create_ASP_esarc_basic_aspx() +20
System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +160
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

DB_Interface.SarcStateDataDB is a class I wrote that calls the constructor for the context object:

codeCS.SarcStateDataDataContext context = new SarcStateDataDataContext();

I don't send it a connection string or connection object, because both the local and deployed versions of the program use the same remote database. But could that be the problem? The local application has access to the connection string in the dbml file, but the deployed application doesn't ... ??

UPDATE: I looked at the code in Reflector and it is getting the connection string from the web.config, and everything looks copasetic on that issue ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熟人话多 2024-10-12 21:05:44

好的,我找到问题了!它正在寻找的连接字符串名称——SarcWritingConnectionString——显然是当我最初通过向导进行设置时,Linq to Sql 已添加到 web.config 中的名称。而且我在部署项目时并没有将新的web.config移到服务器上,因为我认为它没有改变。当我把它向上移动时,一切正常!

(我使用了已经在服务器资源管理器中设置的数据连接,而不是 web.config 中已有的连接字符串 - 我什至不知道如何执行此操作。)

感谢您的帮助 - 很高兴知道我不需要上移 dbml 文件。下次会更容易!

OK, I found the problem! The connection string name it was looking for -- SarcWritingConnectionString -- was something that apparently Linq to Sql had added to the web.config when I was setting it up initially, going through the wizard. And I did not move up the new web.config to the server when I deployed the project, because I thought that it had not changed. When I move it up, everything works!

(I used a data connection I had already set up in Server Explorer instead of a connection string that was already in the web.config -- I wasn't even sure how to do that.)

Thanks for your help -- it's good to know I don't need to move up the dbml file. Next time will be easier!

美人骨 2024-10-12 21:05:44

嗨辛西娅
您不需要部署 .dbml 文件。空引用异常不是由于缺少 .dbml 引起的。很难根据提供的信息猜测问题所在,但首先检查生产和开发数据库上的数据是否相同。也许生产数据库缺少一些记录,并且您从那里得到空引用。还要确保使用 VS 发布向导部署项目(右键单击项目 -> 发布)。另外,如果这没有帮助,您可以发布异常的堆栈跟踪吗?
谢谢

Hi Cynthia
You don't need to deploy .dbml file. The null reference exception is not caused by the lack of .dbml. It's hard to guess the problem with the provided information, but first of all check that the data on the production and development database are the same. Maybe the production database lacks some records and you get nullreference from there. Also make sure that you deploy the project using VS Publishing Wizard (Right click on the project -> Publish). Also, if this doesn't help, could you post a stack trace of the exception?
Thanks

不知在何时 2024-10-12 21:05:44

我建议您传递 DB_Interface.SarcStateDataDB 实例化类时要使用的连接字符串,然后在其 codeCS.SarcStateDataDataContext context = new SarcStateDataDataContext();.

然后,您可以在应用程序级别管理连接字符串,大概是在 web.config 文件中。这样,如果您切换到使用与产品分开的开发数据库,​​那么您可以轻松过渡到单独的连接字符串。

I'd suggest that you pass DB_Interface.SarcStateDataDB the connection string you want to use when you instantiate the class, then use that in its codeCS.SarcStateDataDataContext context = new SarcStateDataDataContext();.

Then you can manage the connection string at the application level, presumably in your web.config file. This way if you ever switch to using a dev db that is separate from prod, then you easily transition to separate connection strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文