Umbraco Document.getProperty(...).Value 抛出空引用异常
我正在编写一个链接到 Umbraco 的小型应用程序(一个小型独立控制台应用程序,最终将作为服务器上的计划任务运行),并且我正在使用 Umbraco API (4.5.2) 来更改数据库/文档。
这是我正在做的事情的一个片段:
IEnumerable<Document> documents = Document.GetChildrenForTree(parentDocumentId);
foreach (Document doc in documents.Where(d => d.Published))
{
doc.getProperty("myData").Value = "some data"; // Exception here
// ...other stuff here...
}
但是,我总是得到 NullReferenceException,因为没有属性。这让我很困惑,因为我可以看到 umbraco 接口中有 5 个属性。
一位同事建议我使用 Node 而不是文档,但是我什至无法创建 Node,因为我从 Node 类构造函数中收到 NullReferenceException。
Node myNode = new Node(-1); // NullReferenceException here
有人有什么想法吗?
I am writing a small app that links into Umbraco (a small stand-alone console application that will eventually run as a scheduled task on the server) and I'm using the Umbraco APIs (4.5.2) to make changes to the database/document.
Here is a fragment of what I'm doing:
IEnumerable<Document> documents = Document.GetChildrenForTree(parentDocumentId);
foreach (Document doc in documents.Where(d => d.Published))
{
doc.getProperty("myData").Value = "some data"; // Exception here
// ...other stuff here...
}
However I always get a NullReferenceException because there are no properties. This confuses me because I can see that there are 5 properties in the umbraco interface.
A colleague suggested that I use a Node instead of a document, however I can't even create one as I get a NullReferenceException from the Node class constructor.
Node myNode = new Node(-1); // NullReferenceException here
Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档类从 umbraco 数据库获取/设置信息。由于您在带外控制台应用程序中运行代码,因此它无法找到 umbraco 上下文。因此抛出空引用异常。
您需要在 umbraco 进程内运行代码。有一个用于第三方集成的 asmx Web 服务。 /umbraco/webservices/api/documentservice.asmx
The document class gets/sets information from the umbraco database. Since your running code in an out of band console application it can't find the umbraco context. Therefore throwing a null reference exception.
You need to run the code inside of the umbraco process. There is a asmx webservice that exists for third party integration. /umbraco/webservices/api/documentservice.asmx
实现此目的的另一种方法是使用 linq2umbraco。
有关更多详细信息,请参阅 http://our.umbraco.org/forum/core/41-feedback/7699-UmbracoLinq-in-console-app--Having-some-troubles
Another way of achieving this could be to use linq2umbraco.
for further details see http://our.umbraco.org/forum/core/41-feedback/7699-UmbracoLinq-in-console-app--Having-some-troubles
我最近查看了 4.5.2 源代码,发现填充 Document 和 Node 对象只需要使用 umbracoDbDsn 进行连接。因此,如果您有一个名为 umbracoDbDsn 的 AppSetting,它指向一个有效的 Umbraco 数据库实例,那么您就可以了。
HTH,
本杰明
I checked out the 4.5.2 source recently, to find that populating Document and Node objects only requires a connection using umbracoDbDsn. So if you have an AppSetting called umbracoDbDsn which points to a valid Umbraco database instance, you'll be good.
HTH,
Benjamin