在IIS中承载WCF服务

2024-06-18 15:18
文章标签 服务 wcf iis 承载

本文主要是介绍在IIS中承载WCF服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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://。。。。表示公布的元数据终结点的服务端地址



这篇关于在IIS中承载WCF服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072354

相关文章

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

SpringCloud之LoadBalancer负载均衡服务调用过程

《SpringCloud之LoadBalancer负载均衡服务调用过程》:本文主要介绍SpringCloud之LoadBalancer负载均衡服务调用过程,具有很好的参考价值,希望对大家有所帮助,... 目录前言一、LoadBalancer是什么?二、使用步骤1、启动consul2、客户端加入依赖3、以服务

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

TP-Link PDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务

《TP-LinkPDDNS服将于务6月30日正式停运:用户需转向第三方DDNS服务》近期,路由器制造巨头普联(TP-Link)在用户群体中引发了一系列重要变动,上个月,公司发出了一则通知,明确要求所... 路由器厂商普联(TP-Link)上个月发布公告要求所有用户必须完成实名认证后才能继续使用普联提供的 D