WCF-005: About WCF basic Connection closed connection accidentally closed-not a problem with using parent class to point to subclass

Source: Internet
Author: User

The basic connection has been closed and the connection is accidentally closed.

I met it again.

In this scenario, a field in the database table indicates the product, but the type of the product is different, that is, the attributes are different. For ease of management, this field is used. The clob type is used to save a json string of the corresponding product class. You may be familiar with json usage.

So I plan to write these methods on the server.

Product Model:

This class indicates this common field,

[DataContract]  public class AbstractModel  {      private string id;      [DataMember]      public string ID      {          get { return id; }          set { id = value; }      }  }

This class indicates the structure of Product 1,

[DataContract]    public class ChildModel1:AbstractModel    {        private string child1ID;        private string child1ProductName;        [DataMember]        public string Child1ID        {            get { return child1ID; }            set { child1ID = value; }        }        [DataMember]        public string Child1ProductName        {            get { return child1ProductName; }            set { child1ProductName = value; }        }    }

This class indicates the structure of product 2,

[DataContract]

Public class ChildModel2

{

Private string child2ID;


Private string child2ProductName;


[DataMember]

Public string Child2ID

{

Get {return child2ID ;}


Set {child2ID = value ;}

}


[DataMember]

Public string Child2ProductName

{

Get {return child2ProductName ;}


Set {child2ProductName = value ;}

}

}


The service method is as follows,

Contract interface,

[ServiceContract] public interface IService1 {// This method is used to allow the client to access the ChildModel1 structure [OperationContract] ChildModel1 DoGetChild1Model (); [OperationContract] ChildModel2 DoGetChild2Model (); // This method is used to allow AbstractModel to represent different product models [OperationContract] AbstractModel DoGetProduct ();}

Implementation,

Public class Service1: IService1 {public ChildModel1 DoGetChild1Model () {ChildModel1 child1 = new ChildModel1 (); child1.Child1ID = "child1"; child1.Child1ProductName = "Product 1"; child1 ;} public ChildModel2 DoGetChild2Model () {ChildModel2 child2 = new ChildModel2 (); child2.Child2ID = "child2"; child2.Child2ProductName = "product 2"; return child2;} public login actmodel DoGetProduct () {ChildModel1 child1 = new ChildModel1 (); child1.Child1ID = "child1"; child1.Child1ProductName = "Product 1"; AbstractModel abmodel = child1; return abmodel ;}}

Create a wcf application program to run. The page of the WCF debugging tool is displayed.

First, run the methods to obtain product 1 and product 2 respectively.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1242302339-0.jpg "style =" float: none; "title =" wcf001.jpg "alt =" 002114864.jpg"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1242302207-1.jpg "style =" float: none; "title =" wcf002.jpg "alt =" 002124981.jpg"/>

Are correct.

Run the method of getting product 1 directed to by the parent class,

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/124230DS-2.jpg "title =" wcf003.jpg "alt =" 002213397.jpg"/>

Error


Therefore, this method does not work. If you want to obtain two different products, you need to define two corresponding product fields and assign values respectively.

My understanding is that there is still an error in serialization. the serialization should be a serialized subclass, but the restoration is based on the structure of the parent class. Therefore, an exception occurs and deserialization fails, the connection is disconnected.

Download test code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.