Windows 窗体应用程序中的 WCF 主机

发布于 2024-11-30 11:36:44 字数 1340 浏览 2 评论 0原文

嗨 我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-12-07 11:36:44

这与您在控制台应用程序中所做的相同。您可以在 Load 方法中启动 ServiceHost,但一个区别是 RichTextbox 只能在 GUI 线程中访问,因此您可能必须将 GUI SynchronizationContext 保存在某处,当您想要向该富文本框输出内容时,您需要调用 Post方法或在 SynchronizationContext 上发送,例如:


public class HelloService: IHelloService {    
private SynchronizationContext context;
private RichTextbox textbox;
public void SayHello(string msg)   
{       
context.Post((obj) => textbox.Add("I rec message : " + msg));
}
}

注意:这只是显示一个示例,它可能不起作用。

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:


public class HelloService: IHelloService {    
private SynchronizationContext context;
private RichTextbox textbox;
public void SayHello(string msg)   
{       
context.Post((obj) => textbox.Add("I rec message : " + msg));
}
}

Note: this just shows a sample, it may not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文