Windows窗体应用程序中的WCF主机 [英] WCF host in Windows Form Application

查看:69
本文介绍了Windows窗体应用程序中的WCF主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiii

我是WCF的新手,我在Console Application中编写了一个完美的代码。

现在我尝试将其转换为Windows Form Application 。



我创建了这样的服务

hiii
I am new to WCF and I have written a code in Console Application which is working perfectly.
Now I have try to convert it into Windows Form Application.

I have created a service like this

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
    void SayHello(string msg);
}





并定义函数



and define the function

public partial class Form1 : IHelloService
   {
       public string SayHello(string msg)
       {
           richTextBox1.Text=msg;
           return "Service on " + Environment.MachineName + " rec. " + msg;
}
   }





我用简单的richTextBox1创建了一个Form1类



And I created a class Form1 with simple richTextBox1

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RichTextBox richTextBox1=new RichTextBox;
        }
    } 



我正在从主程序文件开始服务


and I am starting service from main program file

static void Main(string[] args)
        {
            using(ServiceHost host = new ServiceHost(typeof(Form1)))
            {
            
                host.AddServiceEndpoint(typeof(IHelloService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloWcfService");
                host.Open();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

        }





现在的问题是WCF主机接收数据但是没有在richTextBox1中显示

谢谢

推荐答案

如果你设法开发自托管WCF功能,如果您希望服务部分是 System.Windows.Forms 应用程序,则没有任何改变。如果它与某些类型的UI以及与通信技术无关的任何其他内容交织在一起,WCF就没有任何价值。



随着您的服务合同,它看起来像你想显示客户端在服务端的 RichTextBox 中提供的字符串。

只需重新实施合约方法。



例如,假设你有一个表格:

If you managed to develop a self-hosting WCF functionality, nothing really changes if you want a service part be a System.Windows.Forms application. WCF would have no value if it was interlaced with certain types of UI and anything else not related to communication technology.

With you service contract it looks like you want to show the string supplied by a client in the RichTextBox which is on the service side.
Just re-implement your contract method.

For example, assuming you have a form:
using System.Windows.Forms;

//...

public partial class MyForm : Form {
    RichTextBox MyRichTextBox = new RichTextBox();
    //...
}





以此形式实施您的服务界面。例如,使用部分声明的另一部分添加单独的文件:





Implement your service interface in this form. For example, add a separate file with another part of the partial declaration:

public partial class MyForm : IHelloService {
    void IHelloService.SayHello(string msg) {
        MyRichTextBox.Text = msg;
        //or better: 
        //MyRichTextBox.Rtf = msg; //and pass formatted RTF text
    }
}





注意:你不要必须在两个部分类声明的继承列表中重复:Form和:IHelloService - 这是不需要的,非常方便。此外,我使用显式实现语法(不允许任何访问修饰符)使接口实现非公共。这种语法要好得多,因为没有人可以不小心调用方法 IHelloService.SayHello



-SA


以下工作正常:

This below works fine:
public partial class Form1 : Form
    {
        [ServiceContract]
        public interface IHelloWorldService
        {
            [OperationContract]
            string SayHello(string name);
        }

        public class HelloWorldService : IHelloWorldService
        {
            public string SayHello(string name)
            {
                return string.Format("Hello, {0}", name);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Loading(object sender, EventArgs e)
        {
            Uri baseAddress = new Uri("http://localhost:8758/hello");

            // Create the ServiceHost.
            ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress);
            
			ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
			smb.HttpGetEnabled = true;
			smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
			host.Description.Behaviors.Add(smb);
			host.Open();
		
        }
    }


这篇关于Windows窗体应用程序中的WCF主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆