Exception and error resolution caused by serialization issues in WCF

Source: Internet
Author: User

This period of time has been busy with projects, consisting of several layers of DAL + WCF + WinForm. MySQL + MySQL. Data. dll Driver used by the database. However, the following errors are often encountered during use:
An error occurred while receiving the http Response to HTTP: // localhost/***. svc. This may be caused by binding the service endpoint without using the HTTP protocol. This may also be caused by the server suspending the HTTP request context (possibly because the service is disabled. For more information, see server logs.
Sometimes the following error occurs:
The basic connection is closed: the connection is accidentally closed.
After careful check, it is found that this error is reported every time a MySql. Data. MySqlClient. MySqlException exception occurs. After searching for the Internet for half a day, I did not find a solution. Later I used Trace Viewer to find out that the problem occurred during MySqlException serialization:
System. ServiceModel. CommunicationException: An error occurred while trying to serialize the parameter http://tempuri.org/: result. The InnerException message is "The data protocol name should not be" MySqlException: Signature ". Consider using DataContractResolver or adding any unknown type to a list of known types statically. For example, you can use the KnownTypeAttribute feature or add the unknown type to the list of known types passed to DataContractSerializer .". For more information, see InnerException. System. Runtime. Serialization. SerializationException :.......
Result is a class instance that stores operation results. It spans the DAL, WCF, and WinForm layers to store exceptions and information found in a complete operation. The possible exceptions are encapsulated. Its structure is as follows (other attributes and constructors are not listed ):
[DataContract]
Public class Result
{
Private bool success;
 
[DataMember]
Public boll Success
{
Get {return this. success ;}
Set {this. success = value ;}
}
 
Private Exception exception;

[DataMember]
Publice Exception
{
Get {return this. exception ;}
Set {this. exception = value ;}
}
}
When an error occurs during execution of SQL statements in the DAL layer, the result. Exception type is actually MySqlException. Although [DataContract] has been declared during the Result class definition, there is still an error during serialization, which is a headache! View the definition of MySqlException as follows:
[Serializable]
Public sealed class MySqlException: DbException
{
Public int Number {get ;}
}
You have already declared the [Serializable] Feature and checked MySQL. data. dll file. It was found that serialization is supported in MySQL 0.7, but it still cannot be serialized in WCF. You can only find the reason.
According to the error message, the [ServiceKnownType (typeof (MySql. Data. MySqlClient. MySqlException)] statement is added to the contract declaration interface of IService, and the result does not work in the end. However, if the exception is not of the MySqlException type, the serialization error is not reported.
Compared with the implementation of Exception and MySqlException, it is found that although the [Serializable] feature is declared, MySqlException does not implement serialization or deserialization functions. The problem is likely to occur here.
However, because the source code of MySQL. Data. dll is not available, you cannot add these two functions. Therefore, you have to create a new exception class CustomException and store it in Result. The Code is as follows:
[Serializable]
Public class CustomException: ISerializable, Exception
{
Private string message;
 
Public override string Message
{
Get
{
Return this. message;
}
}
 
Private string stackTrace;
 
Public override string StackTrace
{
Get
{
Return this. stackTrace;
}
}
 
Public CustomException (){}
 
Public CustomException (string exceptionMessage, string exceptionStackTrace)
{
This. message = exceptionMessage;
This. stackTrace = exceptionStackTrace;
}
/*
In the following serialization and deserialization, only Message and StackTrace are saved.
*/
Protected CustomException (SerializationInfo, StreamingContext context)
{
This. message = info. GetString ("Message ");
This. stackTrace = info. GetString ("StackTraceString ");
}
 
Public override void GetObjectData (SerializationInfo info, StreamingContext context)
{
Info. AddValue ("Message", this. message );
Info. AddValue ("StackTraceString", this. stackTrace );
}
 
Public override string ToString ()
{
If (string. IsNullOrEmpty (this. message ))
Return string. Empty;
 
Return this. message. ToString ();
}
}
The reason why override defines Message and StackTrace is to rewrite them, because the StackTrace defined in the Exception class is read-only, and the Message can only be assigned a value in the constructor. In addition, only these two attributes are defined here, because only the two attributes in Exception are used in the project. In general, these two attributes are enough to find the Exception information.
Of course, the most important thing is to implement a constructor for serialization and set the substitution value of the serialized object using the GetObjectData method.
 
When MySqlException is generated, the Result object can be normally run from DAL> WCF> WinForm. OK!
 
Summary: this problem has indeed plagued me for several days. Since it was the first time that I used it in a project, I didn't know much about it. If you do not see the use of Trace Viewer, you will not find the essence of the problem. It seems that the exception prompts under. NET are sometimes not necessarily the problem generation points. Just like compiling code, there are only a few commas, but the prompt information is varied.
 
---------------------------------------------
Author: Ritchie (qigo)

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.