前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF系列教程之WCF服务配置工具

WCF系列教程之WCF服务配置工具

作者头像
郑小超.
发布2018-01-26 15:04:07
8090
发布2018-01-26 15:04:07
举报
文章被收录于专栏:GreenLeavesGreenLeaves

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

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

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

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

IService.cs如下:

代码语言:javascript
复制
using System;using System.ServiceModel;namespace IService
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        int Add(int a, int b); 
    }
}

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

Service.cs代码如下:

代码语言:javascript
复制
using System;

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

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

代码语言:javascript
复制
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文件中。文件内容如下:

代码语言:javascript
复制
<?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,说明配置成功,服务运行正常!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
测试服务
测试服务 WeTest 包括标准兼容测试、专家兼容测试、手游安全测试、远程调试等多款产品,服务于海量腾讯精品游戏,涵盖兼容测试、压力测试、性能测试、安全测试、远程调试等多个方向,立体化安全防护体系,保卫您的信息安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档