WPF中WCF应用实例

开发 前端
我们创建了一个MyServiceClient对象,并使用它来获取来自WCF服务的消息。然后我们在WPF应用程序中显示这个消息。需要注意的是,由于WCF支持多种通信协议和编码方式,因此可以根据实际需求选择不同的绑定和终结点。

WPF和WCF可以很好地结合使用,WCF提供了一种方便、灵活的方式来实现客户端和服务器之间的通信。以下是一个使用WPF和WCF实现简单客户端/服务器应用的示例。

1. 创建WCF服务

首先,在Visual Studio中创建一个新的WCF服务应用程序,称为"ServerApp"。在这个应用程序中,我们将定义一个简单的服务协定,用于向客户端发送一条问候消息。

```csharp
[ServiceContract]
public interface IGreetingService
{
    [OperationContract]
    string Greet(string name);
}




public class GreetingService : IGreetingService
{
    public string Greet(string name)
    {
        return "Hello, " + name + "!";
    }
}
```




然后,在服务器应用程序的App.config文件中添加以下终结点:




```xml
<system.serviceModel>
  <services>
    <service name="ServerApp.GreetingService" behaviorConfiguration="ServiceBehavior">
      <endpoint address="" binding="basicHttpBinding" contract="ServerApp.IGreetingService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
```

2. 创建WPF客户端

在Visual Studio中创建一个新的WPF应用程序,称为"ClientApp"。然后,将WCF服务协定复制到客户端应用程序中,并添加对System.ServiceModel的引用。然后,在客户端应用程序的MainWindow.xaml.cs文件中添加以下代码:

```csharp
public partial class MainWindow : Window
{
    private IGreetingService _greetingService;




    public MainWindow()
    {
        InitializeComponent();
        ChannelFactory<IGreetingService> factory = new ChannelFactory<IGreetingService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/GreetingService"));
        _greetingService = factory.CreateChannel();
    }




    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string name = txtName.Text;
        string greeting = _greetingService.Greet(name);
        lblGreeting.Content = greeting;
    }
}
```

在这个示例中,我们在MainWindow的构造函数中创建了一个WCF代理,用于向服务器发送远程调用。然后,在Button_Click事件中,我们调用WCF代理的Greet方法,并将结果显示在Label控件上。

需要注意的是,服务器应用程序和客户端应用程序可以运行在不同的计算机上。在这种情况下,只需将客户端应用程序中的EndpointAddress地址更改为服务器应用程序的地址即可。

WCF(Windows Communication Foundation)是.NET Framework中的一个组件,它允许应用程序在不同的进程和计算机之间进行通信。WCF支持多种通信协议和编码方式,包括HTTP、TCP、MSMQ和IPC等。以下是一个简单的使用WCF应用的示例:假设我们有一个WPF应用程序和一个后端服务器应用程序,我们想要在这两个应用程序之间进行通信。1. 创建WCF服务在后端服务器应用程序中,我们创建并公开一个WCF服务,用于向客户端提供数据和功能。我们定义一个名为IMyService的接口,其中包含一个GetMessage方法:

```csharp
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string GetMessage();
}


public class MyService : IMyService
{
    public string GetMessage()
    {
        return "Hello, WCF!";
    }
}
```


需要注意的是,在接口和实现类上都使用了WCF的特性,包括ServiceContract和OperationContract等。


然后我们在服务端创建一个ServiceHost对象,将MyService类公开为IMyService服务:


```csharp
ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8000"));
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "MyService");
host.Open();
```

在这个示例中,我们使用了一个基本的HTTP绑定,并将服务公开为http://localhost:8000/MyService。2. 在WPF应用程序中调用WCF服务在WPF应用程序中,我们使用ChannelFactory和WCF代理访问后端服务器应用程序中的WCF服务。我们定义一个名为MyServiceClient的类,用于封装对WCF服务的访问:

```csharp
public class MyServiceClient
{
    private IMyService proxy;


    public MyServiceClient()
    {
        var factory = new ChannelFactory<IMyService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/MyService"));
        proxy = factory.CreateChannel();
    }


    public string GetMessage()
    {
        return proxy.GetMessage();
    }
}
```

在这个类中,我们使用ChannelFactory创建一个IMyService代理,并封装GetMessage方法的调用。然后我们在WPF应用程序中使用MyServiceClient类来访问WCF服务:

```csharp
MyServiceClient client = new MyServiceClient();
string message = client.GetMessage();
MessageBox.Show(message);
```

在这个示例中,我们创建了一个MyServiceClient对象,并使用它来获取来自WCF服务的消息。然后我们在WPF应用程序中显示这个消息。需要注意的是,由于WCF支持多种通信协议和编码方式,因此可以根据实际需求选择不同的绑定和终结点。例如,如果需要在不同的计算机之间进行通信,可以考虑使用TCP绑定或命名管道(Named Pipe)绑定。如果需要在Web浏览器之间进行通信,则可以考虑使用基于REST的Web服务。

责任编辑:武晓燕 来源: WPF践行者
相关推荐

2009-11-05 13:54:07

WCF Service

2009-11-06 15:02:47

WCF契约查询

2010-03-01 15:40:04

WCF实例停用

2009-11-06 09:39:40

WCF契约

2010-03-02 14:06:37

WCF服务实例管理模式

2010-02-23 10:25:29

2021-02-11 08:27:28

数据

2010-03-02 13:43:01

WCF事务演示

2012-02-14 10:18:11

WCF数据服务

2009-12-29 14:58:31

WPF优点

2009-12-21 14:58:57

WCF用户密码认证

2010-11-25 10:05:22

Visual StudSilverlightWCF

2010-03-01 10:45:59

WCF集合类

2009-11-05 16:01:51

WCF单调服务

2009-12-23 15:16:52

WPF数据绑定

2011-05-16 09:30:30

jQueryWCF

2010-02-22 11:25:50

WCF DateSet

2009-12-22 17:30:47

WCF Address

2010-02-23 09:34:15

WCF重载

2009-11-06 09:22:46

WCF应用
点赞
收藏

51CTO技术栈公众号