From: http://blog.csdn.net/zzqqqzzz/article/details/3159680
When using WCF to develop an application system, we often encounter connection errors. At this time, we can actually let the WCF record the log, and then we can use the svctraceviewer tool to analyze the log and find the problem. Here, I will show you an instance.
First, create a WCF Service. Here is the server code
- [Servicecontract]
- Public interface iservice1
- {
- [Operationcontract]
- String getdata (INT value );
- [Operationcontract]
- List <compositetype> getdatausingdatacontract (compositetype composite );
- // Todo: add your service operations here
- }
- // Use a data contract as specified strated in the sample below to add composite types to service operations
- [Datacontract]
- Public class compositetype
- {
- Bool boolvalue = true;
- String stringvalue = "hello ";
- [Datamember]
- Public bool boolvalue
- {
- Get {return boolvalue ;}
- Set {boolvalue = value ;}
- }
- [Datamember]
- Public String stringvalue
- {
- Get {return stringvalue ;}
- Set {stringvalue = value ;}
- }
- }
- Public class service1: iservice1
- {
- Public String getdata (INT value)
- {
- Return string. Format ("You entered: {0}", value );
- }
- Public list <compositetype> getdatausingdatacontract (compositetype composite)
- {
- List <compositetype> composites = new list <compositetype> ();
- For (INT I = 0; I <10; I ++)
- {
- Compositetype comp = new compositetype ();
- Comp. boolvalue = Composite. boolvalue;
- Comp. stringvalue = Composite. stringvalue + I. tostring ();
- Composites. Add (COMP );
- }
- Return composites;
- }
- }
Then we add a console program, add a service reference, and then add the following code
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Service1client client = new service1client ();
- Compositetype ctype = new compositetype ();
- Ctype. boolvalue = true;
- Ctype. stringvalue = "Justin ";
- Compositetype [] composites = client. getdatausingdatacontract (ctype );
- Foreach (compositetype ctype in composites)
- {
- Console. writeline (ctype. stringvalue );
- }
- Console. Readline ();
- }
- }
We can run the program to get the output as follows:
Justin 0
Justin 1
Justin 2
Justin 3
Justin 4
Justin 5
Justin 6
Justin 7
Justin 8
Justin 9
At this time, we can enlarge this loop.
- For (INT I = 0; I <1000; I ++)
- {
- Compositetype comp = new compositetype ();
- Comp. boolvalue = Composite. boolvalue;
- Comp. stringvalue = Composite. stringvalue + I. tostring ();
- Composites. Add (COMP );
- }
Run the program again and we can see this exception:
System. servicemodel. communicationexception: {"the maximum Message Size quota for incoming messages (65536) has been exceeded. To increase the quota, use the maxcompute edmessagesize property on the appropriate binding element ."}
This exception provides comprehensive information. You can modify the configuration file and set maxcompute edmessagesize to a larger value.
[Svcconfigeditor]
(* Both the server and client must be modified)
Then run the command again to see the result.
Justin 0
Justin 1
Justin 2
Justin 3
Justin 4
Justin 5
...
Justin 998
Justin 999
You may be surprised that svctraceviewer is not used here. It doesn't matter. Let's add the loop to 10000.
Another problem occurs. The exception content is as follows:
{"An error occurred while loading the HTTP Response to http: // localhost: 8731/design_time_addresses/mysampleservice/service1 /. this cocould be due to the service endpoint binding not using the HTTP protocol. this coshould also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down ). see server logs for more details. "}
It seems that there is not much information. But the exception mentioned the log, we will record some logs. Svcconfigeditor allows you to easily set logs.
After adding the log, run the program again. If the error persists, stop Debug. Use traceviewer to open the log.
At this time, we can see that there are two exceptions on the server. We choose one of them,
At this time, we can see that there are very detailed exceptions:
There was an error while trying to serialize parameter http://tempuri.org/:GetDataUsingDataContractResult. the innerexception message was 'maximum number of items that can be serialized or deserialized in an object graph is '20140901 '. change the object graph or increase the maxitemsinobjectgraph quota. '. please see innerexception for more details.
It turns out that there is a problem with serialization. We need to modify maxitemsinobjectgraph.
We need to add a datacontractserializer for the servicebehavior of the current service, and set maxitemsinobjectgraph to a larger value. (The client also needs to be modified ).
Run the program again. Everything is normal.
In the development of WCF, traceviewer can always give you unexpected information. It is a very short and elegant tool.