WCF實現多個服務


 

本篇體驗使用WCF實現2個服務。一個使用HTTP通訊,一個使用TCP通訊。

 

大致思路是:

 

→ 創建WCF服務以及接口,寫2個接口
→ 實現2個接口
→ 為WCF創建一個控制台項目的宿主,配置App.config,開啟宿主
→ 創建一個Web客戶端,創建對WCF服務的引用
→ 在Web客戶端,調用WCF服務,即調用WCF的代理類的實例方法

 

打開Visual Studio 2013,創建一個類庫項目。

 

在類庫下創建一個名稱為"HelloServie"的WCF服務。隨即,在項目中多了WCF相關的組件,以及自動創建了IHelloServie類和HelloServie類。

 

修改IHelloService接口如下:

 

namespace MyServices
{
    [ServiceContract]
    public interface IOneService
    {
        [OperationContract]
        string GetOne();
    }
    [ServiceContract]
    public interface ITwoService
    {
        [OperationContract]
        string GetTwo();
    }
}

 

HelloService類現在需要同時實現以上2個接口。

 

namespace MyServices
{
    // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的類名“HelloServie”。
    public class HelloServie : IOneService, ITwoService
    {
        public string GetOne()
        {
            return "本條信息通過HTTP傳遞";
        }
        public string GetTwo()
        {
            return "本條信息通過TCP傳遞";
        }
    }
}

 

現在需要一個宿主。在當前解決方案下創建一個控制台項目。

 

添加對WCF所在類庫的引用,添加對"System.ServiceModel"的引用。

 

設置控制台項目的配置文件。

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
      <services>
        <!--name="命名空間名稱.實現類名稱"-->
        <service name="MyServices.HelloServie" behaviorConfiguration="mexBehaviour">
          <endpoint address="MyServices" binding="basicHttpBinding" contract="MyServices.IOneService">            
          </endpoint>
          <endpoint address="MyServices" binding="netTcpBinding" contract="MyServices.ITwoService">         
          </endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:9090/"/>
              <add baseAddress="net.tcp://localhost:6060/"/>           
            </baseAddresses>
          </host>
        </service>
      </services>
    
      <behaviors>
        <serviceBehaviors>
          <behavior name="mexBehaviour">
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
</configuration>

 

其中,

● 通過Service節點的behaviorConfiguration屬性綁定behavior行為
● service節點的name屬性值的格式是:"命名空間名稱.實現類名稱"
● endpoint一般包含"abc"三個屬性,a代表address,b代表binding,c代表contract
● endpoint中的contract屬性值的格式是:"命名空間名稱.接口名稱"

 

編寫控制台項目如下:

 

using System.ServiceModel;
......
namespace MyHost
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(HelloServie)))
            {
                host.Open();
                Console.WriteLine("宿主已經開啟,開啟時間;" + DateTime.Now);
                Console.ReadKey();
            }
        }
    }
}

 

把控制台項目設置為啟動項目,並啟動控制台項目,即開啟宿主。


21


現在需要客戶端。開啟一個新的Visual Studio解決方案。

 

創建一個空的,名稱為"MyServiceClient"空網站。

22

 

一旦添加了對WCF服務的引用,在網站的Web.config中就自動多了system.serviceModel節點。


創建一個Web界面。

 

    <div>
        <asp:Button ID="Button1" runat="server" Text="獲取通過HTTP通訊的信息" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br /><br />
        <asp:Button ID="Button2" runat="server" Text="獲取通過TCP通訊的信息" OnClick="Button2_Click" />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>

 

編寫按鈕事件。

 

        //HTTP通訊信息
        protected void Button1_Click(object sender, EventArgs e)
        {
            MyServiceClient.MyService.OneServiceClient client = new MyService.OneServiceClient("BasicHttpBinding_IOneService");
            Label1.Text = client.GetOne();
        }
        //TCP通訊信息
        protected void Button2_Click(object sender, EventArgs e)
        {
            MyServiceClient.MyService.TwoServiceClient client = new MyService.TwoServiceClient("NetTcpBinding_ITwoService");
            Label2.Text = client.GetTwo();
        }

以上,聲明OneServiceClient和TwoServiceClient實例時, 構造函數傳遞的實參值是Web.config中endpoint的name屬性值。

 

點擊頁面上的2個按鈕如下:           

23


可見,一個WCF服務可實現多個服務。


免責聲明!

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



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