有关如下内容的详细信息如何WCF和ASP.NET进行交互,请参阅WCF 服务和 ASP.NET。 有关如下内容的详细信息配置安全性,请参阅安全。安全
此示例的源副本,请参阅IIS 承载使用内联代码。session
建立由 IIS 承载的服务
-
确认 IIS 已经安装并在计算机上运行。 有关如下内容的详细信息安装和配置 IIS,请参阅安装和配置 IIS 7.0ide
-
为应用程序文件建立一个称为“IISHostedCalcService”的新文件夹,确保 ASP.NET 有权访问该文件夹的内容,并使用 IIS 管理工具建立一个物理上位于此应用程序目录中的新 IIS 应用程序。 当为应用程序目录建立别名时,请使用“IISHostedCalc”。工具
-
在应用程序目录中建立一个名为“service.svc”的新文件。 编辑此文件添加如下代码@ServiceHost元素。ui
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>
-
在应用程序目录中建立 App_Code 子目录。spa
-
在 App_Code 子目录中建立名为 Service.cs 的代码文件。debug
-
将下面的 using 语句添加到 Service.cs 文件的最前面。code
using System; using System.ServiceModel;
-
将下面的命名空间声明添加到 using 语句后面。orm
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; } }
-
在应用程序目录中建立一个名为“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 服务的简化配置。
-
为了确保正确承载该服务,请打开 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; } }