为什么我会收到SOAP异常?

4

我正在尝试将附件上传到列表中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication3.TestReference;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcUrl = @"C:......comp_name.xlsx";
            FileStream fStream = File.OpenRead(srcUrl);
            string fileName = fStream.Name.Substring(3);
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();

            ServiceWebReference.Lists listService = new ServiceWebReference.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            try
            {
                // adding attachment
                string result = listService.AddAttachment("testList", "1", fileName, contents);
                Console.WriteLine(result);
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {
                Console.WriteLine(e.GetBaseException());
                Console.WriteLine(e);
            }
        }
    }
}

我遇到了未处理的SOAP异常...
以下是我收到的完整异常信息:

System.Web.Services.Protocols.SoapException: Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
  at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
  at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
  at ConsoleApplication3.ServiceWebReference.Lists.AddAttachment(String listName, String listItemID, String fileName, Byte[] attachment) 
    in z:\Xxxxl\TestC\ConsoleApplication3\ConsoleApplication3\Web References\ServiceWebReference\Reference.cs:line 782
  at ConsoleApplication3.Program.Main(String[] args) 
    in z:\Xxxxl\TestC\ConsoleApplication3\ConsoleApplication3\Program.cs:line 29

Press any key to continue . . . 

我正确添加了引用:http ..... /_vti_bin/lists.asmx
我该如何进行调试?在我的情况下,SOAP异常是什么?

1
你必须告诉我们什么是 SoapException。完整的异常信息是什么?请在你的问题中发布它。 - John Saunders
@John Saunders 谢谢您的回复。我已经发布了异常信息。 - Azamat Bagatov
1
我很惊讶它没有说更多,但是SoapException表示服务器返回了一个错误。它可能已经返回了一个“SOAP Fault”,其中可能包含了其他信息。你应该这样做:try {...你的代码...} catch (SoapException ex){...在这里你可以查看异常的详细信息...} - John Saunders
尝试使用 catch (System.Web.Services.Protocols.SoapException ex) - John Saunders
不需要。只需使用 Console.WriteLine(e); 即可。您可能还想在 Console.WriteLine 处设置断点,并在调试器中查看 e 的内容。 - John Saunders
显示剩余2条评论
1个回答

1

为什么要使用catch (System.Web.Services.Protocols.SoapException e)

1.使用ex代替e,因为e已经存在于EventArgs e
2.尝试使用catch (SoapException ex),这是更好的方式

所以,集中精力处理异常吧,希望这能帮助你。


谢谢 +1。如果我使用 catch (SoapException ex),那么 Visual Studio 会将其标记并且无法编译。我应该添加哪个参考库,以便它不会在 catch (SoapException ex) 下面显示红色下划线?John Saunders 建议我使用 catch (System.Web.Services.Protocols.SoapException e) - Azamat Bagatov
你应该浏览并添加 System.Web.Protocols 库,然后在 catch (SoapException ex) 中,你可以添加引用,将其作为你的类库添加到顶部。 - Yogibear

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接