将WCF内容显示到列表框

时间:2014-10-24 06:32:09

标签: c# winforms wcf

我有一个WCF服务合同,旨在通过TCP网络从剪贴板发送字符串,并由winform列表框接收和显示,我已设法连接两个winforms,但是我无法获取显示的URL在列表框中。

的app.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
  <netPeerTcpBinding>
    <binding name ="NewBinding0">
      <security mode ="None" />
      <resolver mode ="Pnrp" />
    </binding>
  </netPeerTcpBinding>
</bindings>
<client>
  <remove contract="IMetadataExchange" name="sb" />
  <endpoint address="net.p2p://URLService" binding="netPeerTcpBinding"
            bindingConfiguration="NewBinding0" contract="WCFWinForm.IApplicationService"
            name="Client" />
</client>
<services>
  <service name="WCFWinForm.ApplicationService">
    <endpoint address="net.p2p://URLService" binding="netPeerTcpBinding"
      bindingConfiguration="NewBinding0" contract="WCFWinForm.IApplicationService" />
  </service>
</services>
</system.serviceModel>
</configuration>

IApplicationService.cs

[ServiceContract]
public interface IApplicationService
{
    [OperationContract(IsOneWay = true)]
    void GetURL();
}

ApplicationService.cs

        public class ApplicationService : IApplicationService
        {
        public void GetURL()
            {
                var addCopy = Clipboard.GetText(TextDataFormat.Text);
                WCF.listURL.Items.AddRange(addCopy.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
            }
        }

WCF.cs(Winform)

    public static void WCF_Load(object sender, EventArgs e)
    {
        //Establish connection with Service
        ServiceHost svc = new ServiceHost(typeof(ApplicationService));
        svc.Credentials.Peer.MeshPassword = "hehe";
        svc.Open();

        //Both Server and Client binding should match
        NetPeerTcpBinding np2p = new NetPeerTcpBinding();

        np2p.Security.Mode = SecurityMode.None;
    }

    private void btnSendURL_MouseClick(object sender, EventArgs e)
    {
        ApplicationService url = new ApplicationService();
        url.GetURL();
    }

您可能会注意到在ApplicationService.cs中我使用WCF.listURL,这是因为我不知道如何正确调用表单列表框; ApplicationService中的listURL。

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的,除了请求数据或执行其他操作之外,服务应该对客户端一无所知。我建议重写服务以仅返回数据,并让客户端应用程序将该数据绑定到ListBox。但是,您需要删除IsOneWay属性。像这样:

[ServiceContract]
public interface IApplicationService
{

    [OperationContract]
    string GetURL();
}    

在上面的代码中,删除了单向属性,该方法返回string。我使用string代替string数组 - 客户端可以确定如何处理数据(拆分或不拆分,重新格式化等)。

public class ApplicationService : IApplicationService
{

    public string GetURL()
    {

        return Clipboard.GetText(TextDataFormat.Text);
    }
}

该实现只返回剪贴板中的文本字符串。

private void btnSendURL_MouseClick(object sender, EventArgs e)
{

    ApplicationService url = new ApplicationService();
    string data = url.GetURL();

    listBox1.Items.AddRange(addCopy.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}

最后,应用程序将调用服务方法并获取返回的数据。然后,它会将数据添加到ListBox(示例中名为listBox1)。现在,您已将WCF服务与客户端分离,应该是这样。