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

2023-09-04 06:01:49 作者:小仙女

所有的,我已经延长本教程获取并在两个单独的WinForm的应用程序中显示相反的字符串。然而,最终目标是获得之间的传递海誓山盟的SQL WinForm的应用程序这方面的工作。为了促进这一点,我已经扩展这个例子,下面是我

All, I have extended this tutorial to get and reverse string displayed in two seperate WinForm applications. However, the end goal is to get this working between to WinForm apps that pass SQL between eachother. To facilitate this I have extended this example and the following is what I have

一个库.dll文件包含

A library .dll containing

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<T, U>() 
    {
        host = new ServiceHost(typeof(U), new Uri[] { new Uri("net.pipe://localhost") });
        host.AddServiceEndpoint(typeof(T), new NetNamedPipeBinding(), serviceEnd);
        host.Open();
    }

    public void CloseServiceHost<T>()
    {
        host.Close();
    }

    public T AddListnerToServiceHost<T>()
    {
        ChannelFactory<T> pipeFactory = 
            new ChannelFactory<T>(new NetNamedPipeBinding(), 
                                         new EndpointAddress(String.Format("net.pipe://localhost/{0}", 
                                                                                      serviceEnd)));
        T pipeProxy = pipeFactory.CreateChannel();
        return pipeProxy;
    }
}

所以,服务器的形式,我做

So on the 'server' form, I do

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

其中,

public class SqlInfo : ISqlListing
{
    private List<string> sqlList;
    public SqlInfo(List<string> sqlList)
    {
        this.sqlList = sqlList;
    }

    public List<string> PullSql()
    {
        return sqlList;
    }
}

[ServiceContract]
public interface ISqlListing
{
    [OperationContract]
    List<string> PullSql();
}

在客户端的WinForm程序

In the client WinForm app

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

和的单击事件我attampt获得名单,其中,串&GT; 从服务器

and on the click event I attampt to get the List<string> from the server

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

我的问题是什么是错的呢?

My question is what is wrong with this?

感谢您的时间。

编辑。我根据意见如下:的

Edit. I have now also changed the client code according to comments as follows

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<ISqlListing> pipeFactory =
    new ChannelFactory<ISqlListing>(
      new NetNamedPipeBinding(),
      new EndpointAddress(
         String.Format("net.pipe://localhost/{0}", serviceEnd)));

    pipeProxy = pipeFactory.CreateChannel();
}

这也挂在click事件。的

this also hangs on the click event.

推荐答案

你有code设置,你被引用WcfInterface.Instance创建客户端上的WCF服务器的方式。然后,您将在同一个线程,它被送达,从而导致你的应用程序锁定调用它。

The way you have the code set up, you are creating a WCF server on the client by referencing WcfInterface.Instance. You are then calling it from the same thread that it is being served on, causing your application to lock up.

有许多方法来解决这个。这里有一些浮现在脑海中:

There are a number of ways to get around this. Here are a few that come to mind:

获取你的第一个winform应用程序运行的服务,然后使用添加服务引用功能,在Visual Studio中创建代理。请注意,您必须 您可以仍引用一个公共库的WCF的合同,但返工你的code,这样你不会在你的客户WinForms应用程序创建服务的一个实例。