• WCF开发实战系列三:自运行WCF服务


    WCF开发实战系列三:自运行WCF服务

    (原创:灰灰虫的家 http://hi.baidu.com/grayworm
    上一篇文章中我们建立了一个WCF服务站点,为WCF服务库运行提供WEB支持,我们把这个WCF服务站点布署到IIS中去,实现WCF服务在Web上的发布。
    这一篇文章中我们来谈一下“自运行WCF服务”。什么是“自运行WCF服务”呢?就是通过我们编写的控制台程序或WinForm程序来为本机或远程机提供WCF服务的方式。

    下面我们使用控制台程序来提供WCF的自运行服务

    第一步:建立控制台应用程序ConsoleBookServiceHost


    《图1》
    第二步:向ConsoleBookServiceHost程序中添加两个引用:一个是WCF服务库Services项目的引用,另一个是System.ServiceModel引用。
    第三步:在ConsoleBookServiceHost 项目中的Program.cs中编写代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Services; //导入WCF服务库项目命名空间
    using System.ServiceModel;//导入WCF服务命名空间

    namespace ConsoleBookServiceHost
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                //实例化ServiceHost服务承载器,并在构造函数中指定要发布的BookService
                ServiceHost host = new ServiceHost(typeof(BookService));
                //打开服务承载器,读取配置文件中的WCF服务的配置信息
               host.Open();
                Console.WriteLine("服务已启动......");
                Console.ReadLine();
                host.Close();
            }
        }
    }

    第四步:在ConsoleBookServiceHost中添加配置文件App.Config。


    《图2》

    第五步:在App.Config上右击选择选择“编辑WCF配置”,弹出服务配置管理窗口


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

    第六步:点击右边的“新建服务...”弹出“新建服务元素向导”窗口,单击“浏览”按钮,选择Bin/Debug目录下Services.dll程序集中的Services.BookService服务。


    《图4》

    第七步:单击“下一步”,是“选择服务契约”项,单击“浏览”按钮,选择Bin/Debug目录下Services.dll程序集中的Services.IBookService服务契约。


    《图5》

    第八步:单击“下一步”,是“选择通信模式”的界面。在这里我们先选择Http


    《图6》

    第九步:单击“下一步”,是选择Http通信的“互操作方式”。界面中有两项供我们选择:基本Web互操作性和高级web互操作性。在这里我们选择第一项


    《图7》

    第十步:单击“下一步”,是“选择终结点地址”,也就告诉程序访问这个终结点的路径地址。这里的地址可以使用http://....格式的绝对地址,也可以像图中那样使用相对地址。


    《图8》
    第十一步:单击“下一步”,出来配置成功界面,单击“完成”完成此次配置


    《图9》
    此时WCF配置窗口中会出现一个服务节点,其中的终结点就是我们刚才配置信息A=basic B=basicHttpBinding C=Service.IBookService


    《图10》

    第十二步:下面我们再使用向导添加一个终结点
    点击左侧的“服务”节点,出现界面如下:


    《图11》
    点击“新建服务终结点...”处弹出向导,选择服务契约



    《图5》
    点击“下一步”出现界面“选择通信模式”,这里我们还是选择Http



    《图6》
    点击“下一步”出现界面“互操作方式”,这里我们选择“高级web互操作性”,这种互操作方式的安全性比较高


    《图12》
    点击“下一步”出现界面“选择终结点地址”,这里我们使用相对地址,填写 ws


    《图13》
    点击“下一步”出现配置成功界面,单击“完成”完成此次配置


    《图14》
    此时WCF配置窗口中又会多出一个服务节点,其中的终结点就是我们刚才配置信息A=ws B=ws2007HttpBinding C=Service.IBookService


    《图15》

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

    第十三步:配置ServiceHost的地址:
    点击左侧“配置”中的“主机”,在右边点击“新建”按钮,在弹出的窗口中写入ServiceHost的地址,在这里可以选择使用端口号。


    《图16》
    这样我们两个终结点算是配置完成了。

    “自运行WCF服务”与上一篇说的“在IIS布运行WCF服务”不一样的是,“自运行WCF服务"除了可以使用Http方式发布WCF服务,可以使用TCP、命名管道和微软消息队列进行信息传输。
    下面我们再配置两个终结点,一个是使用TCP通信模式,另一个使用命名管道通信模式。

    第十四步:添加一个新的TCP终结点,配置如下:


    《图17》

    第十五步:添加一个新的命名管道终结点,配置如下:


    《图18》

    到此为至,我们已经为该WCF服务建立了四个数据传输的终结点:


    《图19》

    下面我们为该ServiceHost程序配置“元数据终结点”,以向客户端发送服务元数据信息

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


    《图20》


    《图21》

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


    《图22》

    第十七步:为服务配置刚刚新建的行为。


    《图23》

    第十八步:配置元数据终结点:


    《图24》


    《图25》


    《图26》


    《图27》


    《图28》


    《图29》

    此时我们运行的时候,会出现错误如下(无法加载配置中的x.509证书标识):


    《图30》
    我们看一下App.config配置文件中的代码如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="NewBehavior">
                        <serviceMetadata />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="NewBehavior" name="Services.BookService">
                    <clear />
                    <endpoint address="basic" binding="basicHttpBinding" contract="Services.IBookService"
                        listenUriMode="Explicit">
                        <identity>
                            <certificateReference storeName="My" storeLocation="LocalMachine"
                                x509FindType="FindBySubjectDistinguishedName" />
                        </identity>
                    </endpoint>
                    <endpoint address="ws" binding="ws2007HttpBinding" contract="Services.IBookService"
                        listenUriMode="Explicit">
                        <identity>
                            <certificateReference storeName="My" storeLocation="LocalMachine"
                                x509FindType="FindBySubjectDistinguishedName" />
                        </identity>
                    </endpoint>
                    <endpoint address="net.tcp://localhost:8082/BookService" binding="netTcpBinding"
                        contract="Services.IBookService" listenUriMode="Explicit">
                        <identity>
                            <certificateReference storeName="My" storeLocation="LocalMachine"
                                x509FindType="FindBySubjectDistinguishedName" />
                        </identity>
                    </endpoint>
                    <endpoint address="net.pipe://localhost/BookService" binding="netNamedPipeBinding"
                        contract="Services.IBookService" listenUriMode="Explicit">
                        <identity>
                            <certificateReference storeName="My" storeLocation="LocalMachine"
                                x509FindType="FindBySubjectDistinguishedName" />
                        </identity>
                    </endpoint>
                    <endpoint address="mex" binding="basicHttpBinding" bindingConfiguration=""
                        contract="IMetadataExchange" />
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8081/BookService" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
    </configuration>

    把其中的
    <identity>
        <certificateReference storeName="My" storeLocation="LocalMachine"
            x509FindType="FindBySubjectDistinguishedName" />
    </identity>
    都删掉。

    按Ctrl+F5运行即可。

    至此,我们把用来提供 WCF服务的运行程序编写完成了。


    测试一下。
    选按Ctrl+F5运行我们上面编写的ServerHost程序
    打开浏览器,在地址栏中输入第十三步中ServerHost中的地址,出现如下界面:


    《图31》
    从图中我们可以看出,这个Service页面就是通过我们上面的ServiceHost程序来提供的WCF服务


    下面我们再通过wcftestclient来测试一下
    选按Ctrl+F5运行我们上面编写的ServerHost程序
    再打开VS2008的DOS窗口,输入wcftestclient http://localhost:8081/BookService出现界面如下:


    《图32》
    从图中我们可以看出,我们为BookService配置的四个终点都显示出来了,我们可以使用不同的终结点添加书目信息,然后再用其它的终结点取出书目信息。因为这四个不同通道的终结点都是操作同一个服务。

  • 相关阅读:
    LeetCode 461. Hamming Distance
    LeetCode 442. Find All Duplicates in an Array
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode Find the Difference
    LeetCode 415. Add Strings
    LeetCode 445. Add Two Numbers II
    LeetCode 438. Find All Anagrams in a String
    LeetCode 463. Island Perimeter
    LeetCode 362. Design Hit Counter
    LeetCode 359. Logger Rate Limiter
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2742780.html
Copyright © 2020-2023  润新知