webservice oneway and new SPSite(myUrl)

旧城冷巷雨未停 提交于 2020-01-24 22:04:26

问题


I have a problem with a oneway web method that open a moss site (probably because in a oneway webmethod the context is null)

Is possible to rewrite this code to remove the null reference exception? (without the oneway attribute i don't have the exception)

[SoapDocumentMethod(OneWay = true)]
        [WebMethod(Description = "TestOneWay")]
        public voidTestOneWay(string webUrl)
        {
            using (SPSite site = new SPSite(webUrl))
            {
                 ....
            }
         }

the exception is:

[2304] Error:Object reference not set to an instance of an object.
[2304] w3wp.exe Error: 0 :
[2304StackTrace:   at System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(Int32 index)
[2304]    at System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name)
[2304]    at System.Web.HttpRequest.AddServerVariableToCollection(String name)
[2304]    at System.Web.HttpRequest.FillInServerVariablesCollection()
[2304]    at System.Web.HttpServerVarsCollection.Populate()
[2304]    at System.Web.HttpServerVarsCollection.Get(String name)
[2304]    at System.Collections.Specialized.NameValueCollection.get_Item(String name)
[2304]    at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)
[2304]    at Microsoft.SharePoint.SPRequestManager.GetContextRequest(SPRequestAuthenticationMode authenticationMode)
[2304]    at Microsoft.SharePoint.Administration.SPFarm.get_RequestNoAuth()
[2304]    at Microsoft.SharePoint.SPSite.CopyUserToken(SPUserToken userToken)
[2304]    at Microsoft.SharePoint.SPSite.SPSiteConstructor(SPFarm farm, Guid applicationId, Guid contentDatabaseId, Guid siteId, SPUrlZone zone, Uri requestUri, String serverRelativeUrl, Boolean hostHeaderIsSiteName, Uri redirectUri, Pairing pairing, SPUserToken userToken)
[2304]    at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
[2304]    at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
[2304]    at Reply.Moss2EAI.SPHandler.GetAllFlowConfiguration(String webUrl, String flowConList)
[2304]    at Reply.Moss2EAI.EaiMossIntegration.GetMessagesFromEai(String webUrl, String listName, String applicationName)

回答1:


i have found a workaround to solve my problem.Create new HttpContext. The question now is: is it the right solution or there are implications that i don't consider?

the method that i use to change the context is this:

//Call this before call new SPSite()
 private static void ChangeContext(string webUrl)
        {
            Trace.TraceInformation("ChangeContext");
            HttpContext current = HttpContext.Current;
            HttpRequest request = new HttpRequest("", webUrl, "");
            HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));
            HttpContext.Current.User = current.User;
        }



回答2:


The only implication that I see is that you are actually using a new HttpContext.

Well..duh :)

Anyway, this will have implications if you also have input- and outputfilters set up. For example if you are using WebServiceExtensions (WSE) with your own input and outputfilters. In that case, you should store the data in the HttpContext (that is there in your input- and outputfilters) and use that one. Just so that you use only one httpContext, instead of creating a second one, and to prevent any future errors when extending your software.




回答3:


Use this will work for you:

SPWeb web = SPContext.Current.Web


来源:https://stackoverflow.com/questions/340192/webservice-oneway-and-new-spsitemyurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!