如何:在 IIS 中承载 WCF 服务
本主题概述了创建 Internet 信息服务 (IIS) 中承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤。本主题假设您熟悉 IIS 且了解如何使用 IIS 管理工具创建和管理 IIS 应用程序。有关 IIS 的更多信息,请参见 Internet 信息服务(可能为英文网页)。在 IIS 环境中运行的 WCF 服务充分利用了 IIS 功能,例如进程回收、空闲关闭、进程运行状况监视和基于消息的激活。此宿主选项要求正确配置 IIS,但不需要编写任何承载代码作为应用程序的一部分。只可以将 IIS 宿主与 HTTP 传输协议一起使用。
有关 WCF 和 ASP.NET 如何交互的更多信息,请参见 WCF 服务和 ASP.NET。有关配置安全性的更多信息,请参见 Windows Communication Foundation 安全性。
有关此示例的源副本,请参见使用内联代码的 IIS 承载。
创建由 IIS 承载的服务
确认 IIS 已安装并运行在您的计算机中。有关安装和配置 IIS 的更多信息,请参见安装和配置 IIS 7.0(可能为英文网页)
为应用程序文件创建一个称为“IISHostedCalcService”的新文件夹,确保 ASP.NET 有权访问该文件夹的内容,并使用 IIS 管理工具创建一个物理上位于此应用程序目录中的新 IIS 应用程序。当为应用程序目录创建别名时,请使用“IISHostedCalc”。
在应用程序目录中创建一个名为“service.svc”的新文件。通过添加以下 @ServiceHost 元素编辑此文件。
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>
在应用程序目录中创建 App_Code 子目录。
在 App_Code 子目录中创建名为 Service.cs 的代码文件。
将下面的 using 语句添加到 Service.cs 文件的最前面。
using System; using System.ServiceModel;
将下面的命名空间声明添加到 using 语句后面。
namespace Microsoft.ServiceModel.Samples { }
定义命名空间声明中的服务协定,如下面的代码所示。
<ServiceContract()> _ Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double End Interface
[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 Implements ICalculator Public Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Add Return n1 + n2 End Function Public Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Subtract Return n1 - n2 End Function Public Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Multiply Return n1 * n2 End Function Public Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements ICalculator.Divide Return n1 / n2 End Function End Class
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; } }
在应用程序目录中创建一个名为“Web.config”的文件,并将下面的配置代码添加到该文件中。在运行时,WCF 基础结构使用这些信息来构造客户端应用程序可与其通信的终结点。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <!-- This section is optional with the default configuration model introduced in .NET Framework 4 --> <service name="Microsoft.ServiceModel.Samples.CalculatorService"> <!-- This endpoint is exposed at the base address provided by host: https://localhost/servicemodelsamples/service.svc --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- The mex endpoint is exposed at https://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> </configuration>
此示例显式指定配置文件中的终结点。如果您未向该服务中添加任何终结点,运行时将为您添加默认终结点。有关默认终结点、绑定和行为的更多信息,请参见简化配置和WCF 服务的简化配置。
为了确保正确承载该服务,请打开 Internet Explorer 的实例,导航到该服务的 URL:
https://localhost/IISHostedCalc/Service.svc
示例
下面是 IIS 承载的计算器服务的代码的完整列表。
Imports System
Imports System.ServiceModel
Namespace Microsoft.ServiceModel.Samples
<ServiceContract()> _
Public Interface ICalculator
<OperationContract()> _
Function Add(ByVal n1 As Double, _
ByVal n2 As Double) As Double
<OperationContract()> _
Function Subtract(ByVal n1 As Double, _
ByVal n2 As Double) As Double
<OperationContract()> _
Function Multiply(ByVal n1 As Double, _
ByVal n2 As Double) As Double
<OperationContract()> _
Function Divide(ByVal n1 As Double, _
ByVal n2 As Double) As Double
End Interface
Public Class CalculatorService
Implements ICalculator
Public Function Add(ByVal n1 As Double, _
ByVal n2 As Double) As Double Implements ICalculator.Add
Return n1 + n2
End Function
Public Function Subtract(ByVal n1 As Double, _
ByVal n2 As Double) As Double Implements ICalculator.Subtract
Return n1 - n2
End Function
Public Function Multiply(ByVal n1 As Double, _
ByVal n2 As Double) As Double Implements ICalculator.Multiply
Return n1 * n2
End Function
Public Function Divide(ByVal n1 As Double, _
ByVal n2 As Double) As Double Implements ICalculator.Divide
Return n1 / n2
End Function
End Class
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;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService">
<!-- This endpoint is exposed at the base address provided by host: https://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<!-- The mex endpoint is explosed at https://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
另请参见
概念
在 Internet 信息服务中承载
承载服务
WCF 服务和 ASP.NET
其他资源
Windows Communication Foundation 安全性
Windows Server App Fabric 承载功能