[WCF Step by Step 读书笔记] Chapter02 寄宿一个WCF服务

如何使用WPF寄宿一个WCF的Service?

1. 首先新建一个WCF的Class Library,包括IProductService.cs和ProductService.cs两个文件,内容与上一章相同。同时要有一个与上一章相同的数据库访问的project

2. 新建一个WPF的工程,其后台CS文件内容为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using Products;

namespace ProductsServiceHost
{
    /// <summary>
    /// Interaction logic for HostController.xaml
    /// </summary>
    public partial class HostController : Window
    {
        private ServiceHost productsServiceHost;

        public HostController()
        {
            InitializeComponent();
        }

        private void handleException(Exception ex)
        {
            MessageBox.Show(ex.Message, "Exception",
                MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void start_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                productsServiceHost = new ServiceHost(typeof(ProductsServiceImpl));
                productsServiceHost.Open();
                stop.IsEnabled = true;
                start.IsEnabled = false;
                status.Text = "Service Running";
            }
            catch (Exception ex)
            {
                handleException(ex);
            }
        }

        private void stop_Click(object sender, EventArgs e)
        {
            try
            {
                productsServiceHost.Close();
                stop.IsEnabled = false;
                start.IsEnabled = true;
                status.Text = "Service Stopped";
            }
            catch (Exception ex)
            {
                handleException(ex);
            }
        }
    }
}

这里工作的原理是:在类内部有一个ServiceHost类型的member,每次揿开始按钮,会为其实例化一个ServiceHost,而揿关闭按钮时会执行Close方法。

注意这里实例化的时候传入的参数是typeof(ProductsServiceImpl),这里typeof将返回一个Type类型的对象,这样ServiceHost的构造函数就可以利用它获得ProductsServiceImpl的各种member和method,具体可参考:

http://msdn.microsoft.com/zh-cn/library/58918ffs(v=vs.80).aspx

3. 在书中的ProductsClient里并没有提到添加Service Reference,实际上也不可以添加,原因在于书中并没有提供该Service的元数据发布的Endpoint。实际上可以通过如下方法进行配置:

1)在ProductsServiceHost这个project里的App.config,通过configuration wizard进行配置的时候,首先在Services=>Host这个节点下添加BaseAddress为net.tcp://localhost:8080/TcpService

2)在Endpoints里添加一个发布给Service的Endpoint,其Binding为mexTcpBinding,发布用的Contract是ProductsServices.IProductService。这里地址栏使用空白,表明其Service的发布地址继承为net.tcp://localhost:8080/TcpService

3)在Endpoints里添加一个所提供的Service的Metadata的Endpoint,Binding为mexTcpBinding,发布用的Contract是IMetadataExchange,地址狼为Mex,表明其Service发布的地址为net.tcp://localhost:8080/TcpService

在经历过以上几步之后,启动Host,启动服务,就可以在Client端添加Add Service Reference了。如果缺少了发布Metadata的第三步,那么则不能添加Service Reference,但是在添加完成之后,将第三步设置的发布metadata的endpoint删除,并不会影响client端的工作。

posted on 2011-02-24 10:52  李志鹏  阅读(260)  评论(0编辑  收藏  举报

导航