[英]Can only call SharePoint 2007 web service using WCF client after calling with ASMX Web Reference
我有一個Web應用程序,該應用程序使用WCF終結點調用SharePoint 2007 Lists.asmx服務。 一整天的嘗試以確定為什么在調用GetListItems方法時為什么收到500響應,我發現在U2U Caml Query Builder中執行查詢后,它開始成功。 也就是說,執行U2U查詢后,Web應用程序將停止獲得500響應,並且可以使用其WCF服務引用成功檢索列表項,直到執行iisreset。
為了響應這一發現,我創建了一個控制台應用程序,該應用程序通過WCF服務參考對GetListItems進行了簡單的調用。 我將其范圍縮小為WCF服務引用的調用僅在從舊版.net 2.0 Web參考代理進行調用之后才有效。 這是我的控制台應用程序(我已經在Web服務器上的VS中運行)的示例代碼:
string listName = "Reusable Content";
string viewName = String.Empty;
string rowLimit = null;
//Always Works
XmlDocument doc = new XmlDocument();
ListsAsmx.Lists oldSvc = new ListsAsmx.Lists();
oldSvc.Credentials = new System.Net.NetworkCredential("user1", "passwrod", "mydomain");
XmlNode result = oldSvc.GetListItems(listName, viewName, doc.CreateElement("Query"), doc.CreateElement("ViewFields"), rowLimit, doc.CreateElement("QueryOptions"), null);
//Only works after the above succeeds
string query = "<Query></Query>";
string viewFields = "<ViewFields></ViewFields>";
string queryOptions = "<QueryOptions></QueryOptions>";
ListsService.ListsSoapClient svc = new ListsService.ListsSoapClient();
svc.ClientCredentials.Windows.ClientCredential.UserName = "user1";
svc.ClientCredentials.Windows.ClientCredential.Domain = "passwrod";
svc.ClientCredentials.Windows.ClientCredential.Password = "mydomain";
svc.GetListItems(listName, viewName, XElement.Parse(query), XElement.Parse(viewFields), rowLimit, XElement.Parse(queryOptions), null);
因此,如果我注釋掉sv2.GetListItems(...)並進行iisreset,我將從svc.GetListItems(...)獲得500響應。 有趣的是,Web Reference調用的來源無關緊要。 當我使用U2U Caml Query Builder發現它時,U2U起源於我的本地開發機,而WCF客戶端調用起源於Web服務器。 即使我沒有顯式設置svc的憑據,上面的代碼也成功了(大概是因為它是自動使用用戶的憑據)。 下面是ListsSoapClient的配置。 您能幫助我了解這里發生了什么以及如何使svc獨自成功嗎?
<binding name="ListsSoap2" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="500000" maxBufferPoolSize="524288" maxReceivedMessageSize="500000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="5120000" maxNameTableCharCount="16384" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://devpladmin.mydomain.com/_vti_bin/Lists.asmx"
binding="basicHttpBinding" bindingConfiguration="ListsSoap2"
contract="ListsService.ListsSoap" name="ListsSoap" />
</client>
編輯
附加信息:
當我收到500時,這會記錄在事件查看器中:
Exception information:
Exception type: FileLoadException
Exception message: Could not load file or assembly 'Microsoft.SharePoint.intl, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)
Request information:
Request URL: https://devpladmin.mydomain.com:443/_vti_bin/Lists.asmx
Request path: /_vti_bin/Lists.asmx
Thread information:
Thread ID: 10
Thread account name: MYDOMAIN\user1
Is impersonating: False
Stack trace: at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at Microsoft.SharePoint.CoreResource..cctor()
由於事件查看器和Jerry Orman的博客( SharePoint的“白屏”之謎)中的錯誤揭示了錯誤,我已經解決了此問題。 簡而言之,我需要向WCF客戶端端點添加一個行為,以允許服務器模擬給定的用戶:
<behaviors>
<endpointBehaviors>
<behavior name="SPServiceBehavior">
<clientCredentials>
<windows allowedImpersonationLevel="Impersonation" allowNtlm="True"/>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
因此,我最好的猜測是:Lists.asmx服務使用Microsoft.SharePoint.intl.dll。 加載Microsoft.SharePoint.intl.dll需要模擬授權用戶(可能是因為未將其標記為安全控件?我對此一無所知)。 重置IIS后,不加載程序集。 如果我使用WCF代理調用Lists.asmx服務,但未顯式指定Impersonation
的allowedImpersonationLevel
,則默認為Identification
並在嘗試加載Microsoft.SharePoint.intl.dll時從事件查看器給出上述模擬錯誤。 Lists服務會處理該異常,並以500響應進行響應,以確保您作為Web服務使用者,不知道問題實際上可能是什么**。 但是,如果我首先使用.NET 2.0 Web參考代理調用該服務,則顯然是(這是我的假設)導致服務器模擬給定的用戶,並且Microsoft.SharePoint.intl.dll程序集加載。 現在,如果我使用WCF代理,則程序集已經加載,並且不需要模擬。 這種解釋可能不太正確,但是添加上述行為可以使我的WCF服務參考起作用。
約翰·桑德斯(John Saunders),感謝您的努力。 謝謝!
**來吧...我無法經歷所有這些,並且至少不能一次在SharePoint上亂糟糟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.