Windows 窗体应用程序中的 WCF 主机
嗨 我是 WCF 新手,我在控制台应用程序中编写了代码。 我创建了一个这样的服务
[ServiceContract]
public interface IHelloService
{
[OperationContract]
void SayHello(string msg);
}
并定义了该功能
public class HelloService: IHelloService
{
public void SayHello(string msg)
{
Console.WriteLine("I rec message : " + msg);
}
}
,我从主程序文件启动服务
static void Main(string[] args)
{
Console.WriteLine("******* Service Console *******");
using(ServiceHost host = new ServiceHost(typeof(HelloWcfServiceLibrary.HelloService)))
{
host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfService");
host.Open();
Console.Read();
}
}
,在客户端,代码
static void Main(string[] args)
{
IHelloService proxy = ChannelFactory<IHelloService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWcfService"));
string msg;
while (true)
{
msg = Console.ReadLine();
msg = proxy.SayHello(msg);
Console.WriteLine("Server returned " + msg);
}
}
工作正常,但我想在 Windows 窗体应用程序中执行相同的操作,并在 richtextbox 中显示接收到的数据但我不知道该怎么做。 请有人帮助我
hiii
I am new to WCF and I have written a code in Console application.
I have created a service like this
[ServiceContract]
public interface IHelloService
{
[OperationContract]
void SayHello(string msg);
}
and define the function
public class HelloService: IHelloService
{
public void SayHello(string msg)
{
Console.WriteLine("I rec message : " + msg);
}
}
and I am starting service from main program file
static void Main(string[] args)
{
Console.WriteLine("******* Service Console *******");
using(ServiceHost host = new ServiceHost(typeof(HelloWcfServiceLibrary.HelloService)))
{
host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfService");
host.Open();
Console.Read();
}
}
and at client side the code is
static void Main(string[] args)
{
IHelloService proxy = ChannelFactory<IHelloService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloWcfService"));
string msg;
while (true)
{
msg = Console.ReadLine();
msg = proxy.SayHello(msg);
Console.WriteLine("Server returned " + msg);
}
}
it is working properly but I want to do the same thing in Windows Form Application and show the received data i richtextbox but I dont know how to do that.
Please Someone help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与您在控制台应用程序中所做的相同。您可以在 Load 方法中启动 ServiceHost,但一个区别是 RichTextbox 只能在 GUI 线程中访问,因此您可能必须将 GUI SynchronizationContext 保存在某处,当您想要向该富文本框输出内容时,您需要调用 Post方法或在 SynchronizationContext 上发送,例如:
注意:这只是显示一个示例,它可能不起作用。
It's just the same as what you did in console application. You can start a ServiceHost in Load method, but one difference is that RichTextbox can only be access in GUI thread, so you may have to save the GUI SynchronizationContext somewhere and when you want to output something to that rich textbox, you need to call Post method or send on the SynchronizationContext, like:
Note: this just shows a sample, it may not work.