zoukankan      html  css  js  c++  java
  • WCF Service Configuration Editor的使用

    原文:http://www.cnblogs.com/Ming8006/p/3772221.html

    通过WCF Service Configuration Editor的配置
    修改Client端

    参考

    在上篇文章创建一个简单的WCF程序中, 通过编码的方式进行终结点的添加和服务行为的定义,但在进行真正的WCF应用开发时,一般会直接是通过配置的方式进行。

    对于初学者来说,WCF的配置显得过于复杂,直接对配置文件进行手工编辑不太现实。在这种情况下,可以直接使用VS提供的配置工具WCF Service Configuration Editor工具生成XML文件来进行WCF的配置。

    通过WCF Service Configuration Editor的配置[1]

    以下是使用WCF Service Configuration Editor的的操作步骤:

    1.打开VS,在Hosting项目中右键,新建一个App.config文件。

    2.点击菜单栏Tools选项,在下拉菜单中选择WCF Service Configuration Editor。

    3.在弹出的工具窗口中选择“File->open->Config File”。找到刚才建的App.config文件,并打开。

    4.新建一个服务,如下图所示,先点击“创建新的服务”链接,再找到Service项目中的WcfServices.Services.CalculatorService服务。

    5.点击下一步,找到Contracts项目中的ICalculator契约。

    6.下一步,选择Http的通信方式。

    7.点击下一步,选择Basic Web Service Interoperability。

    8.点击下一步,输入服务端Endpoint地址: http://localhost:8080/calculatorservice 。下一步Finish。

    9.为服务添加行为(Behavior),这步很重要。在Advanced目录下,右键新建一个Service行为,New Service Behavior Configuraton,然后对行为重命名为CalculatorBehavior。新建一个Stack Element 'serviceMetadata', 并设置它的HttpGetEnabled为true。如下图所示:

     

    10.这些做好了之后,我们回到最上面的Service目录,为Calculator服务添加刚才配的CalculatorBehavior行为配置。如下图所示:

    11.接着配置Host的地址,选中Host,然后点击右下方的New Base Address,输入: http://localhost:8080/calculatorservice

    12.可以新添加一个服务端的Endpoint,用于配置WS-MetadataExchange,当然也可以不加。在Services目录下的Endpoint右键,新建一个Endpoint,名字和地址随意,保证Binding是mexHttpBinding。  

    13.Ctrl+S保存,这样App.config文件就自动写满了,如下:

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3     <system.serviceModel>
     4         <behaviors>
     5             <serviceBehaviors>
     6                 <behavior name="CaclulaterBehavior">
     7                     <serviceMetadata httpGetEnabled="true" />
     8                 </behavior>
     9             </serviceBehaviors>
    10         </behaviors>
    11         <services>
    12             <service behaviorConfiguration="CaclulaterBehavior" name="WcfServices.Services.CalculatorService">
    13                 <endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding"
    14                     bindingConfiguration="" contract="WcfServices.Contracts.ICalculator" />
    15                 <host>
    16                     <baseAddresses>
    17                         <add baseAddress="http://localhost:8080/calculatorservice" />
    18                     </baseAddresses>
    19                 </host>
    20             </service>
    21         </services>
    22     </system.serviceModel>
    23 </configuration>
    View Code

    14。修改Hosting类的代码,删改后如下:

     1 using System;
     2 using System.ServiceModel;
     3 using WcfServices.Services;
     4 
     5 namespace WcfServices.Hosting
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
    12             {
    13                 host.Opened += delegate
    14                 {
    15                     Console.WriteLine("CalculaorService have started, press any key to stop it!");
    16                 };
    17  
    18                 host.Open();
    19                 Console.Read();
    20             }
    21         }
    22     }
    23 }
    View Code

    注意

    1 <endpoint address="" binding="basicHttpBinding" name="Calculator"   Contract="Contracts.ICalculator" >
    2   <host>
    3       <baseAddress>
    4           <add baseAddress="http://localhost:8080/CalculatorService"/>
    5       </baseAddress>
    6   </host>

    endpiont的地址为空,只是配了Host的基地址。当然也可以直接配Endpoint的地址,不配Host的基地址。但如果host了多个服务呢?有多了Endpoint挂在同一个host下,那么配基地址就显得很重要。

    修改Client端 


    返回

    打开客户端的项目(服务端不要关闭),选择Client项目下的Service Reference,在你的服务命名空间上右键,点击Update Service Reference。会生成新的app.config文件。

    然后Rebuild Client端项目即可,ClientApp的代码无须改变。

    配置客户端[2]

    在真正的WCF应用中,大都采用配置的方式进行终结点的定义。对于客户端,我们也可以通过下面的配置指定终结点的三要素,并为相应的终结点指定一个终结点配置名称(calculatorservice)。 在客户端项目添加Application Configuration file (App.config),内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <client>
          <endpoint address="http://localhost:8080/calculatorservice" binding="basicHttpBinding" contract="WcfServices.Contracts.ICalculator" name="calculatorservice" />
        </client>
      </system.serviceModel>
    </configuration>

    对客户端项目,我们将添加的服务引用移除,并为Client项目添加对Contracts项目的引用。借助于这个服务契约,并通过ChannelFactory<ICalculator>创建服务代理对象,直接进行相应的服务调用。

    using System;
    using System.ServiceModel;
    using WcfServices.Contracts;
    
    namespace WcfServices.Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ChannelFactory<ICalculator> channelFactory  = new ChannelFactory<ICalculator>("calculatorservice"))
                {
                    ICalculator proxy = channelFactory.CreateChannel();
                    using (proxy as IDisposable)
                    {
                        Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
                    }
                    Console.Read();
                }
            }
        }
    }
    View Code

     源代码:SimpleWCFWithConfig.zip

    参考:

    [1] WCF Service Configuration Editor的使用  http://www.cnblogs.com/judastree/archive/2012/08/26/2657555.html

    [2] 我的WCF之旅(1):创建一个简单的WCF程序  http://www.cnblogs.com/artech/archive/2007/06/14/782845.html 

  • 相关阅读:
    html file 表单样式(css过滤器实现)
    网页a标签
    Criteria示例
    数类型转换顺序
    Mysql命令行添加用户
    MySQL中导出用户权限设置的脚本
    mysql 命令行登录详解
    mysql的auto-rehash简介
    UltraEdit中的特殊字符
    ps aux详解(进程状态说明)
  • 原文地址:https://www.cnblogs.com/Percy_Lee/p/4863445.html
Copyright © 2011-2022 走看看