Use svctraceviewer to debug a WCF exception

Source: Internet
Author: User

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

 

  1. [Servicecontract]
  2. Public interface iservice1
  3. {
  4. [Operationcontract]
  5. String getdata (INT value );
  6. [Operationcontract]
  7. List <compositetype> getdatausingdatacontract (compositetype composite );
  8. // Todo: add your service operations here
  9. }
  10. // Use a data contract as specified strated in the sample below to add composite types to service operations
  11. [Datacontract]
  12. Public class compositetype
  13. {
  14. Bool boolvalue = true;
  15. String stringvalue = "hello ";
  16. [Datamember]
  17. Public bool boolvalue
  18. {
  19. Get {return boolvalue ;}
  20. Set {boolvalue = value ;}
  21. }
  22. [Datamember]
  23. Public String stringvalue
  24. {
  25. Get {return stringvalue ;}
  26. Set {stringvalue = value ;}
  27. }
  28. }
  29. Public class service1: iservice1
  30. {
  31. Public String getdata (INT value)
  32. {
  33. Return string. Format ("You entered: {0}", value );
  34. }
  35. Public list <compositetype> getdatausingdatacontract (compositetype composite)
  36. {
  37. List <compositetype> composites = new list <compositetype> ();
  38. For (INT I = 0; I <10; I ++)
  39. {
  40. Compositetype comp = new compositetype ();
  41. Comp. boolvalue = Composite. boolvalue;
  42. Comp. stringvalue = Composite. stringvalue + I. tostring ();
  43. Composites. Add (COMP );
  44. }
  45. Return composites;
  46. }
  47. }

Then we add a console program, add a service reference, and then add the following code

 

 

  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. Service1client client = new service1client ();
  6. Compositetype ctype = new compositetype ();
  7. Ctype. boolvalue = true;
  8. Ctype. stringvalue = "Justin ";
  9. Compositetype [] composites = client. getdatausingdatacontract (ctype );
  10. Foreach (compositetype ctype in composites)
  11. {
  12. Console. writeline (ctype. stringvalue );
  13. }
  14. Console. Readline ();
  15. }
  16. }

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.

 

  1. For (INT I = 0; I <1000; I ++)
  2. {
  3. Compositetype comp = new compositetype ();
  4. Comp. boolvalue = Composite. boolvalue;
  5. Comp. stringvalue = Composite. stringvalue + I. tostring ();
  6. Composites. Add (COMP );
  7. }

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.

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.