2011年7月26日 星期二

WCF教學(8) - 使用Windows Service裝載服務

實作服務並不能讓服務執行,你需要能接聽特定port的請求,並將請求傳給服務。WCF中的HOST可以接聽多個port,並視需求使用不同通訊類型。Host負責建立Endpoint,接聽請求,把請求傳到服務的operation。
以下步驟介紹如何建立Windows Service類型的程式來當作WCF Host裝載服務,並利用NetNamedPipeBinding和用戶端溝通。
  • File->New->Project,建立Windows Service專案,名稱 WindowsServiceHost
  • File->Add->New Project 加入WCF Service Library類型專案,名稱MyWCFService ,使用預設的服務介面和程式碼來完成這個練習。
  • WindowsServiceHost ,加入MyWCFService參考
image
  • WindowsServiceHost ,加入System.ServiceModel.dll參考
image
  • WindowsServiceHost ,Service1.cs,在OnStart方法啟動接聽,使用NetNamedPipeBinding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
namespace WindowsServiceHost
{
    public partial class Service1 : ServiceBase
    {
        private ServiceHost myHost;        public Service1()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            myHost = new ServiceHost(typeof(MyWCFService.Service1));
            myHost.AddServiceEndpoint(typeof(MyWCFService.IService1),
                new NetNamedPipeBinding(),
                "net.pipe://localhost/complex"
                );
            myHost.Open();
        }
        protected override void OnStop()
        {
           myHost.Close();        }
    }
}



  • WindowsServiceHost ,加入Installer
image
  • WindowsServiceHost專案 ,ServiceInstaller1,StartType Property 設 Automatic
image
  • WindowsServiceHost 專案,ServiceProcessInstaller1,Account property 設 LocalSystem
image
  • WindowsServiceHost ,Build
  • 在Visual Studio 2010 Command Prompt安裝服務 ,執行 installutil WindowsServiceHost.exe
image
  • 在OS服務工具,Start the Service1
image
  • 加一個Console Application當Client,名稱ConClient
  • ConClient,Add Service Reference,產生proxy
image
  • ConClient,Edit app.config
image
  • ConClient,新增Client Endpoint
image
  • ConClient,設定呼叫資訊
image
  • ConClient,CTRL+S存檔,config檔看起來如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8732/Design_Time_Addresses/MyWCFService/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.pipe://localhost/complex" binding="netNamedPipeBinding"
                bindingConfiguration="" contract="ServiceReference1.IService1"
                name="WinBinding" />
        </client>
    </system.serviceModel>
</configuration>
  • ConClient,修改Program.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.IService1 proxy =
                new ServiceReference1.Service1Client("WinBinding");
            Console.WriteLine(proxy.GetData(10));
            Console.ReadLine();
        }
    }
}
  • 設ConClient為起始專案,因為裝載服務的WindowsServiceHost.exe已手動Run了
image
  • CTRL+F5執行,若跳出任何視窗,按yes,ConClient會印出You entered: 10,代表成功叫用服務



2 則留言:

匿名 提到...

Start the Service1
在這裡卡住
他無法啟動顯示1053的問題
在ConClient,設定呼叫資訊的時候Contract沒檔案可以選擇


米米貓學開發 提到...

不是很清楚您的問題是如何產生的,不過微軟有一篇KB http://support.microsoft.com/kb/839174/zh-tw
可能程式造成服務回應逾期
另一個問題,在ConClient,設定呼叫資訊的時候Contract沒檔案可以選擇==>先編譯你的專案

總瀏覽量