在IIS中承载WCF服务

22 篇文章 0 订阅

http://blog.csdn.net/lilypp/article/details/7652496


MSDN 原文:

承载服务:http://msdn.microsoft.com/zh-cn/library/ms730158

托管应用程序中的自承载(如上一篇WCF入门中演示的那样);托管Windows服务(用Installutil.exe 安装);IIS(本文);WAS(Windows进程激活服务)

 

如何:在IIS中承载WCF服务 http://msdn.microsoft.com/zh-cn/library/ms733766

 

本文基本上是摘抄上文“如何:在IIS中承载WCF服务”,对配置文件修改了一点,原文的会出错。

1. 创建 IISHostedCalcService 文件夹;在IIS中创建应用,指向此目录,别名为:IISHostedCalc。

2. 在IISHostedCalcService下创建 service.svc文件(WCF服务文件),内容:

[html]  view plain  copy
  1. <%@ ServiceHost language="c#" Debug="true" Service="WcfServiceLibrary8.Service1" %>

.svc文件包含WCF特定的处理指令@ServiceHost,该指令允许WCF承载基础机构激活所承载的服务,以相应传入消息。

 

3. 在 IISHostedCalcService 下创建App_Code子目录,创建Service.cs文件,内容:

[csharp]  view plain  copy
  1. using System;  
  2. using System.ServiceModel;  
  3.   
  4. namespace WcfServiceLibrary8  
  5. {  
  6.     //Define a service contract  
  7.     [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]  
  8.     public interface ICalculator  
  9.     {  
  10.         //Create the method declaration for the contract  
  11.         [OperationContract]  
  12.         double Add(double n1, double n2);  
  13.   
  14.         [OperationContract]  
  15.         double Substract(double n1, double n2);  
  16.   
  17.         [OperationContract]  
  18.         double Multiply(double n1, double n2);  
  19.   
  20.         [OperationContract]  
  21.         double Divide(double n1, double n2);  
  22.     }  
  23.   
  24.     /// <summary>  
  25.     /// Create service class that implements the service contract  
  26.     /// </summary>  
  27.     public class CalculatorService : ICalculator  
  28.     {  
  29.         #region ICalculator Members  
  30.   
  31.         // Implement functionality for the service operations.  
  32.         double ICalculator.Add(double n1, double n2)  
  33.         {  
  34.             return n1 + n2;  
  35.         }  
  36.   
  37.         double ICalculator.Substract(double n1, double n2)  
  38.         {  
  39.             return n1 - n2;  
  40.         }  
  41.   
  42.         double ICalculator.Multiply(double n1, double n2)  
  43.         {  
  44.             return n1 * n2;  
  45.         }  
  46.   
  47.         double ICalculator.Divide(double n1, double n2)  
  48.         {  
  49.             return n1 / n2;  
  50.         }  
  51.  
  52.         #endregion  
  53.     }  
  54. }  

对App_Code目录中文件进行的任何更改都会导致在收到下一个请求时回收和重新编译整个应用程序。

实现代码也可以按内联方式位于.svc文件中,@ServiceHost指令之后。


4. 在IISHostedCalcService下创建 Web.config,内容:(与MSDN原文相比,多了<behaviors>,否则在IE中会出现:Metadata publishing for this service is currently disabled.)

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service name="WcfServiceLibrary8.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">  
  6.   
  7.         <!-- This endpoint is exposed at the base address provided by host:http://localhost/servicemodelsamples/service.svc  -->  
  8.         <endpoint address=""  
  9.                   binding="wsHttpBinding"  
  10.                   contract="WcfServiceLibrary8.ICalculator" />  
  11.   
  12.         <!-- The mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex -->  
  13.         <endpoint address="mex"  
  14.                   binding="mexHttpBinding"  
  15.                   contract="IMetadataExchange" />  
  16.       </service>  
  17.     </services>  
  18.   
  19.     <behaviors>  
  20.       <serviceBehaviors>  
  21.         <behavior name="CalculatorServiceBehaviors" >  
  22.           <!-- Without this config: Metadata publishing for this service is currently disabled. -->  
  23.           <serviceMetadata httpGetEnabled="true" />  
  24.         </behavior>  
  25.       </serviceBehaviors>  
  26.     </behaviors>  
  27.   
  28.   </system.serviceModel>  
  29.   
  30. </configuration>  

 

5. 在IE中输入:http://localhost/IISHostedCalc/Service.svc 

关于如何创建访问服务的客户端,参加上一篇《WCF入门》。



============================================================================

在IIS上承载WCF方式二

在IIS目录下创建Service.svc 文件和Web.config文件即可:

Service.svc内容:

<%@ ServiceHost language="c#" Debug="true" Service="WcfServiceLibrary8.Service1" %>
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;


namespace WcfServiceLibrary8
{
    [ServiceContract]
    public interface IService1
    {
       
        [OperationContract]
        double Add(double n1, double n2);


    }


    class Service1 : IService1
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }


    }


}



Web.config内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>


  <!-- 部署服务库项目时,必须将配置文件的内容添加到
 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary8.Service1" behaviorConfiguration="serviceBehavior1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary8.IService1">
        
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
       
      </service>
     
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior1">
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


</configuration>



===============================================================================

除了使用《添加服务引用》的方式生产客户端代理,还可以使用cmd的方式:


参数:/out:表示输出到哪个路径下。/config:表示配置文件输出到哪个路径下 。后面的net.tcp://。。。。表示公布的元数据终结点的服务端地址



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值