之前我们建立了一个WCF服务站点,通过把WCF服务站点布署到IIS中去,实现WCF服务在Web上的发布,这是一个非常简单实用的方法,我推荐将服务部署在IIS上,IIS会为我们提供服务器程序所具有的诸多特性,而无需我们自己手动去编写或者考虑。
那么如何编写一个桌面程序发布服务,为本机或远程机提供WCF服务呢,其实也很简单,下面我们就来建立一个简单的WPF程序,让他来发布和管理我们的WCF服务。

首先建立一个WPF程序和一个WCF服务库

image

把WcfServiceLibrary1服务库项目和System.ServiceModel 添加引用到WPF项目中

图片

好,向WPF 窗体随意拖动几个控件

image 

双击按钮进入代码页,添加事件代码如下

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 WcfServiceLibrary1; //导入WCF服务库项目命名空间
using System.ServiceModel;//导入WCF服务命名空间

namespace WpfApplication1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        //实例化ServiceHost服务承载器,并在构造函数中指定要发布的Service1
        ServiceHost host = new ServiceHost(typeof(Service1));
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //打开服务承载器,读取配置文件中的WCF服务的配置信息
            host.Open();
            label1.Content = "服务启动。。。";
            //host.Close();

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // 让服务停止
            host.Close();
            label1.Content = "服务已经停止。";
        }
    }
}

 

编译一下解决方案

 

给这个WPF项目添加一个App.config

image

App.Config上右击选择编辑WCF配置,弹出服务配置管理窗口,开始配置

image

由于该App.Config文件是我们新添加的一个配置文件,所以左边的服务项中都是空的。

点击右边的“新建服务...”弹出“新建服务元素向导”窗口一步步的配置服务。

在下面的窗体中我们选择wcfServiceLibrary1.dll 组件

image

结果如下

image

使用默认了,你也可以点击浏览选择

image

这里默认使用Http的方式,当然可以选择多种方式,回头咱们一起研究一下,每篇内容不要过多,只表达一个问题。

image

还是选择了默认

image 

选择终结点地址,告诉程序访问这个终结点的路径地址。这里的地址可以使用http://....格式的绝对地址,也可以像图中那样使用相对地址。

image

好了服务配置完成。

image

结果如下

image

这样就配置好了一个http通道下的终结点,但这终结点的地址我们使用的是相对地址,它们是相对于当前ServiceHost地址,所以我们还需要配置当前ServiceHost的地址.

点击左侧“配置”中的“主机”,在右边点击“新建”按钮,在弹出的窗口中写入ServiceHost的地址,在这里可以选择使用端口号,避免服务冲突。

image

与之前发布到IIS不同,除了可以使用Http方式发布WCF服务,这里我们还可以使用TCP、命名管道和微软消息队列进行信息传输。

添加服务行为。在左侧配置中选择“高级”-“服务行为”,再点击右侧的“新建服务行为分配”

image

点击“添加”弹出“添加行为元素扩展部份”窗口,选择“serviceMetaData”

image

为服务配置刚刚新建的行为

image 

文件菜单,保存配置文件,F5运行,结果如下,报错了

image

打开App.config 文件,删除红字部分,F5 运行

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="NewBehavior">
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="NewBehavior" name="WcfServiceLibrary1.Service1">
                <clear />
                <endpoint address="basic" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1"
                    listenUriMode="Explicit">
                    <identity>
                        <certificateReference storeName="My" storeLocation="LocalMachine"
                            x509FindType="FindBySubjectDistinguishedName" />
                    </identity>
                </endpoint>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://PCServer:8008/service" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

好,程序可以运行了,点击启动服务。

image

我们在浏览器中输入地址,结果如下图。

image 

根据提示添加下面绿色的字体部分

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="NewBehavior">
                    <serviceMetadata  httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="NewBehavior" name="WcfServiceLibrary1.Service1">
                <clear />
                <endpoint address="basic" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1"
                    listenUriMode="Explicit">
                </endpoint>
                <host>
                    <baseAddresses>
                        <add baseAddress="http://PCServer:8008/service" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

F5 重新运行,并使用浏览器访问,看看结果如何呢。好的,已经成功了

image

下面我们再通过wcftestclient来测试一下。


保证程序在运行,然后在VS2008的DOS窗口,输入wcftestclient http://PCServer:8008/servicewcftestclient 命令后面是要测试的服务地址)

image

好的,调用GetData方法,结果运行正常。

 

源代码:WpfApplication1.rar

posted on 2009-12-25 13:13  冯瑞涛  阅读(5581)  评论(1编辑  收藏  举报