WCF相关 求调用参数输入有误的情况下的异常捕获

百合杰 2018-04-12 11:08:11
本人现在wcf的程序接口是restful形式 输入输出格式为json格式
[WebInvoke(Method = "POST", UriTemplate = "vm_destroy", BodyStyle = WebMessageBodyStyle.WrappedRequest,  RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
VM_STARTSTOP VM_Destroy(VMTYPE vm_type,string user_name,string user_pwd,string vm_id);

正常调用参数比如
{
"vm_type":1,
"user_name":"xulei01",
"user_pwd":"123.com",
"vm_id":"f9de392e-5b0d-4fe4-ba2f-0ad804781fa8"
}

返回值是VM_STARTSTOP形式的 例如
{
"code": 40008,
"msg": "VMID与账号或密码或VM类型不匹配",
"success": false
}


但如果客户程序发给我的格式有误的情况下 比如
{
"vm_type":1,
"user_name":"xulei01",
"user_pwd":"123.com",
"vm_id"://这边故意改成错的
}

wcf就自己捕获异常并返回了 都没到我的代码层 返回结果如下(返回:反序列化时引发异常的html)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>请求错误</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">请求错误</p>
<p>服务器处理请求时遇到错误。异常消息为“格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: user_pwd。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。遇到意外字符“}”。”。有关详细信息,请参见 InnerException。”。有关详细信息,请参见服务器日志。异常堆栈跟踪为: </p>
<p> 在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part)
在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue)
在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest)
在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
在 System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
在 System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
在 System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
</div>
</body>
</html>

这样的话客户程序就不认识这个格式了
现在我想要捕获这种类似于反序列化的异常情况,并以我想要的格式(比如说VM_STARTSTOP结构的json格式)返回给客户端,请教怎么做?
...全文
632 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
Silverlight2.0功能展示Demo源码 1、Silverlight(3) - 2.0控件之Border, Button, Calendar, Canvas, CheckBox, ComboBox 介绍 Silverlight 2.0 控件一览:Border, Button, Calendar, Canvas, CheckBox, ComboBox 2、Silverlight(4) - 2.0控件之DataGrid, DatePicker, Grid, GridSplitter, HyperlinkButton, Image 介绍 Silverlight 2.0 控件一览:DataGrid, DatePicker, Grid, GridSplitter, HyperlinkButton, Image 3、Silverlight(5) - 2.0控件之ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton 介绍 Silverlight 2.0 控件一览:ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton 4、Silverlight(6) - 2.0控件之ScrollViewer, Slider, StackPanel, TabControl, TextBlock, TextBox, ToggleButton 介绍 Silverlight 2.0 控件一览:ScrollViewer, Slider, StackPanel, TabControl, TextBlock, TextBox, ToggleButton 5、Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle 介绍 Silverlight 2.0 图形: Ellipse - 椭圆 Line - 线 Path - 一系列相互连接的直线和曲线 Polygon - 多边形,闭合图形,起点与终点自动相连 Polyline - 非闭合图形,一串连接起来的线,起点与终点不会自动相连 Rectangle - 矩形 6、Silverlight(8) - 2.0图形之基类System.Windows.Shapes.Shape 介绍 Silverlight 2.0 图形:基类System.Windows.Shapes.Shape(Ellipse, Line, Path, Polygon, Polyline, Rectangle都继承自抽象类System.Windows.Shapes.Shape)。 Fill - 填充;Stroke - 笔画(边框) StrokeThickness - 笔画(边框)尺寸 Stretch - 拉伸值 StrokeDashArray - 虚线和间隙的值的集合 StrokeDashCap - 虚线两端(线帽)的类型 StrokeStartLineCap - 虚线起始端(线帽)的类型 StrokeEndLineCap - 虚线终结端(线帽)的类型 StrokeDashOffset - 虚线的起始位置。从虚线的起始端的 StrokeDashOffset 距离处开始描绘虚线 StrokeLineJoin - 图形连接点处的连接类型 StrokeMiterLimit - 斜接长度 与 StrokeThickness/2 的比值。默认值 10,最小值 1 7、Silverlight(9) - 2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush 介绍 Silverlight 2.0 画笔: SolidColorBrush - 单色画笔 ImageBrush - 图像画笔 VideoBrush - 视频画笔 LinearGradientBrush - 线性渐变画笔 RadialGradientBrush - 放射性渐变画笔 8、Silverlight(10) - 2.0其它之Transform详解,以及UIElement和FrameworkElement的常用属性 介绍 Silverlight 2.0 其它: RenderTransform - 呈现位置的转换(System.Windows.Media.Transform类型) RenderTransformOrigin - 位置转换的中心点 Clip - 容器的剪辑区域(System.Windows.Media.Geometry类型) IsHitTestVisible - 命中测试是否可见 Opacity - 不透明度。0 - 1之间 OpacityMask - 不透明蒙版(遮罩)(System.Windows.Media.Brush类型) UseLayoutRounding - 是否使用完整像素布局 Cursor - 鼠标移动到 FrameworkElement 上面时,鼠标指针的样式 Margin - 容器边缘与边缘之外的空白距离(像素值:上下左右;左右,上下;左,上,右,下) Tag - 保存一些额外的信息(System.Object类型) 9、Silverlight(11) - 2.0动画之ColorAnimation, DoubleAnimation, PointAnimation, 内插关键帧动画 介绍 Silverlight 2.0 动画: ColorAnimation - 在两个 Color 值之间做线性内插动画处理 DoubleAnimation - 在两个 Double 值之间做线性内插动画处理 PointAnimation - 在两个 Point 值之间做线性内插动画处理 内插关键帧动画 - 在 Color 或 Double 或 Point 动画中内插关键帧,以做线性, 离散, 三次贝塞尔曲线的动画处理 动态改变动画 - 通过程序控制,动态地改变动画 10、Silverlight(12) - 2.0外观之样式, 模板, 视觉状态和视觉状态管理器 介绍 Silverlight 2.0 外观控制:样式(Style), 模板(Template), 视觉状态(VisualState)和视觉状态管理器(VisualStateManager) 11、Silverlight(13) - 2.0交互之鼠标事件和键盘事件 介绍 Silverlight 2.0 人机交互:响应用户的鼠标操作和键盘操作。 MouseEnter - 鼠标进入时触发的事件(显然,此事件不能冒泡) MouseLeave - 鼠标离开时触发的事件(显然,此事件不能冒泡) MouseLeftButtonDown - 鼠标左键单击按下时触发的事件 MouseLeftButtonUp - 鼠标左键单击按下并放开时触发的事件 MouseMove - 鼠标移动时触发的事件 MouseEventArgs.GetPosition() - 鼠标相对于指定元素的坐标 MouseButtonEventArgs.Handled - 此事件是否已被处理 KeyDown - 鼠标按下时触发的事件 KeyUp - 鼠标按下并放开时触发的事件 KeyEventArgs.Key - 与事件相关的键盘的按键 [Input.Key枚举] KeyEventArgs.Handled - 是否处理过此事件 Keyboard.Modifiers - 当前按下的辅助键 [Input.ModifierKeys枚举] 12、Silverlight(14) - 2.0交互之InkPresenter(涂鸦板) 介绍 Silverlight 2.0 人机交互:InkPresenter(涂鸦板)。 InkPresenter - 涂鸦板,也就是在面板上呈现墨迹。InkPresenter 可以包含子控件 Cursor - 鼠标移动到 InkPresenter 上面时,鼠标指针的样式 Background - 涂鸦板背景 Opacity - 面板上墨迹的不透明度 Clip - InkPresenter 的剪辑区域 Stroke.DrawingAttributes - Stroke(笔划)的外观属性 UIElement.CaptureMouse() - 为 UIElement 对象启用鼠标捕捉 UIElement.CaptureMouse() - 为 UIElement 对象释放鼠标捕捉 13、Silverlight(15) - 2.0数据之一次绑定, 单向绑定, 双向绑定, INotifyPropertyChanged, 数据转换, 数据验证 介绍 Silverlight 2.0 数据绑定: Binding - 将绑定目标对象的属性与数据源联接起来 INotifyPropertyChanged - 向客户端发出某一属性值已更改的通知 IValueConverter - 值转换接口,将一个类型的值转换为另一个类型的值。它提供了一种将自定义逻辑应用于绑定的方式 BindingValidationError - 出现验证错误或解决上次验证错误则触发此事件 14、Silverlight(16) - 2.0数据之独立存储(Isolated Storage) 介绍 Silverlight 2.0 数据的独立存储(Isolated Storage): IsolatedStorageFile - 操作 独立存储 的类; IsolatedStorageFile.GetUserStoreForSite() - 按站点获取用户的独立存储 IsolatedStorageFile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储 IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表 IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表 IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表 15、Silverlight(17) - 2.0数据之详解DataGrid, 详解ListBox 介绍 Silverlight 2.0 详解DataGrid, 绑定数据到ListBox: AutoGenerateColumns - 是否根据数据源自动生成列 RowDetailsVisibilityMode - 显示相应的行的详细数据时所使用的显示模式 DataGrid.RowDetailsTemplate - 用于显示相应的行的详细数据的模板 AreRowDetailsFrozen - 是否冻结 RowDetailsTemplate GridLinesVisibility - 表格分隔线的显示方式 RowBackground - 奇数数据行背景 AlternatingRowBackground - 偶数数据行背景 IsReadOnly - 单元格是否只读 FrozenColumnCount - 表格所冻结的列的总数(从左边开始数) SelectionMode - 行的选中模式 CanUserReorderColumns - 是否允许拖动列 CanUserResizeColumns - 是否允许改变列的宽度 CanUserSortColumns - 是否允许列的排序 16、Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器 介绍 Silverlight 2.0 详解MediaElement:开发一个简易版的全功能播放器。 Source - 需要播放的媒体地址 Stretch - 拉伸值 AutoPlay - 是否自动播放媒体 CurrentState - 播放状态 Position - 媒体的位置 DroppedFramesPerSecond - 媒体每秒正在丢弃的帧数 BufferingProgress - 缓冲进度 DownloadProgress - 下载进度 NaturalDuration - 媒体文件的时长; Volume - 音量大小 Balance - 音量平衡 BufferingTime - 需要缓冲的时间的长度 CurrentState - 播放状态 IsMuted - 是否静音 Play() - 播放媒体 Pause() - 暂停媒体的播放 Stop() - 停止媒体的播放 17、Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据 介绍 Silverlight 2.0 调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据。 通过 System.Net.WebClient 类调用 REST 服务 通过 System.Json 命名控件下的类处理 JSON 数据 通过 System.Xml.Linq 命名空间下的类(LINQ to XML)处理 XML 数据 通过 System.ServiceModel.Syndication 命名空间下的类处理 RSS/ATOM 数据 18、Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据, 以流的方式上传/下载数据 介绍 Silverlight 2.0 详解WebClient,以字符串的形式上传、下载数据;以流的方式上传、下载数据。 WebClient - 将数据发送到指定的 URI,或者从指定的 URI 接收数据的类 DownloadStringAsync(Uri address, Object userToken) - 以字符串的形式下载指定的 URI 的资源 UploadStringAsync(Uri address, string data) - 以字符串的形式上传数据到指定的 URI。所使用的 HTTP 方法默认为 POST OpenReadAsync(Uri address, Object userToken) - 以流的形式下载指定的 URI 的资源 OpenWriteAsync(Uri address, string method, Object userToken) - 打开流以使用指定的方法向指定的 URI 写入数据 19、Silverlight(21) - 2.0通信之WebRequest和WebResponse, 对指定的URI发出请以及接收响应 介绍 Silverlight 2.0 详解WebRequest和WebResponse,对指定的URI做GET和POST请,以及接收其响应。 HttpWebRequest - 对指定的 URI 发出请 Create() - 初始化一个 WebRequest BeginGetResponse() - 开始对指定 URI 资源做异步请 EndGetResponse() - 结束对指定 URI 资源做异步请 HttpWebResponse - 对指定的 URI 做出响应 GetResponseStream() - 获取响应的数据流 20、Silverlight(22) - 2.0通信之调用WCF服务, 对传输信息做加密 介绍 Silverlight 2.0 调用 WCF 服务,对客户端与服务端传输的消息做加密。在 Visual Studio 2008 中使用"添加服务引用"会自动生成代理类。只支持BasicHttpBinding 21、Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service) 介绍 Silverlight 2.0 调用 WCF 的双向通信服务(Duplex Service) 。 开发一个服务端主动向客服端发送股票信息的程序,首先客户端先向服务端发送需要监控的股票的股票代码,然后服务端在该股信息发生变化的时候将信息推送到客户端 22、Silverlight(24) - 2.0通信之Socket, 开发一个多人聊天室 介绍 Silverlight 2.0 Socket通信。开发一个多人聊天室。 服务端:实例化Socket, 绑定, 监听, 连接, 接收数据, 发送数据 客户端:实例化Socket, 指定服务端地址, 连接, 接收数据, 发送数据 23、Silverlight(25) - 2.0线程之Thread, Timer, BackgroundWorker, ThreadPool 介绍 Silverlight 2.0 使用Thread, Timer, BackgroundWorker, ThreadPool来实现多线程开发。 Thread - 用于线程的创建和控制的类 Timer - 用于以指定的时间间隔执行指定的方法的类 BackgroundWorker - 用于在单独的线程上运行操作 ThreadPool - 线程池的管理类 24、Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute 介绍 Silverlight 2.0 使用Lock, Interlocked, EventWaitHandle, Monitor来实现线程同步。 Lock - 确保代码块完成运行,而不会被其他线程中断 Interlocked - 为多个线程共享的变量提供原子级的操作 EventWaitHandle - 通知其他线程是否可入的类 Monitor - 提供同步访问对象的机制 ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的 25、Silverlight(27) - 2.0网页之可脚本化, 与DOM的交互, 与JavaScript的交互 介绍 Silverlight 2.0 使用c#开发可脚本化的代码,Silverlight与宿主页面的DOM之间的交互,Silverlight与宿主页面的JavaScript之间的交互。 ScriptableMemberAttribute - 需要脚本化的属性、方法、事件要标记为此 HtmlPage.RegisterScriptableObject - 将可脚本化对象注册到客户端 HtmlElement - 表示网页的文档对象模型 (DOM) 中的 HTML 元素 HtmlWindow - 提供 JavaScript 的 window 对象的 Silverlight 端的托管表示形

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧