<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引頁]</a>
化零為整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
介紹
WCF(Windows Communication Foundation) - 宿主(Hosting):WCF服務可以宿主在IIS, Application, WAS, WindowsService。本文以宿主在WindowsService為例。
示例
1、服務
IHello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Sample
{
/// <summary>
/// IHello接口
/// </summary>
[ServiceContract]
public interface IHello
{
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="name">人名</param>
/// <returns></returns>
[OperationContract]
string SayHello(string name);
}
}
Hello.cs
/// Hello類
public class Hello : IHello
public string SayHello(string name)
{
return "Hello: " + name;
}
2、宿主
Hello.cs(WindowsService)
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.ServiceProcess;
namespace WCF.ServiceHostByWindowsService.Sample
/// 初始化 System.Configuration.Install.Installer 類的新執行個體。
[RunInstaller(true)]
public class ProjectInstaller : Installer
private ServiceProcessInstaller process;
private ServiceInstaller service;
/// 構造函數
public ProjectInstaller()
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCF.ServiceHostByWindowsService";
service.Description = "WCF服務宿主在WindowsService[webabcd測試用]";
base.Installers.Add(process);
base.Installers.Add(service);
/// Windows服務類
public class WindowsService : ServiceBase
public ServiceHost serviceHost = null;
/// 主函數
public static void Main()
ServiceBase.Run(new WindowsService());
public WindowsService()
base.ServiceName = "WCF.ServiceHostByWindowsService";
/// 啟動Windows服務
/// <param name="args">args</param>
protected override void OnStart(string[] args)
if (serviceHost != null)
{
serviceHost.Close();
}
// 為WCF.ServiceLib.Sample.Hello建立ServiceHost
serviceHost = new ServiceHost(typeof(WCF.ServiceLib.Sample.Hello));
serviceHost.Open();
ServiceHost的幾個事件(顧名思義)
/// 停止Windows服務
protected override void OnStop()
serviceHost = null;
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!--name - 提供服務的類名-->
<!--behaviorConfiguration - 指定相關的行為配置-->
<service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior">
<!--address - 服務位址-->
<!--binding - 通信方式-->
<!--contract - 服務契約-->
<endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Sample.IHello" />
<!--中繼資料交換的endpoint-->
<!--注:address是mex,它會和host/baseAddresses節點中的baseAddress做拼接,即提供中繼資料交換的位址為:http://localhost:12345/Binding/mex-->
<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:11233/ServiceHostByWindowsService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SampleBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
3、用戶端
Hello.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs"
Inherits="Hosting_Hello" Title="宿主Hosting(服務宿主在WindowsService)" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<ul>
<li style="color: Red;">本例為宿主在WindowsService的示例</li>
<li>宿主在IIS請參見本解決方案的ServiceHost項目</li>
<li>宿主在應用程式請參見本解決方案的ServiceHost2項目</li>
<li>應用程式自宿主就是把本解決方案的ServiceLib項目和ServiceHost2項目結合在一起</li>
<li>宿主在Windows Activation Services(WAS),因為我沒有環境,就先不寫示例了</li>
</ul>
</div>
<asp:TextBox ID="txtName" runat="server" Text="webabcd" />
<asp:Button ID="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" />
</asp:Content>
Hello.aspx.cs
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Hosting_Hello : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnSayHello_Click(object sender, EventArgs e)
var proxy = new HostingByWindowsService.HelloClient();
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format("alert('{0}')", proxy.SayHello(txtName.Text)),
true);
proxy.Close();
Web.config
<?xml version="1.0"?>
<client>
<!--address - 服務位址-->
<!--binding - 通信方式-->
<!--contract - 服務契約-->
<endpoint address="http://localhost:11233/ServiceHostByWindowsService/" binding="wsHttpBinding" contract="Sample.IHello" />
</client>
運作結果:
啟動"WCF.ServiceHostByWindowsService"服務,單擊"Hello"按鈕後彈出提示框,顯示"Hello: webabcd"
OK
<a href="http://down.51cto.com/data/100781" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344113,如需轉載請自行聯系原作者