Gör så här: Skapa en anpassad tillförlitlig sessionsbindning med HTTPS
Det här avsnittet visar användningen av SSL-transportsäkerhet (Secure Sockets Layer) med tillförlitliga sessioner. Om du vill använda en tillförlitlig session via HTTPS måste du skapa en anpassad bindning som använder en tillförlitlig session och HTTPS-transporten. Du aktiverar den tillförlitliga sessionen antingen imperativt med hjälp av kod eller deklarativt i konfigurationsfilen. Den här proceduren använder klient- och tjänstkonfigurationsfilerna för att aktivera den tillförlitliga sessionen och <httpsTransport-elementet.>
Den viktigaste delen av den här proceduren är att slutpunktskonfigurationselementet <>innehåller ett bindingConfiguration
attribut som refererar till en anpassad bindningskonfiguration med namnet .reliableSessionOverHttps
Bindningskonfigurationselementet <> refererar till det här namnet för att ange att en tillförlitlig session och HTTPS-transporten används genom att inkludera <reliableSession>- och< httpsTransport-element.>
Källkopian av det här exemplet finns i Custom Binding Reliable Session over HTTPS (Anpassad bindning av tillförlitlig session via HTTPS).
Konfigurera tjänsten med en CustomBinding för att använda en tillförlitlig session med HTTPS
Definiera ett tjänstkontrakt för typen av tjänst.
[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); }
Implementera tjänstkontraktet i en tjänstklass. Observera att adress- eller bindningsinformationen inte anges i implementeringen av tjänsten. Du behöver inte skriva kod för att hämta adress- eller bindningsinformationen från konfigurationsfilen.
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; } }
Skapa en Web.config-fil för att konfigurera en slutpunkt för
CalculatorService
med en anpassad bindning med namnetreliableSessionOverHttps
som använder en tillförlitlig session och HTTPS-transporten.<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <!-- Use base address provided by the host --> <endpoint address="" binding="customBinding" bindingConfiguration="reliableSessionOverHttps" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- The mex endpoint is exposed as http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <customBinding> <binding name="reliableSessionOverHttps"> <reliableSession /> <httpsTransport /> </binding> </customBinding> </bindings> </system.serviceModel> </configuration>
Skapa en Service.svc-fil som innehåller raden:
<%@ServiceHost language=c# Service="CalculatorService" %>
Placera filen Service.svc i din virtuella IIS-katalog (Internet Information Services).
Konfigurera klienten med en CustomBinding för att använda en tillförlitlig session med HTTPS
Använd verktyget ServiceModel Metadata Utility (Svcutil.exe) från kommandoraden för att generera kod från tjänstens metadata.
Svcutil.exe <Metadata Exchange (MEX) address or HTTP GET address>
Klienten som genereras innehåller det
ICalculator
gränssnitt som definierar det tjänstkontrakt som klientimplementeringen måste uppfylla.// 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); }
Det genererade klientprogrammet innehåller även implementeringen av
ClientCalculator
. Observera att adress- och bindningsinformationen inte anges i implementeringen av tjänsten. Du behöver inte skriva kod för att hämta adress- och bindningsinformationen från konfigurationsfilen.// 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); } }
Konfigurera en anpassad bindning med namnet
reliableSessionOverHttps
för att använda HTTPS-transport och tillförlitliga sessioner.<?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>
Skapa en instans av
ClientCalculator
i ett program och anropa sedan tjänståtgärderna.//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({value1},{value2}) = {result}"); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine($"Subtract({value1},{value2}) = {result}"); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine($"Multiply({value1},{value2}) = {result}"); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine($"Divide({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(); } }
Kompilera och kör klienten.
.NET Framework-säkerhet
Eftersom certifikatet som används i det här exemplet är ett testcertifikat som skapats med Makecert.exe visas en säkerhetsavisering när du försöker komma åt en HTTPS-adress, till exempel https://localhost/servicemodelsamples/service.svc
, från webbläsaren.