我正在使用WCF服务4.5 F/W开发REST Full API.请建议如何向客户端发送异常或任何业务验证消息.我正在寻找非常通用的解决方案,请提出最佳方法.我尝试了下面的一些方法,
在向客户端发送响应之前设置OutgoingWebResponseContext.
定义的故障合同只能正常工作添加服务引用(代理类)不能在REST FULL环境中工作.
在catch块中添加WebFaultException类.
WCF休息入门套件 - 我收到了一些关于此的文章和帖子,但他们的codeplex官方建议不再提供此信息.链接.所以,我不想这样做.
这些没有按预期工作..我的示例代码片段:接口:
[OperationContract]
[FaultContract(typeof(MyExceptionContainer))]
[WebInvoke(Method = "GET", UriTemplate = "/Multiply/{num1}/{num2}", BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string Multiply(string num1, string num2);
Run Code Online (Sandbox Code Playgroud)
执行:
public string Multiply(string num1, string num2)
{
if (num1 == "0" || num2 == "0")
{
try
{
MyExceptionContainer exceptionDetails = new MyExceptionContainer();
exceptionDetails.Messsage = "Business Rule violatuion";
exceptionDetails.Description = "The numbers should be non zero to perform this operation";
//OutgoingWebResponseContext outgoingWebResponseContext = WebOperationContext.Current.OutgoingResponse; …Run Code Online (Sandbox Code Playgroud)