WCF基本WinForm应用程序通信问题

全部,我已经扩展了本教程,以获取和反转在两个单独的WinForm应用程序中显示的字符串。 但是,最终目标是让这种方法在相互之间传递SQL的WinForm应用程序之间运行。 为了方便这一点,我扩展了这个例子,以下是我所拥有的

包含的库.dll

public class WcfInterface { private static WcfInterface instance; private ServiceHost host; private const string serviceEnd = "Done"; protected WcfInterface() { } public static WcfInterface Instance() { if (instance == null) instance = new WcfInterface(); return instance; } public void OpenServiceHost() { host = new ServiceHost(typeof(U), new Uri[] { new Uri("net.pipe://localhost") }); host.AddServiceEndpoint(typeof(T), new NetNamedPipeBinding(), serviceEnd); host.Open(); } public void CloseServiceHost() { host.Close(); } public T AddListnerToServiceHost() { ChannelFactory pipeFactory = new ChannelFactory(new NetNamedPipeBinding(), new EndpointAddress(String.Format("net.pipe://localhost/{0}", serviceEnd))); T pipeProxy = pipeFactory.CreateChannel(); return pipeProxy; } } 

所以在’服务器’表格上,我这样做

 private void Form1_Load(object sender, EventArgs e) { List sqlList = new List(); foreach (string line in this.richTextBoxSql.Lines) sqlList.Add(line); SqlInfo sqlInfo = new SqlInfo(sqlList); WcfInterface wcfInterface = WcfInterface.Instance(); wcfInterface.OpenServiceHost(); } 

哪里

 public class SqlInfo : ISqlListing { private List sqlList; public SqlInfo(List sqlList) { this.sqlList = sqlList; } public List PullSql() { return sqlList; } } [ServiceContract] public interface ISqlListing { [OperationContract] List PullSql(); } 

在客户端WinForm应用程序中

 private ISqlListing pipeProxy; public Form1() { InitializeComponent(); WcfInterface wcfInterface = WcfInterface.Instance(); pipeProxy = wcfInterface.AddListnerToServiceHost(); } 

并且在click事件中我无法从服务器获取List

 private void button1_Click(object sender, EventArgs e) { this.richTextBoxSql.Text = pipeProxy.PullSql().ToString(); // Code hangs here. } 

我的问题是这有什么问题?

谢谢你的时间。


编辑。 我现在还根据以下注释更改了客户端代码

 private ISqlListing pipeProxy { get; set; } private const string serviceEnd = "Done"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.richTextBoxSql.Text = pipeProxy.PullSql().ToString(); } private void Form1_Load(object sender, EventArgs e) { ChannelFactory pipeFactory = new ChannelFactory( new NetNamedPipeBinding(), new EndpointAddress( String.Format("net.pipe://localhost/{0}", serviceEnd))); pipeProxy = pipeFactory.CreateChannel(); } 

这也挂在click事件上。

您设置代码的方式是通过引用WcfInterface.Instance在客户端上创建WCF服务器。 然后,您将从它所服务的同一个线程中调用它,从而导致应用程序锁定。

有很多方法可以解决这个问题。 以下是一些想到的:

  • 在第一个WinForm应用程序中运行该服务,然后使用visual studio中的“添加服务引用”function来创建代理。 请注意,你必须这样做
  • 您仍然可以引用WCF合同的公共库,但是重新编写代码,这样您就不会在“客户端”WinForms应用程序中创建服务实例。