zoukankan      html  css  js  c++  java
  • WCF系列教程之WCF服务配置工具

    本文参考自http://www.cnblogs.com/wangweimutou/p/4367905.html

    Visual studio 针对服务配置提供了一个可视化的配置界面(Microsoft Service Configuration Editor),极大的方便开发者进行服务配置,接下来将演示如何对一个WCF服务程序进行配置:

    所有与WCF服务有关的文件类,全都引入System.ServiceModel命名空间。

    1、新建一个IService类库,在里面编写服务的契约接口IService

    IService.cs如下:

    using System;using System.ServiceModel;namespace IService
    {
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            int Add(int a, int b); 
        }
    }

    2、新建一个Service类库,实现IService中的服务契约接口

    Service.cs代码如下:

    using System;
    
    namespace Service
    {
        public class Service:IService.IService
        {
            public int Add(int a, int b)
            {
                return a + b;
            }
        }
    }

    3、搭建WCF服务宿主程序,这里使用控制台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Host
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(Service.Service))) 
                {
                    host.Open();
                    Console.WriteLine("服务已启动,按任意键中止...");
                    Console.ReadKey(true);
                    host.Close();
                }
            }
        }
    }

     ok,生成整个解决方案,一定要生成,要不然下面的操作无法进行。服务契约和服务类和宿主全部搭建成功,下面开始配置WCF服务

    4.通过WCF服务配置编辑器(Microsoft Service Configuration Editor)来配置服务程序,选择visual studio 菜单中的工具选项下的WCF服务配置编辑器,点击即可打开。

    (1)、文件->新建配置

    (2)、新建服务、选择服务类型,也就是具体要对外发布的服务内容

    该服务类型在Service层的bin目录下

    (3)、选择对应的服务契约,选择完服务类型后,系统会自动匹配

    (4)、选择服务的通信模式

    根据程序的通讯模式选择不同的通讯类型,这里采用HTTP

    (5)、服务端与客户端的通信模式

    i、基本的Web服务互操作性:设置当前程序的通信模式为请求与答复模式,具体请参考WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)

    ii、高级Web服务互操作性:分为单工通信和双工通信

    这里选择请求与答复模式

    (6)、设置服务终结点的地址

    当前程序的设置为基地址,所以终结点的地址设置为空。

    (7)、向导配置完毕

    点击完成,就完成了一个服务配置文件的创建,接下来就开始配置各个节点和属性元素。

    (8)、添加基地址

    配置服务的基地址,点击左边服务菜单项的主机选项,然后点击右下角的新建按钮添加基地址。

    点击新建

    此处选用本地Ip地址,端口号为666,ok主机基地址设置完毕,对应host节点中的baseadress节点中的配置

    (8)、修改终结点中的binding属性

    修改默认终结点的绑定类型为wsHttpBinding,把标识中的DNS设置为Localhost.

     

    (9)、添加元数据终结点配置

    添加元数据终结点配置,选择左侧终结点菜单选项,右键选择新建服务终结点。设置Address为mex,Binding 设置为mexHttpBinding,Contract设置为IMetadataExchange

     

    (10)、添加绑定配置

    添加绑定配置,选择左侧的绑定菜单项,新建绑定配置

    点击确定

    (11)、配置终结点行为

    配置终结点行为,选择左侧的高级选项的终结点行为配置新建终结点行为配置,将名称设置为endpointBehavior,点击添加按钮添加终结点行为

     

    终结点行为的属性有很多,这里只选择数据序列化大小的选项

    默认最大长度为2147483647,这里设置成214748366

    (12)、添加服务行为配置

    添加服务行为配置,选择左侧服务行为菜单项新建服务行为配置。设置名称为serviceBehavior,点击添加按添加服务行为。

    (13)、为当前服务类型绑定服务行为

    为服务选择BehaviorConfiguration的选项为serviceBehavior。点击左侧的Service.Service选择,将右侧的BehaviorConfiguration选择设置为serviceBehavior

    (14)、为当前服务终结点

    为终结点选择绑定配置和行为配置,点击左侧的第一个终结点,将右侧的BehaviorConfiguration设置为endpointBehavior、BindingConfiguration设置为mybinding.

    (15)、配置完成,保存至桌面,并将配置内容复制到宿主的App.config文件中。文件内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="endpointBehavior">
              <dataContractSerializer maxItemsInObjectGraph="214748366" />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <wsHttpBinding>
            <binding name="mybinding" />
          </wsHttpBinding>
        </bindings>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="Service.Service">
            <endpoint address="" behaviorConfiguration="endpointBehavior"
                binding="wsHttpBinding" bindingConfiguration="mybinding" contract="IService.IService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
                contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://127.0.0.1:666/WcfConfig/" />
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>
    </configuration>

    (16)、利用WCF客户端测试服务是否能正常运行。

    ok,说明配置成功,服务运行正常!

  • 相关阅读:
    jvisualvm工具使用
    Java四种引用包括强引用,软引用,弱引用,虚引用。
    <实战> 通过分析Heap Dump 来了解 Memory Leak ,Retained Heap,Shallow Heap
    什么是GC Roots
    Memory Analyzer tool(MAT)分析内存泄漏---理解Retained Heap、Shallow Heap、GC Root
    JDK自带工具之问题排查场景示例
    websocket协议握手详解
    ssh 登陆服务器原理
    新版本macos无法安装mysql-python包
    如何将多个小值存储进一个值中
  • 原文地址:https://www.cnblogs.com/GreenLeaves/p/6854559.html
Copyright © 2011-2022 走看看