views:

306

answers:

2

Ii have a web method that looks like this:

[WebMethod]
public int ImportData(System.Collections.Generic.List<Record> importRecords)
{
    int count = 0;
    if (importRecords != null && importRecords.Count > 0)
    {
        DataLayer datalayer = new DataLayer();
        foreach (Record rec in importRecords)
        {
            datalayer.InsertRecord(rec);
            count++;
        }
    }
    return count;
}

Will an exception be thrown back to the client who called the method if there is an exception thrown on the webservice method?

+2  A: 

Yes, but it will be a SoapException. If you get a NullReferenceException for example, you're not going to get a NullReferenceException on the client side.

There's a bit more information about webservices and exceptions on MSDN

The key piece here is this:

An ASP.NET client receives a SoapException with the details serialized into text in the Message property.

This applies to all exceptions that are not SoapExceptions. If you want to customise the exception in some way, you can create a SoapException with the appropriate detail, and throw that. Then ASP.NET will simply forward your exception to the client instead of wrapping it.

Nader Shirazie
So if somewhere within the data layer i said throw new SQLException("Error in accessing the database " + ex.ToString); The Exception on the client side would be a soapexception with the error message i provided?
zSysop
@zSysop - yes, you should get the message out of the exception properly. Just don't have any reliance on the "type" of the exception. If you want proper handling of exceptions, you have to start looking at error codes.
Nader Shirazie
+1  A: 

If an exception is thrown an not handled, .NET will convert the exception into a SOAP Fault and send it to the client.

Many clients (including .NET clients and Java clients) will translate SOAP Faults into exceptions on the client side. In the case of a .NET ASMX client, the exception thrown will be a SoapException. In the case of WCF, it will be a FaultException.

ASMX web services do not properly support SOAP Faults. This means that it's tedious to return specific SOAP Faults, that there is no way to cause the fault to show up in the WSDL, and that an ASMX client will only ever see SoapException, and not anything more specific.

This is one other reason to move to WCF as soon as possible, as it has full support for SOAP Faults. Of course the other reason is that Microsoft has declared that ASMX technology is "legacy technology" and that they will not be fixing bugs there.

John Saunders