Solution to a common error in a WCF project: the basic connection is closed: the connection is accidentally closed.

Source: Internet
Author: User

When developing a WCF project, we often encounter some inexplicable errors. Sometimes, based on the error message, it is difficult to locate the specific problem, due to the special nature of the WCF Service, debugging is not so convenient, so it often takes a lot of time to track and process. This article describes how to handle a common error that occurs when I use the WCF Service in my framework. The prompt message is:The basic connection is closed: the connection is accidentally closed.. In this case, I have two types of errors: one is when the DataTable is returned, and the other is when the object class contains an enumeration type, the following describes how to process them.

1. The returned value of DataTable is incorrect.

Generally, when designing the return value of the WCF interface, we use the object class or object class list method. However, sometimes we may need to return the Data Type of a DataTable, if the function content is constructed and returned in the following way, an error may occur.

Interface Definition

/// <Summary> /// Based on the query conditions, returns the record to the able set /// </summary> /// <param name = "condition"> query condition </param> /// <param name = "pagerInfo"> paging condition </param> /// <returns> </returns> [OperationContract] DataTable FindToDataTableWithPager (string condition, ref PagerInfo pagerInfo );

To facilitate the demonstration, complete the simple test code.

/// <Summary> /// query the database based on the condition and return the object set (for paging data display) /// </summary> /// <param name = "condition"> query condition </param> /// <param name = "info"> paging object </ param> // <returns> set of specified objects </returns> public DataTable FindToDataTableWithPager (string condition, ref PagerInfo pagerInfo) {// test code DataTable dt = new DataTable (); dt. columns. add ("Test"); for (int I = 0; I <10; I ++) {dt. rows. add (new object [] {"Test" + I}) ;}return dt ;}

An error occurs during the test.

If you follow the prompts, you may not know what the problem is. This is actually very interesting because no TableName occurs when the serial number DataTable, in the implementation of the function body, assign a value to the TableName attribute of the returned DataTable object.

/// <Summary> /// query the database based on the condition and return the object set (for paging data display) /// </summary> /// <param name = "condition"> query condition </param> /// <param name = "info"> paging object </ param> // <returns> set of specified objects </returns> public DataTable FindToDataTableWithPager (string condition, ref PagerInfo pagerInfo) {// test code DataTable dt = new DataTable (); dt. columns. add ("Test"); for (int I = 0; I <10; I ++) {dt. rows. add (new object [] {"Test" + I});} dt. tableName = "test"; return dt ;}

After the test, we can see that it can be successfully executed.

 

2. Errors Caused by enumeration types in object classes

Object class definition, which defines an enumerated objectApplyStatus

/// <Summary> /// process application form /// </summary> [DataContract] public class ApplyInfo: BaseEntity {........ /// <summary> /// current status (0: Processing in progress, 1: Completed, 2: returned, 3: Undo) (other values are invalid) /// </summary> [DataMember] public virtualApplyStatusStatus {get {return this. m_Status;} set {this. m_Status = value ;}}}

The following code is usually used to define enumeration:

/// <Summary> /// the current processing status of the form. /// Current status (0: Processing in progress, 1: Completed, 2: returned, 3: Undo) (other values are invalid) /// </summary> [DataContract] public enum ApplyStatus {// <summary> // Processing = 0 /// </summary> Processing = 0, /// <summary> /// completed = 1 /// </summary> completed = 1, /// <summary> /// returned = 2 /// </summary> returned = 2, /// <summary> /// undo = 3 /// </summary> undo = 3}

Then define the WCF Service Interface and implementation code.

WCF Interface Definition

/// <Summary> /// query the database based on the condition and return the object set (for paging data display) /// </summary> /// <param name = "condition"> query condition </param> /// <param name = "info"> paging object </ param> // <returns> set of specified objects </returns> [OperationContract] List <T> FindWithPager (string condition, ref PagerInfo info );

WCF Service Interface implementation

/// <Summary> /// query the database based on the condition and return the object set (for paging data display) /// </summary> /// <param name = "condition"> query condition </param> /// <param name = "info"> paging object </ param> // <returns> set of specified objects </returns> public List <ApplyInfo> FindWithPager (string condition, ref PagerInfo info) {return bll. findWithPager (condition, info );}

The following error occurs when you obtain the object information through the WCF Service:The basic connection is closed: the connection is accidentally closed.

 

The cause of the error is that the [EnumMember] Declaration must be added to the enumerated value due to an error in Enumeration type serialization, as shown below.

[DataContract] public enum ApplyStatus {// <summary> /// Processing = 0 /// </summary> [EnumMember] Processing = 0, /// <summary> /// completed = 1 /// </summary> [EnumMember] completed = 1, /// <summary> /// returned = 2 /// </summary> [EnumMember] returned = 2, /// <summary> /// undo = 3 /// </summary> [EnumMember] Undo = 3}

Compile and run the program again and test it. The final result is the parsing result.

These are some of the solutions that we often encounter when developing WCF. I hope you will encounter similar problems when developing and calling WCF, which can solve the problem well, save valuable time.

 

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.