[索引页]
[源码下载]


化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)


作者:webabcd


介绍
WCF(Windows Communication Foundation) - 宿主(Hosting):WCF服务可以宿主在IIS, Application, WAS, WindowsService。本文以宿主在WindowsService为例。


示例
1、服务
IHello.cs
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)using System;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Collections.Generic;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Linq;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Text;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.ServiceModel;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
namespace WCF.ServiceLib.Sample

Hello.cs
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)using System;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Collections.Generic;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Linq;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Text;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.ServiceModel;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
namespace WCF.ServiceLib.Sample


2、宿主
Hello.cs(WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)using System;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Collections.Generic;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Linq;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Text;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.ComponentModel;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Configuration;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Configuration.Install;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.ServiceModel;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.ServiceProcess;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
namespace WCF.ServiceHostByWindowsService.Sample

App.config
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)<?xml version="1.0" encoding="utf-8" ?>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
<configuration>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)  
<system.serviceModel>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
<services>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<!--name - 提供服务的类名-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<!--behaviorConfiguration - 指定相关的行为配置-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior">
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<!--address - 服务地址-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<!--binding - 通信方式-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<!--contract - 服务契约-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Sample.IHello" />
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<!--元数据交换的endpoint-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<host>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)          
<baseAddresses>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)            
<add baseAddress="http://localhost:11233/ServiceHostByWindowsService/"/>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)          
</baseAddresses>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
</host>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
</service>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
</services>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
<behaviors>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<serviceBehaviors>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
<behavior name="SampleBehavior">
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)          
<serviceMetadata httpGetEnabled="True"/>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)          
<serviceDebug includeExceptionDetailInFaults="False" />
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)        
</behavior>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
</serviceBehaviors>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
</behaviors>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)  
</system.serviceModel>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
</configuration>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)


3、客户端
Hello.aspx

Hello.aspx.cs
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)using System;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Collections;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Configuration;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Data;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Linq;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web.Security;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web.UI;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web.UI.HtmlControls;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web.UI.WebControls;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Web.UI.WebControls.WebParts;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
using System.Xml.Linq;
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
public partial class Hosting_Hello : System.Web.UI.Page

Web.config
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)<?xml version="1.0"?>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
<configuration>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)  
<system.serviceModel>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
<client>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<!--address - 服务地址-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<!--binding - 通信方式-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<!--contract - 服务契约-->
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)      
<endpoint address="http://localhost:11233/ServiceHostByWindowsService/" binding="wsHttpBinding" contract="Sample.IHello" />
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)    
</client>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)  
</system.serviceModel>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)
</configuration>
化零为整WCF(5) - 宿主Hosting(宿主在IIS, Application, WAS, WindowsService)


运行结果:
启动"WCF.ServiceHostByWindowsService"服务,单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"


OK
[源码下载]

相关文章: