本文介绍了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());
            }

        }







推荐答案

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


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主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:18