HOW TO:使用 HTTPS 建立自訂可靠工作階段繫結
本主題示範使用 Secure Sockets Layer (SSL) 傳輸安全性來搭配可靠工作階段。若要透過 HTTPS 使用可靠工作階段,您必須建立使用可靠工作階段與 HTTPS 傳輸的自訂繫結。您可以透過命令式程式碼或是宣告式組態檔來啟用可靠工作階段。此程序會使用用戶端與服務的組態檔來啟用可靠工作階段和 <httpsTransport> 項目。
此程序的關鍵在於 endpoint 組態項目所包含的 bindingConfiguration 屬性可參考名為 "reliableSessionOverHttps" 的自訂繫結組態。<binding> 組態項目可以接著參考此名稱,並加入 reliableSession 和 httpsTransport 項目以指定使用可靠工作階段和 HTTPS 傳輸。
如需這個範例的原始檔複本,請參閱HTTPS 上的自訂繫結可靠工作階段。
若要設定包含 CustomBinding 的服務來使用搭配 HTTPS 的可靠工作階段
定義服務類型的服務合約。
[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 檔,以便使用名為 "reliableSessionOverHttps" (使用可靠工作階段和 HTTPS 傳輸) 的自訂繫結來設定 CalculatorService 端點。
建立包含此行的 Service.svc 檔案:
<%@ServiceHost language=c# Service="CalculatorService" %>
將 Service.svc 檔放入您的網際網路資訊服務 (IIS) 虛擬目錄中。
若要設定包含 CustomBinding 的用戶端來使用搭配 HTTPS 的可靠工作階段
從命令列使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe),產生來自服務中繼資料的程式碼。
Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
所產生的用戶端會包含 ICalculator 介面,其中定義了用戶端實作所必須滿足的服務合約。
//Generated interface defining the ICalculator contract [System.ServiceModel.ServiceContractAttribute( Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")] public interface ICalculator { [System.ServiceModel.OperationContractAttribute( Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")] double Add(double n1, double n2); [System.ServiceModel.OperationContractAttribute( Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")] double Subtract(double n1, double n2); [System.ServiceModel.OperationContractAttribute( Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")] double Multiply(double n1, double n2); [System.ServiceModel.OperationContractAttribute( Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")] double Divide(double n1, double n2); }
產生的用戶端應用程式也包含 ClientCalculator 的實作。請注意,服務的實作內部並未指定位址和繫結資訊。同時,您不需要撰寫可從組態檔擷取該資訊的程式碼。
// Implementation of the CalculatorClient public partial class CalculatorClient : System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, Microsoft.ServiceModel.Samples.ICalculator { public CalculatorClient() { } public CalculatorClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public CalculatorClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public double Add(double n1, double n2) { return base.Channel.Add(n1, n2); } public double Subtract(double n1, double n2) { return base.Channel.Subtract(n1, n2); } public double Multiply(double n1, double n2) { return base.Channel.Multiply(n1, n2); } public double Divide(double n1, double n2) { return base.Channel.Divide(n1, n2); } }
將名為 "reliableSessionOverHttps" 的自訂繫結設定成使用 HTTPS 傳輸和可靠的工作階段。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <!-- this endpoint has an https: address --> <endpoint name="" address="https://localhost/servicemodelsamples/service.svc" binding="customBinding" bindingConfiguration="reliableSessionOverHttps" contract="Microsoft.ServiceModel.Samples.ICalculator" /> </client> <bindings> <customBinding> <binding name="reliableSessionOverHttps"> <reliableSession /> <httpsTransport /> </binding> </customBinding> </bindings> </system.serviceModel> </configuration>
在應用程式中建立 ClientCalculator 的執行個體,然後呼叫服務作業。
//Client implementation code. class Client { static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); } }
請編譯並執行用戶端。
範例
安全性
因為本範例中所使用的憑證是使用 Makecert.exe 所建立的測試憑證,所以當您嘗試從瀏覽器存取 HTTPS 位址時 (例如 https://localhost/servicemodelsamples/service.svc),會顯示安全性警示。