• 在 IIS 中承载 WCF 服务


    本主题概述了创建 Internet 信息服务 (IIS) 中承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤。 本主题假设您熟悉 IIS 且了解如何使用 IIS 管理工具创建和管理 IIS 应用程序。 有关以下内容的详细信息请参阅 IIS Internet Information Services AWCF在 IIS 环境中运行的服务充分利用 IIS 功能,如进程回收、 空闲关闭、 进程状况监视和基于消息的激活。 此宿主选项要求正确配置 IIS,但不需要编写任何承载代码作为应用程序的一部分。 只可以将 IIS 宿主与 HTTP 传输协议一起使用。

    有关以下内容的详细信息如何WCF和ASP.NET进行交互,请参阅WCF 服务和 ASP.NET。 有关以下内容的详细信息配置安全性,请参阅安全

    此示例的源副本,请参阅IIS 承载使用内联代码

    创建由 IIS 承载的服务

    1. 确认 IIS 已经安装并在计算机上运行。 有关以下内容的详细信息安装和配置 IIS,请参阅安装和配置 IIS 7.0

    2. 为应用程序文件创建一个称为“IISHostedCalcService”的新文件夹,确保 ASP.NET 有权访问该文件夹的内容,并使用 IIS 管理工具创建一个物理上位于此应用程序目录中的新 IIS 应用程序。 当为应用程序目录创建别名时,请使用“IISHostedCalc”。

    3. 在应用程序目录中创建一个名为“service.svc”的新文件。 编辑此文件添加以下代码@ServiceHost元素。

      <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>  
      
    4. 在应用程序目录中创建 App_Code 子目录。

    5. 在 App_Code 子目录中创建名为 Service.cs 的代码文件。

    6. 将下面的 using 语句添加到 Service.cs 文件的最前面。

      using System;  
      using System.ServiceModel;  
      
    7. 将下面的命名空间声明添加到 using 语句后面。

      namespace Microsoft.ServiceModel.Samples  
      {  
      }  
      
    8. 定义命名空间声明中的服务协定,如下面的代码所示。

       
        [ServiceContract]
        public interface ICalculator
        {
           [OperationContract]
           double Add(double n1, double n2);
           [OperationContract]
           double Subtract(double n1, double n2);
           [OperationContract]
           double Multiply(double n1, double n2);
           [OperationContract]
           double Divide(double n1, double n2);
        }
      
    9. 在服务协定定义后实现服务协定,如下面的代码所示。

       
        public class CalculatorService : ICalculator
        {
           public double Add(double n1, double n2)
           {
              return n1 + n2;
           }
           public double Subtract(double n1, double n2)
           {
              return n1 - n2;
           }
           public double Multiply(double n1, double n2)
           {
              return n1 * n2;
           }
           public double Divide(double n1, double n2)
           {
              return n1 / n2;
           }
        } 
      
    10. 在应用程序目录中创建一个名为“Web.config”的文件,并将下面的配置代码添加到该文件中。 在运行时,WCF 基础结构使用这些信息来构造客户端应用程序可与其通信的终结点。

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <system.serviceModel>
          <services>
            <service 
                name="Microsoft.ServiceModel.Samples.CalculatorService"
                behaviorConfiguration="CalculatorServiceBehavior">
              <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc  -->
              <!-- specify customBinding binding and a binding configuration to use -->
              <endpoint address=""
                        binding="customBinding"
                        bindingConfiguration="Binding1" 
                        contract="Microsoft.ServiceModel.Samples.ICalculator" />
              <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
              <endpoint address="mex"
                        binding="mexHttpBinding"
                        contract="IMetadataExchange" />
            </service>
          </services>
      
          <!-- custom binding configuration - configures HTTP transport, reliable sessions -->
          <bindings>
            <customBinding>
              <binding name="Binding1">
                <reliableSession />
                <security authenticationMode="SecureConversation"
                           requireSecurityContextCancellation="true">
                </security>
                <compositeDuplex />
                <oneWay />
                <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8" />
                <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                              hostNameComparisonMode="StrongWildcard" 
                              proxyAuthenticationScheme="Anonymous" realm="" 
                              useDefaultWebProxy="true" />
              </binding>
            </customBinding>
          </bindings>
      
          <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
          <behaviors>
            <serviceBehaviors>
              <behavior name="CalculatorServiceBehavior">
                <serviceMetadata httpGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="False" />
              </behavior>
            </serviceBehaviors>
          </behaviors>
        </system.serviceModel>
      </configuration>

      此示例显式指定配置文件中的终结点。 如果您不希望向服务添加任何终结点,则运行时为您添加默认终结点。 有关以下内容的详细信息默认终结点、 绑定和行为,请参阅简化配置WCF 服务的简化配置

    11. 为了确保正确承载该服务,请打开 Internet Explorer 的实例,导航到该服务的 URL:http://localhost/IISHostedCalc/Service.svc,如果一切正常会如下图:

    示例

    下面是 IIS 承载的计算器服务的代码的完整列表。

    using System;
    using System.ServiceModel;
    
    namespace Microsoft.ServiceModel.Samples
    {
    
      [ServiceContract]
      public interface ICalculator
      {
         [OperationContract]
         double Add(double n1, double n2);
         [OperationContract]
         double Subtract(double n1, double n2);
         [OperationContract]
         double Multiply(double n1, double n2);
         [OperationContract]
         double Divide(double n1, double n2);
      }
    
    
      public class CalculatorService : ICalculator
      {
         public double Add(double n1, double n2)
         {
            return n1 + n2;
         }
         public double Subtract(double n1, double n2)
         {
            return n1 - n2;
         }
         public double Multiply(double n1, double n2)
         {
            return n1 * n2;
         }
         public double Divide(double n1, double n2)
         {
            return n1 / n2;
         }
      } 
    
  • 相关阅读:
    iOS Core Animation 简明系列教程
    常用的iOS第三方资源
    超全!整理常用的iOS第三方资源
    iOS使用Workspace来管理多项目 ( 转 )
    转 与App Store审核的斗智斗勇
    Python -- Web -- WSGI
    Python -- Web
    Python -- 序列化
    Python -- 文档测试
    Python -- 单元测试
  • 原文地址:https://www.cnblogs.com/go-jzg/p/6442078.html
Copyright © 2020-2023  润新知