使用svcTraceViewer来调试WCF异常
来源:互联网 发布:python 打印字符串截取 编辑:程序博客网 时间:2024/04/30 07:24
我们在使用WCF开发应用系统的时候,经常会遇到一些连接错误。这个时候,其实我们可以让WCF记录日志,然后我们用svcTraceViewer工具可以分析这些日志然后找到问题。在这里,我将给大家演示一个实例。
首先我们创建一个WCF服务,这里是服务端代码
- [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 illustrated 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;
- }
- }
然后我们添加一个控制台程序,添加一个服务引用,然后添加下面的代码
- 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();
- }
- }
我们运行程序,就可以得到这样的输出:
Justin 0
Justin 1
Justin 2
Justin 3
Justin 4
Justin 5
Justin 6
Justin 7
Justin 8
Justin 9
这时候,我们可以把这个循环放大一些
- for(int i=0;i<1000;i++)
- {
- CompositeType comp = new CompositeType();
- comp.BoolValue = composite.BoolValue;
- comp.StringValue = composite.StringValue + i.ToString();
- composites.Add(comp);
- }
再运行程序,我们可以看到这样的异常:
System.ServiceModel.CommunicationException: {"The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."}
这个异常提供的信息很全,我们可以修改一下配置文件,把MaxReceivedMessageSize设置成大一些的值。
[svcConfigEditor]
(*服务端和客户端两边都需要修改)
这样再运行一下,就可以看到结果了.
Justin 0
Justin 1
Justin 2
Justin 3
Justin 4
Justin 5
...
Justin 998
Justin 999
你可能奇怪,这里没有用到svcTraceViewer啊。不要紧,让我们把循环加到10000。
又出问题了,这次的异常内容是这样的:
{"An error occurred while receiving the HTTP response to http://localhost:8731/Design_Time_Addresses/MySampleService/Service1/. This could be due to the service endpoint binding not using the HTTP protocol. This could 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."}
好像没有太多信息啊。不过异常里面提到了log,我们就来记录一些日志。svcConfigEditor可以方便的设置日志。
添加完日志我们再运行一下程序,错误照旧,停止debug, 我们用TraceViewer打开一下日志看看。
这时候我们可以看到在服务端有2个异常,,我们选择其中一个,
这时候我们可以看到,这里有非常详细的异常:
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 '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '. Please see InnerException for more details.
原来是序列化出现了问题,我们需要把MaxItemsInObjectGraph给修改一下。
我们需要为当前服务的ServiceBehavior添加一个DataContractSerializer, 把其中的MaxItemsInObjectGraph设置更大的值。(客户端也需要修改)。
重新运行程序,一切正常。
在WCF开发中,TraceViewer总可以给你意想不到的信息,实在是短小精悍的好工具。
- 使用svcTraceViewer来调试WCF异常
- 使用svctraceviewer查看WCF服务异常信息
- WCF svctraceviewer.exe 工具的使用
- WCF svctraceviewer.exe 工具的使用
- 使用WCFTestClient工具来测试WCF Service
- 使用日志来调试
- 使用gdb调试异常
- WCF调试异常信息:ServiceHost 仅支持类服务类型
- 使用 WcFTestClient.exe 调试 wcf 服务
- 调试WCF
- WCF 调试
- 使用ThreadGroup来处理异常
- 使用slf4j来打印异常
- 使用Visual Studio 开发、调试WCF入门-1-零代码开发、调试WCF服务器端。HelloWorld
- 使用Log4J来进行调试
- 使用EditPlus来调试javascript
- 使用python来调试串口
- 使用python来调试串口
- 自学(网站制作,FLASH,PS,3D)者,一定要看。
- 丁磊自述 在浙江大学的演讲
- 害怕别人乱输数据吗?提高你的数据校验功能-AJAX输入校验(8)
- Ajax学习--输入前提示
- ATLAS自动完成功能
- 使用svcTraceViewer来调试WCF异常
- 学习C++:实践者的方法
- javascript清空网页代码防止查看源代码
- MVC开发模式图解
- 转载——bootloader 开发,最郁闷前三步。
- MyEclipse设置JSP页面默认编码方式以及设置在Java文件中作者、日期等说明
- 转载—— WinCE5.0 BSP BOOTLOADER开发详解-生成bin和nb0
- 语句
- 公布C#写的网游外挂源代码