0

I'm using a sample SOAP code generated by wsdl.exe. The object lastError is declared like this:

private Exception lastError;

Visual Studio is giving error on build on this line

String msg = lastError.Message;

saying

'Exception' does not contain a definition for 'Message' and no extension method 'Message' accepting a first argument of type 'Exception' could be found (are you missing a using directive or an assembly reference?) :

The Exception class generated by wsdl.exe looks like this:

/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(NestedException))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(PersistenceException))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(BbSecurityException))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://gradebook.ws.blackboard")]
public partial class Exception {

    private object exception1Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Exception", IsNullable=true)]
    public object Exception1 {
        get {
            return this.exception1Field;
        }
        set {
            this.exception1Field = value;
        }
    }
}
joym8
  • 4,014
  • 3
  • 50
  • 93
  • 1
    What is the question? The `Exception` class (no relation to `System.Exception` by the way) does not have a property for `Message`... – Ron Beyer Dec 15 '15 at 20:39
  • I understand Exception class being generated by wsdl does not have a Message property. But I thought it had some relation to System.Exception class so it was inheriting that property? – joym8 Dec 15 '15 at 20:43
  • 1
    No, as you can see from the definition, there is no inheritance to `System.Exception`. Unless you see it explicitly extend the class it won't, it doesn't extend it just because its `partial` with the same name. – Ron Beyer Dec 15 '15 at 20:44
  • " it doesn't extend it just because its partial" that answers my question, thanks! – joym8 Dec 15 '15 at 20:45

1 Answers1

0

partial classes don't automatically extend any built-in or non-partial classes by virtue of a shared name. If you want the above Exception class to extend System.Exception, then the easiest way to do that is to add another partial class and extend it explicitly:

(In some other file)

public partial class Exception : System.Exception
{

}

You should be aware of issues having a class named Exception though. Technically you shouldn't really catch the generic Exception, but if you had something like this in your namespace, you may not be catching the exception you think you are:

public void SomeMethod()
{
    try
    {
        DoSomethingThatExcepts();
    }
    catch (Exception e)
    {
        //You are actually catching the defined Exception, not System.Exception
    }
}

So anywhere you use System.Exception you may have to fully qualify the name.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Will use `System.Exception` to be sure. Can't get rid of the custom Exception class generated by wsdl – joym8 Dec 15 '15 at 21:37