有關如下內容的詳細信息如何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; } }