【问题标题】:How resolve "C# Native Messaging host tried sending a message that is 959524914 bytes long"如何解决“C# Native Messaging 主机尝试发送 959524914 字节长的消息”
【发布时间】:2019-10-28 03:55:44
【问题描述】:

我今天和昨天花了几个小时,研究如何使用 C# 主机获取 Chrome 原生消息传递。用 Java Host 再试一次,同样的错误。 在这些问题的帮助下,我尝试了几次,但没有成功:

C# native host with Chrome Native Messaging

Native messaging from chrome extension to native host written in C#

Very Slow to pass "large" amount of data from Chrome Extension to Host (written in C#)

尝试使用 VStudio 2017、2019,使用 c# 4.7、c# 客户端配置文件 4.0; 使用 Java,尝试使用 JRE 8、JRE 11,并出现相同的错误。 使用Python,测试成功,为什么? (二进制模式有效?)项目经理接受的唯一解决方案是 C#...

static void Main(string[] args)
{
  bool platform64 = true;
  if (IntPtr.Size == 8)
    platform64 = true;
  else
    platform64 = false;
  logger.InfoFormat("Platform x64?: {0}", platform64);

  logger.InfoFormat("Arguments:");
  foreach (String arg in args)
  {
    logger.InfoFormat("  arg: {0}", arg);
  }

  JObject data;
  while ((data = Read()) != null)
  {
    var processed = ProcessMessage(data);
    SendMessage(processed);
    if (processed["text"].Value<string>().Equals("exit"))
    {
      return;
    }
  }
}

public static JObject Read()
{
  logger.Debug("Init Read()...");
  var stdin = Console.OpenStandardInput();

  var lengthBytes = new byte[4];
  stdin.Read(lengthBytes, 0, 4);

  var length = BitConverter.ToInt32(lengthBytes, 0);

  logger.DebugFormat("Length of bytes...{0}", length);

  var buffer = new char[length];

  var input = "";
  for (int i = 0; i < length; i++)
  input += (char)stdin.ReadByte();

  return JsonConvert.DeserializeObject<JObject>(input);
}

public static void SendMessage(JObject data)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(data.ToString(Formatting.None));
            var stdout = Console.OpenStandardOutput();
            stdout.WriteByte((byte)((bytes.Length >> 0) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 8) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 16) & 0xFF));
            stdout.WriteByte((byte)((bytes.Length >> 24) & 0xFF));
            stdout.Write(bytes, 0, bytes.Length);
            stdout.Flush();
        }

从 Google 的文档中,关于二进制格式和来自 StackOverflow 的信息,有谁知道什么可以使它工作,并且 Google 与主机通信?

发送本机消息的示例(直接):

chrome.runtime.sendNativeMessage('com.otisw.signer',
    { text: "list" },
    function (response) {
        console.log("Received " + response);
    });

【问题讨论】:

  • @wOxxOm,谢谢。我已经编辑了帖子,包括 SendMessage() 方法
  • 您的 SendMessage() 在写入长度时使用硬编码的 little-endian 字节顺序,但它应该取决于平台。正确的实现应该可以说像您的 Read() 一样使用 BitConverter。
  • @wOxxOm,我试过使用 BitConverter,同样的错误(主机尝试发送字节......)使用 BitConverter:使用(var stdout = Console.OpenStandardOutput()){ var bytes = System.Text .Encoding.UTF8.GetBytes(json); var lengthBytes = BitConverter.GetBytes(bytes.Length); logger.DebugFormat("字节长度...{0}", lengthBytes.Length); stdout.Write(lengthBytes, 0, 4); stdout.Write(bytes, 0, bytes.Length);标准输出.Flush(); }

标签: c# google-chrome-extension


【解决方案1】:

它正在工作。你不会相信,但移除 log4net 依赖,主机开始通信。 只有导入依赖项,它也可以工作,但是当我使用 logger.debug() 方法时,插件再次停止工作。也许 log4net 使用来自控制台的流。 感谢您的帮助,@wOxxOm,我已将 Write 方法修改为使用 BitConverter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 2020-05-29
    • 2021-08-27
    相关资源
    最近更新 更多