【问题标题】:Display WCF content to listbox将 WCF 内容显示到列表框
【发布时间】:2014-10-24 06:32:08
【问题描述】:

我有一个 WCF 服务合同,旨在通过 TCP 网络从剪贴板发送一个字符串,并由 winform 列表框接收和显示,我已经成功连接了两个 winform,但是我无法获取要显示的 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,这是因为我不知道如何正确调用Form列表框; ApplicationService 中的 listURL。

【问题讨论】:

  • WinForm 应用程序应该调用服务,服务应该返回数据,然后WinForm 应该获取数据并将其绑定到控件。除了请求执行一个操作(方法)之外,服务本身应该对客户端一无所知。

标签: c# winforms wcf


【解决方案1】:

正如我在评论中提到的,除了请求数据或要执行的其他操作之外,服务应该对客户端一无所知。我建议重写服务以仅返回数据,并让客户端应用程序将该数据绑定到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 服务与客户端解耦了。

【讨论】:

  • 嗯,我明白你的意思了。我试过了,但它摆脱了我的 P2P WCF 表单连接,并且由于某种原因仍然没有向列表框添加内容。
  • 它不应该摆脱连接。你是如何添加连接的?您可能还想编辑您的问题以包含相关的配置文件。
  • 好的,我已经更新了更多信息。也许你能更好地理解我现在想要做什么......哈哈。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多