WCF系列教程之WCF服務配置工具 WCF系列教程之消息交換模式之請求與答覆模式(Request/Reply)

本文參考自http://www.cnblogs.com/wangweimutou/p/4367905.htmlhtml

Visual studio 針對服務配置提供了一個可視化的配置界面(Microsoft Service Configuration Editor),極大的方便開發者進行服務配置,接下來將演示如何對一個WCF服務程序進行配置:編輯器

全部與WCF服務有關的文件類,全都引入System.ServiceModel命名空間。ide

一、新建一個IService類庫,在裏面編寫服務的契約接口IService工具

IService.cs以下:post

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

二、新建一個Service類庫,實現IService中的服務契約接口測試

Service.cs代碼以下:url

using System;

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

 

三、搭建WCF服務宿主程序,這裏使用控制檯spa

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服務3d

4.經過WCF服務配置編輯器(Microsoft Service Configuration Editor)來配置服務程序,選擇visual studio 菜單中的工具選項下的WCF服務配置編輯器,點擊便可打開。code

(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,說明配置成功,服務運行正常!

相關文章
相關標籤/搜索