Azamat Bagatov
Azamat Bagatov

Reputation: 289

Why do I get Soap exception?

I am trying to upload an attachment to a list:

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

I get Unhandled SOAP exception ....
Here is the full exception that I got:

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 . . . 

I added the reference correctly: http ..... /_vti_bin/lists.asmx

How can I debug it? What is the SOAP exception in my case?

Upvotes: 4

Views: 3301

Answers (1)

Yogibear
Yogibear

Reputation: 153

Why are you using catch (System.Web.Services.Protocols.SoapException e)

1.Use ex instead e, because e already exists as EventArgs e
2.Try catch (SoapException ex) It's a better way

So, concentrete to the Exception handling buddy, I hope this helps you

Upvotes: 1

Related Questions