.net remoting 抛出异常
原文:.net remoting 抛出异常 本文告诉大家如何在使用 .net remoting 的时候,抛出异常。 所有在远程软件运行的类,如果需要传输到本地,都需要继承 MarshalByRefObject 或其他可以序列化的类。 在 .net Framework 4.0 就默认指定只反序列化基础类型,如果需要反序列化其他的类型,那么就需要设置 但是设置了TypeFilterLevel不是对所有的类型都可以进行转换,如果不小心直接在调用方法抛出异常,那么会因为无法反序列,让本地拿不到 这时本地会提示 如果需要在 .net remoting 使用异常,那么需要自己创建一个异常,继承 RemotingException 因为默认的 RemotingException 没有反序列,所以需要添加 Serializable 特性 微软建议继承 如果直接运行,会发现报告 解决方法是创建一个构造函数,写入这个函数就不需要再写其他的代码。 如果有一些特殊的属性需要自己设置,建议创建一个默认构造函数,和两个方法,因为使用上面的方法不会序列化自己定义的属性。 在 GetObjectData 拿到必要的属性,这个需要自己把需要的属性写入。然后在构造函数重写 因为上面的代码用到 Message ,需要重写这个属性,因为默认是只读,不能在构造函数设置。 是不是觉得很复杂,实际上简单的方法是通过 json 在GetObjectData把类转换为json,在构造转换为类。 那么为什么在使用 Serializable 特性还需要继承 ISerializable ,因为继承 ISerializable 就可以在一个构造函数 关于 ISerializable 请看 c# - What is the point of the ISerializable interface? - Stack Overflow How to: Create an Exception Type That Can be Thrown by Remote Objects 我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=19bm8i8js1ezb 本文会经常更新,请阅读原文:
TypeFilterLevel,设置的方法是在使用下面代码 public static IChannel CreatChannel(string port = "")
{
if (string.IsNullOrEmpty(port))
{
port = Guid.NewGuid().ToString("N");
}
var serverProvider = new SoapServerFormatterSinkProvider();
var clientProvider = new SoapClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["portName"] = port.ToString();
return new IpcChannel(props, clientProvider, serverProvider);
}
// 远程
public void Foo()
{
throw new CsdnNotFoundException();
}
public class CsdnNotFoundException : Exception
{
public CsdnNotFoundException(string str) :
base(str)
{
}
}
System.Runtime.Serialization.SerializationException程序无法序列。反序列
[Serializable]
public class CsdnNotFoundException : RemotingException
{
public CsdnNotFoundException(string str) :
base(str)
{
}
}
ISerializable,标记特性 [Serializable]
public class CsdnNotFoundException : RemotingException, ISerializable
{
public CsdnNotFoundException(string str) :
base(str)
{
}
}
System.Runtime.Serialization.SerializationException:“未找到反序列化“lindexi.Csdn.CsdnNotFoundException”类型对象的构造函数 protected CsdnNotFoundException([NotNull] SerializationInfo info, StreamingContext context) : base(info,
context)
{
}
[Serializable]
public class CsdnNotFoundException : RemotingException, ISerializable
{
public CsdnNotFoundException()
{
//默认构造,可以在反射创建
}
public CsdnNotFoundException(string str) :
base(str)
{
}
protected CsdnNotFoundException([NotNull] SerializationInfo info, StreamingContext context)
//: base(info, context) 不使用基类的原因是基类会报告 找不到 ClassName 和其他很多的坑
{
//反序列化创建
Message = (string) info.GetValue(MessageSerialization, typeof(string));
}
// 重写消息,用于在构造设置值
public override string Message { get; }
// 用于在构造拿到消息的值
private const string MessageSerialization = "Message";
// 重写这个方法,在序列化调用
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(MessageSerialization, Message);
}
}
[NotNull] SerializationInfo info, StreamingContext context方法的,可以拿到值ISerializable
xx([NotNull] SerializationInfo info, StreamingContext context)进行处理和处理如何序列化。处理如何序列化可以提高性能,因为自己知道哪些需要序列化,哪些不需要。
https://lindexi.gitee.io/lindexi/post/.net-remoting-%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8.html
,以避免陈旧错误知识的误导,同时有更好的阅读体验。
本作品采用
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议
进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:
https://lindexi.gitee.io
),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请
与我联系
。
.net remoting 抛出异常的更多相关文章
- .net remoting 使用事件
原文:.net remoting 使用事件 在RPC如果需要使用事件,相对是比较难的.本文告诉大家如何在 .net remoting 使用事件. 目录 使用 Channel 序列化 开发建议 修复异常 ...
- WPF 从零开始开发 dotnet Remoting 程序
本文告诉大家如何不使用框架,从零开始开发一个 dotnet remoting 程序 在我的另一篇博客 WPF 使用RPC调用其他进程 就大概告诉了大家如何在 WPF 使用 dotnet remotin ...
- dotnet 从入门到放弃的 500 篇文章合集
本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...
- WPF 使用RPC调用其他进程
如果在 WPF 需要用多进程通信,一个推荐的方法是 WCF ,因为 WCF 是 RPC 计算.先来讲下 RPC (Remote Procedure Call) 远程过程调用,他是通过特定协议,包括 t ...
- 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集
title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...
- 2019-3-1-WPF-从零开始开发-dotnet-Remoting-程序
title author date CreateTime categories WPF 从零开始开发 dotnet Remoting 程序 lindexi 2019-03-01 09:30:45 +0 ...
- 2018-6-24-WPF-使用RPC调用其他进程
title author date CreateTime categories WPF 使用RPC调用其他进程 lindexi 2018-06-24 14:41:29 +0800 2018-2-13 ...
- 2019-9-24-dotnet-remoting-抛出异常
title author date CreateTime categories dotnet remoting 抛出异常 lindexi 2019-09-24 15:39:50 +0800 2018- ...
- 2019-9-24-dotnet-remoting-使用事件
title author date CreateTime categories dotnet remoting 使用事件 lindexi 2019-09-24 15:39:26 +0800 2018- ...
随机推荐
- (转)使用qemu-img管理虚拟机磁盘镜像(创建虚拟机,虚拟机快照)
转自:http://blog.csdn.net/bravezhe/article/details/8461386 一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系 ...
- PythonAdvanced
PythonAdvanced function 函数 (要多使用函数,方便,少变量,好改错) 函数是可以重复执行的语句块,可以重复使用 作用: 1.用于封装语句块,提高代码的重用性 2.定义用户级别的 ...
- TCP快速重传与快速恢复原理分析(四种不同的算法)
在TCP/IP中,快速重传和恢复(fast retransmit and recovery,FRR)是一种拥塞控制算法,它能快速恢复丢失的数据包.没有FRR,如果数据包丢失了,TCP将会使用定时器来要 ...
- 总览:SpringCloud基础结构
客户端: 1.连接 配置中心2.连接 服务中心3.连接 链路跟踪3.feign调用4.熔断机制5.连接 熔断监控6.swagger2 生成的RESTful-API7.消息总线-rabbitMQ实现 基 ...
- iTestin云测工具
软件概述 iTestin是免费服务移动App开发者的真机自动化云测试客户端工具.基于真实的智能终端设备录制一个测试脚本然后运行,并输出运行结果.覆盖Android和iOS两大设备平台,支持Pad/Ph ...
- ORACEL上传BLOB,深度遍历文件夹
// uploadingDlg.cpp : 实现文件// #include "stdafx.h"#include "uploading.h"#include & ...
- android获取一个用于打开Word文件的intent
近期在做项目使用webview显示后,有写文档须要打开,找了一些资料,研究了一下,弄出来了! 以下贴一下主要代码: param为文档的主要路径 public static Intent getWord ...
- 【u106】3D模型
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若 ...
- IntelliJ IDEA设置鼠标悬浮提示
测试代码; public interface MyInterface { /** * 我是接口方法的注释 * @param num1 我是接口方法入参的注释 * @return 我是接口方法返回值的注 ...
- 使用SQLiteHelper创建数据库并插入数据 分类: H1_ANDROID 2013-11-05 22:44 1398人阅读 评论(0) 收藏
参考<疯狂android讲义>8.4节P424 1.获取SQLiteDatabase实例有2种方法,一是直接new SQLiteDatabase(),另一种使用SQLiteHelper.一 ...