实现 JavaScript 代码与控制台应用程序 C# 之间的通信
现在我正在解决网络服务器性能测试任务,但遇到了一个小问题。
有一种方法可以DHTML 和 C# 代码之间进行通信。它在 Win 应用程序中完美运行,但我需要控制台应用程序。 所以我创建了简单的测试
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CometTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Client c = new Client(@"URL");
Console.ReadLine();
Console.WriteLine(c.ToString());
}
}
}
Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace CometTest
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Client
{
public WebBrowser browser;
public Client(string host)
{
browser = new WebBrowser();
browser.ObjectForScripting = this;
browser.Navigate(host);
}
public void Notify(String message)
{
Console.WriteLine(message);
}
}
}
在我的 JavaScript 代码中执行 window.external.Notify('测试'); ,但没有成功:-(
有什么建议吗?
Now I'm solving web server performance testing task and a have a little problem.
There is a way to communication between DHTML and C# code. It works perfectly in Win Application but I need Console Application.
So I create simple test
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CometTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Client c = new Client(@"URL");
Console.ReadLine();
Console.WriteLine(c.ToString());
}
}
}
Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace CometTest
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Client
{
public WebBrowser browser;
public Client(string host)
{
browser = new WebBrowser();
browser.ObjectForScripting = this;
browser.Navigate(host);
}
public void Notify(String message)
{
Console.WriteLine(message);
}
}
}
In my JavaScript code I perform
window.external.Notify('test');
, but without success :-(
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你几乎和这里的人有同样的问题:WebBrowser Control - 控制台应用程序 - 事件未触发
也许他得到的答案也对您有帮助。来自控制台应用程序的消息不会被触发,因为:
I think you pretty much have the same proplem as the guy here: WebBrowser Control - Console Application - Events Not Firing
Maybe the answer he got helps you, too. The messages from an console application are not fired because: