积极答复者
如何对RESTful的自动序列化进行debug?

问题
-
大家好
我最近在做一个实施RESTful WCF的项目,在向mono中部署服务器的时候,发现了mono兼容性的问题:如果POST时没有写ContentType,就会出现致命错误,进而自行关闭
Unhandled Exception: System.ArgumentNullException: Argument cannot be null. Parameter name:contentType.
我想处理这个Exception,但奇怪的是无论我对Service.foo 还是对Host本身的代码(自承载的命令行)进行catch都没法捕获它。
虽然接收的参数是JSON的,但是我是在WebInvoke里设置它的RequestFormat来自动序列/反序列化,没有手动实现/绑定JsonMapper.
所以请问一下,如果要对自动序列化过程中遇到的Exception进行捕获的话,应该怎么做?谢谢!
------------------------------------------------------------------------------------------------------------------------------
Interface:
[OperationContract]
[WebInvoke(UriTemplate="Echo", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
string Echo(string name, string say);APP.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Contracts.MyService" behaviorConfiguration="MEXBehavior">
<endpoint address="" binding="webHttpBinding" contract="Contracts.IMyService" behaviorConfiguration="MyRestBehavior" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:18688/MyService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyRestBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MEXBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup></configuration>
Host
static void Main(string[] args)
{
try
{
ServiceHost _host = new ServiceHost(typeof(Contracts.MyService));
_host.Open();
DisplayHostInfo(_host);
while (true)
{
Console.ReadLine();
}
}
catch (System.ArgumentNullException e)
{
Console.WriteLine("I Catch A Exception!");
throw e;
}
}Implementation
public string Echo(string name,string say)
{
try
{
return name + " says: " + say;
}
catch (ArgumentNullException e)
{
Console.WriteLine(e);
throw;
}
}
答案
全部回复
-
没部署过Mono WCF服务,之前查看官方文档看很多特性不支持。
Frank Xu Lei--谦卑若愚,好学若饥
[老徐的网站]:http://www.frankxulei.com/[老徐的博客]:http://54peixun.com/Author/frankxulei
微软WCF中文技术论坛
微软WCF英文技术论坛
微软WCF技术群:166599314