如何:启用 WCF 配置文件服务

更新:2007 年 11 月

本主题演示如何在 Web 服务器上配置 ASP.NET 配置文件服务,使其可供使用 Windows Communication Foundation (WCF) 的客户端使用。本主题还演示如何执行以下任务:

  • 定义配置文件属性。

  • 使得可通过配置文件服务来使用配置文件属性。

    Bb514968.alert_note(zh-cn,VS.90).gif说明:

    如果将敏感数据存储在配置文件属性中,则通过安全套接字层(SSL,通过使用 HTTPS 协议)访问配置文件服务。有关如何设置 SSL 的更多信息,请参见位于 Microsoft 网站上的 Configuring Secure Sockets Layer (IIS 6.0 Operations Guide)(配置安全套接字层(IIS 6.0 操作指南))和 Configuring Secure Sockets Layer in IIS 7.0(在 IIS 7.0 中配置安全套接字层)。

将 ASP.NET 配置文件服务配置为 WCF 服务

  1. 如果还没有 ASP.NET Web 应用程序,请创建一个。

  2. 向网站中添加一个 WCF 服务文件 (.svc) 文件,该文件中包含下列指令以引用 ProfileService 类:

     [Visual Basic]
    <%@ ServiceHost Language="VB"
    Service="System.Web.ApplicationServices.ProfileService" 
    Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    <%@ ServiceHost Language="C#"
    Service="System.Web.ApplicationServices.ProfileService" 
    Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    
  3. 保存 .svc 文件,并将其关闭。

  4. 在 Web.config 文件中进行以下的配置设置以配置服务:

    • 在 services 元素中定义终结点协定,在 behaviors 元素中定义服务行为。如下面的示例所示,在终结点协定中包括 bindingNamespace 属性,以防止在一些代理生成工具中出现异常。有关 WCF 终结点的更多信息,请参见 Windows Communication Foundation Endpoints(Windows Communication Foundation 终结点)。

    • 配置 serviceHostingEnvironment 元素以获得 ASP.NET 兼容性。有关承载 WCF 服务的更多信息,请参见 WCF Services and ASP.NET(WCF 服务和 ASP.NET)。

    下面的示例演示 Web.config 文件中的 system.serviceModel 元素,该文件显示了上一个列表中描述的配置设置。

    <system.serviceModel>
      <services>
        <service name="System.Web.ApplicationServices.ProfileService"
          behaviorConfiguration="MyServiceTypeBehaviors">
          <endpoint contract=
            "System.Web.ApplicationServices.ProfileService"
            binding="basicHttpBinding" 
            bindingNamespace="https://asp.net/ApplicationServices/v200"/>
        </service>
      </services>
      <serviceHostingEnvironment
        aspNetCompatibilityEnabled="true"/>
      <bindings>
        <basicHttpBinding allowCookies="true">
      </bindings> 
    </system.serviceModel>
    

启用配置文件服务并公开属性

  1. 如果您尚未定义 Web 应用程序的配置文件属性,请在 Web.config 文件中定义它们。

    下面的示例演示两种配置文件属性的定义。

    <system.web>
      <profile> 
        <properties>
          <add name="Birthday" /> 
          <add name="FavoriteColor" />
        </properties> 
      </profile>
    </system.web>
    

    有关定义配置文件属性的更多信息,请参见定义 ASP.NET 配置文件属性

  2. 在 Web.config 文件中,启用配置文件服务并标记可作为只读属性或读写属性使用的属性。

    下面的示例演示如何启用配置文件服务和设置配置文件属性的可访问性。

    <system.web.extensions>
      <scripting>
        <webServices>
          <profileService enabled="true"
            readAccessProperties="Birthday, FavoriteColor"
            writeAccessProperties="Birthday, FavoriteColor" >
        </webServices>
      </scripting>
    </system.web.extensions>
    

安全性

如果将敏感数据保存在配置文件属性中,请通过安全套接字层(SSL,通过使用 HTTPS 协议)访问配置文件服务。还请确保安全存储数据。有关更多信息,请参见保证配置文件属性的安全。有关如何设置 WCF 服务以使其通过 SSL 运行的更多信息,请参见 Transport Security(传输安全)。有关如何设置 SSL 的更多信息,请参见位于 Microsoft 网站上的 Configuring Secure Sockets Layer (IIS 6.0 Operations Guide)(配置安全套接字层(IIS 6.0 操作指南))和 Configuring Secure Sockets Layer in IIS 7.0(在 IIS 7.0 中配置安全套接字层)。

请参见

任务

演练:使用 ASP.NET 应用程序服务

概念

Windows Communication Foundation 身份验证服务概述

Windows Communication Foundation 角色服务概述

其他资源

Configuring Services(配置服务)