.NET Remoting,将对象传递给方法

mea*_*nny 8 .net c# remoting serializable

我正在编写.NET Remoting应用程序.我的dll,服务器和客户端都正常工作.但是,当我尝试更改我的方法调用以获取对象参数而不是像int这样的简单类型时,它会抱怨此错误.

键入System.Runtime.Remoting.ObjRef及其中的类型(例如System.Runtime.Remoting.ObjRef)不允许在此安全级别进行反序列化.

方法是这样的.

public List<Orders> GetOrders(int UserID) { //Works

public List<Orders> GetOrders(Users user) { // Doesnt Work

[Serializable]
public class Users : MarshalByRefObject {
Run Code Online (Sandbox Code Playgroud)

现在我已经创建了User类,[Serializable]并给它MarshalByRefObject继承.这可能是我的问题吗?我试过从User类中删除[Serializable]并且它抱怨因为它无法解释它.

编辑 好的,这是我的客户端方法.

IChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
CustomType Server = (CustomType)Activator.GetObject(typeof(CustomType), "tcp://localhost:9934/CustomType");
Run Code Online (Sandbox Code Playgroud)

这是我的服务器.

BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 9934;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomType), "CustomType", WellKnownObjectMode.Singleton);
Console.WriteLine("Server is initialized");
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

Fre*_*els 1

实际上,.NET 远程处理是一项过时的技术。你应该看看 WCF。

关于您的实际问题:您可能以太低的信任级别运行应用程序。
该类Users应该是可序列化的,但是,如果它不包含任何应在服务器上运行的方法,则它不应派生自MarshalByRefObject

  • 可以说,远程处理仍然可用于同一进程中的 AppDomain 之间的通信。只是在说' (12认同)