WCF中的ServiceHost初始化两种方式 .
在宿主程序中初始化ServiceHost有直接写代码和使用配置文件两种方式。使用ServiceHost首先要引用System.ServiceModel 命名空间。
1 代码方式
using(ServiceHost host=new ServiceHost(typeof(HelloWordService)))
{
host.AddServiceEndpoint(typeof(IHelloWordService),
new BasicHttpBinding(), new Uri("http://localhost:10000/HelloWorldService"));
host.AddServiceEndpoint(typeof(IHelloWordService),
new NetTcpBinding(), new Uri("net.tcp://localhost:10001/HelloWorldService"));
if (host.State != CommunicationState.Opening)
host.Open();
}
2 配置文件方式
配置文件代码:
<services>
<service behaviorConfiguration="serverBehavior" name="HelloWordService">
<endpoint address="http://localhost:10000/HelloWorldService"
binding="basicHttpBinding" contract="IHelloWordService"></endpoint>
<endpoint address="net.tcp://localhost:10001/HelloWorldService"
binding="netTcpBinding" contract="IHelloWorldService"></endpoint>
</service>
</services>当然也可以使用基地址的方式来配置
<services>
<service behaviorConfiguration="serverBehavior" name="HelloWordService">
<endpoint address="HelloWorldService"
binding="basicHttpBinding" contract="IHelloWordService"></endpoint>
<endpoint address="HelloWorldService"
binding="netTcpBinding" contract="IHelloWorldService"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:10000/"/>
<add baseAddress="net.tcp://localhost:10001/"/>
</baseAddresses>
</host>
</service>
</services>配置好配置文件后就宿主程序中就很简单了,如下:
using(ServiceHost host=new ServiceHost(typeof(HelloWordService)))
{
if (host.State != CommunicationState.Opening)
host.Open();
}
分享到:
相关推荐
WCF4高级编程Professional.WCF.4.rar
[WCF服务编程].(Programming.WCF.Services).Juval.Lowy.文字版.pdf.7z
Wrox.Professional.WCF.4.Windows.Communication.Foundation.with.NET.4.
Packtpub.WCF.Multi.tier.Services.Development.with.LINQ.Dec.2008.rar
WCF 控制台 和 应用程序两种, 好玩分享下。 呵呵呵,
Apress.Pro.WCF.Practical.Microsoft.SOA.Implementation.Jan.rar
Wrox.Professional.WCF.4.Windows.Communication.Foundation.with.NET.4
Apress.Pro.WCF.4.Practical.Microsoft.SOA.Implementation
Packtpub.WCF.4.0.Multi.tier.Services.Development.with.LINQ.to.Entities.Jun.2010.rar
Packtpub.WCF.Multi.tier.Services.Development.with.LINQ.Dec.2008
1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统...6.自此就完成了部署和代理类生成的所有操作,我们只需要在项目中实例化 ClientCode.cs 中的服务类 ,并调用其中的方法即可
Title: WCF Multi-Layer Services Development with Entity Framework, 4th Edition Author: Mike Liu Length: 388 pages Edition: 4 Language: English Publisher: Packt Publishing Publication Date: 2014-10-27 ...
《WCF揭密》分两个压缩包 《WCF揭密》.part1.rar 《WCF揭密》.part2.rar 请分别下载。
WCF学习资料笔记合集 WCF资料.part2.rar
然后要在服务项目中初始化ServiceHost并打开指定服务,如果存在多个服务的情况还需要初始化多个ServiceHost。 本工具就是为了解决这系列问题,快速发布、自动生成托管需要发布的服务的运行引擎和管理工具。 目前...
7.WCF数据契约与序列化 8.使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据 9.WCF服务实例激活类型编程与开发 10.请求应答(Request-Reply) 、单向操作(One-Way)、回调操作(Call Back) 11.WCF...
.NET下rest 风格的服务 内含WCF和 web API 两种方式的demo.由于可能给同事讲解,因此不收分了。
This book is a step-by-step tutorial to guide you through learning WCF, Entity Framework, LINQ, and LINQ to Entities. You will be guided to create six WCF and Entity Framework solutions from scratch, ...