WCF服务承载 (转载)

我们对宿主的概念、环境、特点做了文字性的介绍和概括,接下来我们将通过实例对这几种寄宿方式进行介绍。为了更好的说明各寄宿环境特点,本实例采用Http和net.tcp两种服务通讯方式,同时寄宿在不同的宿主中。程序结构如下:

服务契约的接口和实现代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace Service{    [ServiceContract]    public interface IServiceCalculator    {        [OperationContract]        double Add(double n1, double n2);    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;namespace Service{    [ServiceContract]    public interface IServiceMessage    {        [OperationContract]        string ReturnMessage();    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{    public class ServiceCalculator:IServiceCalculator    {        public double Add(double n1, double n2)        {            return n1 + n2;        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{    public class ServiceMessage:IServiceMessage    {        public string ReturnMessage()        {            return "调用服务计算结果如下";        }    }}

在 IIS 中承载 WCF 服务和WAS 中承载 WCF 服务

  1. 完成IISHost代码
  • 引用Service程序集
  • 添加ServiceCalculator.svc新文件,代码如下

<%@ ServiceHost Language="C#" Debug="true"  Service="Service.ServiceCalculator" %>

  • 添加ServiceCalculator.svc新文件,代码如下

<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServiceMessage" %>

<?xml version="1.0" encoding="utf-8"?><configuration>    <system.serviceModel>      <services>        <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior">          <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" />          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />        </service>         <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior">          <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" />          <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />        </service>      </services>       <bindings>        <netTcpBinding>          <binding name="PortSharingBinding" portSharingEnabled="true">            <security mode="None" />          </binding>        </netTcpBinding>      </bindings>       <behaviors>          <serviceBehaviors>              <behavior name="mexBehavior">                  <serviceMetadata httpGetEnabled="true" />                  <serviceDebug includeExceptionDetailInFaults="false" />              </behavior>          </serviceBehaviors>      </behaviors>          </system.serviceModel></configuration>

  2.  寄宿服务

  • 生成IISHost程序,将bin文件目录、ServiceCalculator.svc、ServiceMessage.svc、Web.config拷贝到新建的WCFHost文件夹中
  • 新建网站配置该程序以便承载服务。

点击IIS菜单的应用程序池,找到WCFHost程序池,将.net framework版本设置为v4.0,托管管道模式设置为集成

在浏览器中输入http://localhost:1234/ServiceMessage.svc可以看到服务发布成功

  这是因为ServiceCalculator.svc启用的是net.tcp通讯,而在IIS中启用net.tcp通讯就必须依靠Windows 进程激活服务(也称为 WAS)

  • 要使用WAS寄宿程序,就需要配置几个地方

    在控制面板->程序和功能->打开或关闭windows功能勾选以下几个功能,安装WCF 激活组件

        

    配置承载服务的WCFHost网站,添加net.tcp通讯。

    

    点击网站的高级设置,在已启用的协议后追加net.tcp协议

    

  3. 客户端验证服务

  • 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端

在托管应用程序中承载 WCF 服务

  1. 完成AppHost代码

  • 添加对service程序集的引用,配置文件App.config代码如下

<?xml version="1.0" encoding="utf-8"?><configuration>  <system.serviceModel>    <services>      <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior">        <host>          <baseAddresses>            <add baseAddress="http://localhost:2234/ServiceMessage/"/>          </baseAddresses>        </host>        <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" />        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      </service>       <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior">        <host>            <baseAddresses>              <add baseAddress="http://localhost:1235/ServiceCalculator/"/>              <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/>            </baseAddresses>          </host>         <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" >          <identity>            <dns value="localhost"/>          </identity>        </endpoint>        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      </service>    </services>     <bindings>      <netTcpBinding>        <binding name="PortSharingBinding" portSharingEnabled="true" >          <security mode="None" />        </binding>      </netTcpBinding>    </bindings>     <behaviors>      <serviceBehaviors>        <behavior name="mexBehavior">          <serviceMetadata httpGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>      </serviceBehaviors>    </behaviors>   </system.serviceModel></configuration>

  • Program.cs代码如下

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Service;using System.ServiceModel;namespace AppHost{    class Program    {        static void Main(string[] args)        {            try            {                ServiceHost MessageHost = new ServiceHost(typeof(ServiceMessage));                ServiceHost CalculatorHost = new ServiceHost(typeof(ServiceCalculator));                 MessageHost.Open();                CalculatorHost.Open();                Console.WriteLine("服务已经启动。。。");                Console.ReadLine();                MessageHost.Close();                CalculatorHost.Close();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                Console.Read();            }           }    }}

  2.  寄宿服务

  • 生成AppHost工程,找到bin目录下的AppHost.exe,点击运行,查看到服务寄宿成功

  3.  客户端验证服务

  • 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端。分别添加服务地址:

    http://localhost:2234/ServiceMessage/

    http://localhost:1235/ServiceCalculator/

  

在托管 Windows 服务中承载 WCF 服务  

  1. 完成NTHost代码
  • 添加windows服务程序services1.cs,在设计界面上单击右键添加安装程序ProjectInstaller.cs,在ProjectInstaller.cs设计界面上有serviceProcessInstaller1和serviceInstaller1两个安装组件,分别设置他们的属性

添加配置文件App.config代码,代码如下:

<?xml version="1.0" encoding="utf-8"?><configuration>  <system.serviceModel>    <services>      <service name="Service.ServiceMessage" behaviorConfiguration="mexBehavior">        <host>          <baseAddresses>            <add baseAddress="http://localhost:2234/ServiceMessage/"/>          </baseAddresses>        </host>        <endpoint address="" binding="wsHttpBinding" contract="Service.IServiceMessage" />        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      </service>       <service name="Service.ServiceCalculator" behaviorConfiguration="mexBehavior">        <host>            <baseAddresses>              <add baseAddress="http://localhost:1235/ServiceCalculator/"/>              <add baseAddress="net.tcp://localhost:1234/ServiceCalculator"/>            </baseAddresses>          </host>         <endpoint address="" binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Service.IServiceCalculator" >          <identity>            <dns value="localhost"/>          </identity>        </endpoint>        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      </service>    </services>     <bindings>      <netTcpBinding>        <binding name="PortSharingBinding" portSharingEnabled="true" >          <security mode="None" />        </binding>      </netTcpBinding>    </bindings>     <behaviors>      <serviceBehaviors>        <behavior name="mexBehavior">          <serviceMetadata httpGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="false" />        </behavior>      </serviceBehaviors>    </behaviors>   </system.serviceModel></configuration>

  • Service1.cs代码如下:

using System.ServiceProcess;using Service;using System.ServiceModel;namespace NTHost{    public partial class Service1 : ServiceBase    {        public Service1()        {            InitializeComponent();        }          ServiceHost MessageHost = null;        ServiceHost CalculatorHost = null;         protected override void OnStart(string[] args)        {            MessageHost = new ServiceHost(typeof(ServiceMessage));            CalculatorHost = new ServiceHost(typeof(ServiceCalculator));             MessageHost.Open();            CalculatorHost.Open();        }         protected override void OnStop()        {            MessageHost.Close();            CalculatorHost.Close();             MessageHost = null;            CalculatorHost = null;        }    }}

  2.  寄宿服务

  • 生成NTHost工程,安装windows服务程序NTHost.exe,在命令行中输入

  Cd C:\Windows\Microsoft.NET\Framework\v4.0.30319,回车后输入installutil.exe 程序生成的bin目录绝对地址\NTHost.exe –i,回车后安装服务程序,程序注册成功后启动服务。

在开始菜单输入services.msc命令,打开服务管理程序将NTServiceHost服务设置为启动

  

  3.  客户端验证服务

  • 启动Visual Studio 命令提示(2010)命令行工具,输入wcftestclient命令调用WCF服务测试客户端。分别添加服务地址:

    http://localhost:2234/ServiceMessage/

    http://localhost:1235/ServiceCalculator/

  

时间: 2024-08-03 14:18:19

WCF服务承载 (转载)的相关文章

WCF初探-9:WCF服务承载 (下)

在WCF初探-8:WCF服务承载 (上)中,我们对宿主的概念.环境.特点做了文字性的介绍和概括,接下来我们将通过实例对这几种寄宿方式进行介绍.为了更好的说明各寄宿环境特点,本实例采用Http和net.tcp两种服务通讯方式,同时寄宿在不同的宿主中.程序结构如下: 服务契约的接口和实现代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Ser

WCF系列教程之WCF服务宿主

本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF也不例外.如果我们需要使用WCF服务,那么我们就必须将服务寄宿与创建它并控制它的上下文和生存期的运行时环境当中,承载服务的环境,称之为宿主.WCF服务可以在支持托管代码的任意Windows进程中运行.WCF提供了统一编程模型,用于生成面向服务的应用程序.此编程模型保持一致且独立于部署服务的运行时环境

使用IIS承载WCF服务

1.WCF可以方便的通过IIS承载,此承载模型与ASP.NET和ASP.NET Web Service使用的模型类似.2.WCF可以在以下操作系统上的IIS版本上承载 Windows XP SP2上的IIS 5.1 Windows Server 2003上的IIS 6.0 Windows Server 2008或者Windows Vista或者Windows 7上的IIS 7.0 以及IIS后续版本 3.在IIS 7.0中提供了一种新的承载服务方式即WAS(Windows Process Act

在控制台中承载WCF服务,并提供Ajax访问和SOAP访问,以及跨域访问

WCF服务可以承载与iis.winform.console.window服务中,下面重点介绍以console为载体,对外提供服务(服务满足web访问以及soap方式访问) 1.服务类的实现 wcf服务类一般有两种实现方式,下面分别对两种方式进行介绍: 1.1 使用接口进行实现 1 namespace Example 2 { 3 [ServiceContract] 4 public interface IService 5 { 6 7 [OperationContract] 8 string Ge

在 IIS 中承载 WCF 服务

本主题概述了创建 Internet 信息服务 (IIS) 中承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤. 本主题假设您熟悉 IIS 且了解如何使用 IIS 管理工具创建和管理 IIS 应用程序. 有关以下内容的详细信息请参阅 IIS Internet Information Services AWCF在 IIS 环境中运行的服务充分利用 IIS 功能,如进程回收. 空闲关闭. 进程状况监视和基于消息的激活. 此宿主选项要求正确配置 I

转载——Java与WCF交互(一):Java客户端调用WCF服务

最近开始了解WCF,写了个最简单的Helloworld,想通过java客户端实现通信.没想到以我的基础,居然花了整整两天(当然是工作以外的时间,呵呵),整个过程大费周折,特写下此文,以供有需要的朋友参考: 第一步:生成WCF服务 新建WCF解决方案,分别添加三个项目,HelloTimeService(类库),HelloTimehost(控制台程序),HelloTimeClient(控制台程序),项目结构如图:各个项目的主要代码:service: Host: Client: 编译通过后,测试Hos

WCF中常见的几种Host,承载WCF服务的方法

1:写在前面 我们都知道WCF在运行的时候必须自己提供宿主来承载服务.WCF 本身没有附带宿主,而是提供了一个 ServiceHost 的类,该类允许您在自己的应用程序中host WCF 服务.然后调用 ServiceHost 的 Open 方法即可.我们知道WCF是针对SOA的一套技术.对于SOA而言,我们必须确保服务能够正常运行,平稳的运行,所以此时如何host我们的服务,用什么来Host我们的服务是很重要的,所以我们要为我们的应用程序选择一个合适的Host方式是很有必要的. 2:常见的几种

Windows服务承载WCF

Source文件 ------------------------- 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.ComponentModel; 7 using System.ServiceModel; 8 using System.ServiceProcess; 9 using System.Configuration

WCF服务类的实例模式(本文为转载)

WCF开发时如何选择正确的实例模式(InstanceMode)? 在使用WCF实例模型时,你是否思考过这几个的问题: ”WCF中的实例模式如何正确应用”? ”使用WCF中的实例模式有何原则可以遵循吗”? 众所周知:客户端调用服务时,最终会将调用服务端的某个实例来完成.在WCF服务中,可以通过ServiceBehavior的InstanceContextMode设置服务实例. InstanceContextMode定义如下: // 摘要:     //     指定可用来处理包含在传入消息中的调用