locked
Exception Handling Issue in WCF DataContract RRS feed

  • Question

  • User1663286328 posted


    Hi all:

    I have an interface (actually two interfaces, with one inheriting
    from the other, but that is not particularly relevant). The
    interface is for some extension methods, which operate on
    DataContract classes (as it is used in a WCF duplex operation).

    I had the idea of creating a base class that implements all
    of the interface elements, throwing an exception for anything
    and everything that is not overridden in a DataContract class
    that derives from the base class. This is all in a library that
    is referenced in common by both the client and server.

    I have a problem (which is why I'm postingWink). If I give the
    accessors within the base class the DataMember attribute,
    and it is not overridden in my derived class, the base accessor
    is called and the exception method is called when it shouldn't be.

    If I don't give them the DataMember attribute, then my communication
    gets messed up, and the callback isn't performed.

    Does anyone have an idea as to how I can get around this?

    EDIT: This is self-hosted, if it makes any difference.

    EDIT-02: I can set a boolean value to false in my base class,
    and use that to avert the call to my exception method:

    private void naughty (string p_method_name)
    {   if (exceptions_are_active)
           NI_XCPT.naughty (p_method_name);
    }

    This works, in that I'm getting normal functionality, but my
    exception thrower method is no longer active.

    What I need now is an event handler to turn the boolean
    switch on once the datacontract class has been loaded on
    the server. I'm just learning WCF, so to say that I'm a novice
    would be an understatement.

    Can anyone help me out on this?


    Thanks!

    //########################################################################
      //########################################################################
    
        public interface I21_DataService_XTN
        {
            int GS_client_ID { get; set; }
            int GS_remote_server_ID { get; set; }
            long GS_node_count { get; set; }
        }
      //########################################################################
      //########################################################################
    
        public interface I21_DataService_XTN2 : I21_DataService_XTN
        {
            string GS_user_ID { get ; set ; }
            string GS_remote_machine_name { get ; set ; }
            DateTime GS_Date_Time { get ; set ; }
            int GS_operation_code { get ; set ; }
        }
      //########################################################################
      //########################################################################
    
        [DataContract]
        public class DataService_XTN2_XCPT : object
        {
            // The method that throws the exception.
            private void naughty (string p_method_name)
     {
     if (exceptions_are_active)
     NI_XCPT.naughty (p_method_name);
     }
    protected bool exceptions_are_active;

    public DataService_XTN2_XCPT ()
    { exceptions_are_active = false; }


    // Now I need to call this from an event handler
    // of some sort.
    public void activate_exceptions ()
    { exceptions_are_active = true; }

    // Should I use this instead? //[System.Runtime.Serialization.DataMemberAttribute()] [DataMember] public virtual string GS_user_ID { get { naughty ("GS_user_ID(GET)"); return ""; } set { naughty ("GS_user_ID(SET)"); } } [DataMember] public virtual int GS_client_ID { get { naughty ("GS_client_ID(GET)"); return 0; } set { naughty ("GS_client_ID(SET)"); } } [DataMember] public virtual int GS_remote_server_ID { get { naughty ("GS_remote_server_ID(GET)"); return 0; } set { naughty ("GS_remote_server_ID(SET)"); } } [DataMember] public virtual string GS_remote_machine_name { get { naughty ("GS_remote_machine_name(GET)"); return ""; } set { naughty ("GS_remote_machine_name(SET)"); } } [DataMember] public virtual DateTime GS_Date_Time { get { naughty ("GS_Date_Time(GET)"); return new DateTime (); } set { naughty ("GS_Date_Time(SET)"); } } [DataMember] public virtual int GS_operation_code { get { naughty ("GS_operation_code(GET)"); return 0; } set { naughty ("GS_operation_code(SET)"); } } [DataMember] public virtual long GS_node_count //{get {return v_nc;} set {v_nc = value;} } { get { naughty ("GS_node_count(GET)"); return 0; } set { naughty ("GS_node_count(SET)"); } } } //######################################################################## //######################################################################## [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="VS_a_DataService", Namespace="http://schemas.datacontract.org/2004/07/WCF01.Services")] public partial class VS_a_DataService : System.Runtime.Serialization.IExtensibleDataObject { private System.Runtime.Serialization.ExtensionDataObject extensionDataField; public System.Runtime.Serialization.ExtensionDataObject ExtensionData { get { return this.extensionDataField; } set { this.extensionDataField = value; } } } //######################################################################## //######################################################################## public partial class VS_a_DataService : DataService_XTN2_XCPT, I21_DataService_XTN2 { private string v_user_ID; [DataMember] public override string GS_user_ID { get { return this.v_user_ID; } set { this.v_user_ID = value; } } private int v_client_ID; [DataMember] public override int GS_client_ID { get { return this.v_client_ID; } set { this.v_client_ID = value; } } private int v_remote_server_ID; [DataMember] public override int GS_remote_server_ID { get { return this.v_remote_server_ID; } set { this.v_remote_server_ID = value; } } private string v_remote_machine_name; [DataMember] public override string GS_remote_machine_name { get { return this.v_remote_machine_name; } set { this.v_remote_machine_name = value; } } private DateTime v_Date_Time; [DataMember] public override DateTime GS_Date_Time { get { return this.v_Date_Time; } set { this.v_Date_Time = value; } } } //###################################################################### //######################################################################

    Monday, March 18, 2013 7:17 PM

Answers

  • User1663286328 posted

    Hi all,

    I solved this issue by including DataMember accessors that
    turn my boolean switch on and off, then using the Order parameter
    on all of my DataElement members, making the accessor that
    turns the switch off the first in order, and the one that turns it
    back on the last.

    Now I have another issue ...

    Thanks for reading!



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2013 11:29 PM