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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM