为什么我会得到Soap异常?

时间:2014-02-02 22:41:54

标签: c# sharepoint soap

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

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);
            }
        }
    }
}

我得到Unhandled SOAP exception ... 以下是我得到的完整例外:

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 个答案:

答案 0 :(得分:1)

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

1.使用ex而不是e,因为e已经存在为EventArgs e
2.试试catch (SoapException ex)这是一种更好的方式

所以,专注于异常处理伙伴, 我希望这可以帮助你

此致