【问题标题】:MissingMethodException when making a .NET Remoting call over IPC通过 IPC 进行 .NET Remoting 调用时出现 MissingMethodException
【发布时间】:2013-01-10 15:09:59
【问题描述】:

我有一项服务需要将字符串泵送到一个帮助应用程序,该应用程序将来自服务的关键消息显示给用户(Vista+ 不让服务访问 GUI)。由于我使用了基于 TCP 的 .NET Remoting,我想我会对 IPC 协议做同样的事情。但是,在获得对远程对象的引用后,在调用远程方法时出现以下异常:

MissingMethodException: No parameterless constructor defined for this object.

只需向类添加一个无参数构造函数,就会在调用时给我一个 NullReferenceException。我做错了什么?我在下面包含了我的相关代码:

申请代码

public class MyMsgBus : MarshalByRefObject, IDisposable, IMxServeBus
{
    private Thread myThread = null;
    private volatile List<string> myMsgBus = null;
    private volatile bool myThreadAlive = false;
    private volatile bool myIsDisposed = false;
    private volatile bool myIsDisposing = false;
    private IpcChannel myIpc = null;

    public MyMsgBus(string busname)
    {
       myMsgBus = new List<string>();

       myIpc = CreateIpcChannel(busname);
       ChannelServices.RegisterChannel(myIpc);

       var entry = new WellKnownServiceTypeEntry(
          typeof(MxServeBus),
          "MyRemoteObj.rem",
          WellKnownObjectMode.Singleton);

       RemotingConfiguration.RegisterWellKnownServiceType(entry);
    }

    // defined in IMyMsgBus
    public void SendMessage(string message)
    {
       // do stuff
    }

    public static IpcChannel CreateIpcChannel(string portName)
    {
       var serverSinkProvider = new BinaryServerFormatterSinkProvider();
       serverSinkProvider.TypeFilterLevel = TypeFilterLevel.Low;

       IDictionary props = new Hashtable();
       props["portName"] = portName;
       props["authorizedGroup"] = "Authenticated Users";

       return new IpcChannel(props, null, serverSinkProvider);
    }

    public static IpcChannel CreateIpcChannelWithUniquePortName()
    {
       return CreateIpcChannel(Guid.NewGuid().ToString());
    }
}

测试客户端

static void Main(string[] args)
{
   var channel = MyMsgBus.CreateIpcChannelWithUniquePortName();
   ChannelServices.RegisterChannel(channel, true);

   var objUri = "ipc://MyMsgBus/MyRemoteObj.rem";
   IMyMsgBus lBus = (IMyMsgBus)Activator.GetObject(typeof(IMyMsgBus), objUri);
   lBus.SendMessage("test");
   Console.WriteLine();
}

在此先感谢您提供任何帮助。仅供参考,这是一个通过使用共享接口配置的远程处理实例,其中 IMyMsgBus 定义了可用于通过 IPC 调用的方法。

【问题讨论】:

  • 你能显示你添加了无参数构造函数的类吗?好像你错过了一些初始化
  • 我在 MyMsgBus 中添加了无参数构造函数。
  • 显示MyMsgBus的代码
  • 我已经添加了 MyMsgBus 类的基本大纲和相关成员。
  • 我已经添加了答案。

标签: c# .net .net-4.0 ipc .net-remoting


【解决方案1】:

您将获得NullReferenceException 添加无参数构造函数作为正确初始化所需的参数。如果不提供它,就会错过初始化的某些部分。

您应该重构您的代码以允许无参数构造函数为

   private bool _initialized = false;

    public MyMsgBus() {}

    public Initialize(string busname) // Make this part of interface    
    {
       myMsgBus = new List<string>();

       myIpc = CreateIpcChannel(busname);
       ChannelServices.RegisterChannel(myIpc);

       var entry = new WellKnownServiceTypeEntry(
          typeof(MxServeBus),
          "MyRemoteObj.rem",
          WellKnownObjectMode.Singleton);

       RemotingConfiguration.RegisterWellKnownServiceType(entry);
       _initialized = true;
    }

在所有其他公共方法中,检查_initialized 标志和throw NotInitializedException 如果标志为假。

你应该把它当作

 IMyMsgBus lBus = (IMyMsgBus)Activator.GetObject(typeof(IMyMsgBus));
 lBus.Initialize(objUri);
 ... do Further operation

【讨论】:

  • 如果初始化设置远程处理,我将如何从远程进程调用初始化?
  • 试试这个。如果不行,就需要其他方式传参了。
  • 我没有完全按照你上面概述的那样实现它,但我实现了无参数构造函数,如下所示:public MyMsgBus() : this("MyMsgBus") { }。哪个有效,谢谢!
  • 那是另一种方式,但它仍然使消息总线名称固定。这就是权衡。
猜你喜欢
  • 2016-11-08
  • 1970-01-01
  • 2011-08-03
  • 2015-06-17
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
相关资源
最近更新 更多