System.InvalidOperationException:由于对象的当前状态,操作无效。

时间:2013-04-25 08:49:58

标签: c# wcf sharepoint-2010 sharepoint-object-model

我正在为SharePoint Foundation 2010开发asp.net mvc应用程序.asp.net应用程序托管在一台计算机上,SharePoint Foundation 2010托管在另一台计算机上。我在SharePoint环境的ISAPI文件夹中部署了一个自定义wcf服务。此自定义wcf服务以编程方式创建内容数据库。 asp.net应用程序使用此自定义wcf服务。我正在使用这种方法,因为我的方案类似于链接 - http://blog.sharedove.com/adisjugo/index.php/2012/07/31/creating-site-collections-in-specific-content-database/。实现上述场景我遇到了错误 - http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopmentprevious/thread/f10d009b-13c4-4942-86b4-2ab8c4f5994d。我用http://beyondweblogs.com/sharepoint-2010-sppersistedobject-access-denied/解决了这个错误。现在我收到错误 - System.InvalidOperationException:由于对象的当前状态,操作无效。

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
   at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest()
   at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
   at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Add(T newObj, Boolean ensure)
   at Microsoft.SharePoint.Administration.SPPersistedChildCollection`1.Ensure(T newObj)
   at Microsoft.SharePoint.Administration.SPContentDatabaseCollection.Add(Guid newDatabaseId, String strDatabaseServer, String strDatabaseName, String strDatabaseUsername, String strDatabasePassword, Int32 warningSiteCount, Int32 maximumSiteCount, Int32 status, Boolean provision, Guid lockId, Int32 addFlags)
   at Microsoft.SharePoint.Administration.SPContentDatabaseCollection.Add(String strDatabaseServer, String strDatabaseName, String strDatabaseUsername, String strDatabasePassword, Int32 warningSiteCount, Int32 maximumSiteCount, Int32 status)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.<createContentDb>b__7()
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.createContentDb()
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.<>c__DisplayClass3.<GetName>b__1()
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at Niks.SP2010.SPHostedWCFService1.SPHostedWCFService.GetName()

此错误在上面的链接bolg.sharedove.com中有所描述。我想我需要按照bolg.sharedove.com

中的描述设置以下代码
var storeContext = HttpContext.Current; 
HttpContext.Current = null; 
// do your code 
HttpContext.Current = storeContext;

我thnik我无法在wcf服务中设置上述代码。我试图在asp.net应用程序中设置它,但发生了同样的错误。请问anuone请告诉我应该在哪里设置此代码?如果有人有不同的解决方案,请分享。

1 个答案:

答案 0 :(得分:0)

我只是在wcf服务应用程序中添加了对System.Web.dll的引用。现在我可以在wcf中使用HttpContext。我在wcf

中设置了以下代码
var storeContext = HttpContext.Current; 
HttpContext.Current = null; 
// do your code 
HttpContext.Current = storeContext;

现在wcf中的所有代码都运行正常。我现在能够以编程方式创建内容数据库和网站集。