gebeurtenis
17 mrt, 21 - 21 mrt, 10
Neem deel aan de meetup-serie om schaalbare AI-oplossingen te bouwen op basis van praktijkgebruiksvoorbeelden met collega-ontwikkelaars en experts.
Nu registrerenDeze browser wordt niet meer ondersteund.
Upgrade naar Microsoft Edge om te profiteren van de nieuwste functies, beveiligingsupdates en technische ondersteuning.
In dit onderwerp vindt u een overzicht van de basisstappen die nodig zijn voor het maken van een WCF-service (Windows Communication Foundation) die wordt gehost in Internet Information Services (IIS). In dit onderwerp wordt ervan uitgegaan dat u bekend bent met IIS en begrijpt hoe u het IIS-beheerprogramma gebruikt om IIS-toepassingen te maken en te beheren. Zie Internet Information Services voor meer informatie over IIS. Een WCF-service die wordt uitgevoerd in de IIS-omgeving, maakt optimaal gebruik van IIS-functies, zoals procesrecycling, niet-actieve afsluiting, processtatuscontrole en activering op basis van berichten. Deze hostingoptie vereist dat IIS correct is geconfigureerd, maar vereist niet dat er hostingcode wordt geschreven als onderdeel van de toepassing. U kunt IIS-hosting alleen gebruiken met een HTTP-transport.
Zie WCF-services en ASP.NET voor meer informatie over de interactie tussen WCF en ASP.NET. Zie Beveiliging voor meer informatie over het configureren van beveiliging.
Zie IIS-hosting met behulp van inlinecode voor de bronkopie van dit voorbeeld.
Controleer of IIS is geïnstalleerd en wordt uitgevoerd op uw computer. Zie IIS 7.0 installeren en configurerenvoor meer informatie over het installeren en configureren van IIS.
Maak een nieuwe map voor uw toepassingsbestanden met de naam IISHostedCalcService, zorg ervoor dat ASP.NET toegang heeft tot de inhoud van de map en gebruik het HULPPROGRAMMA IIS-beheer om een nieuwe IIS-toepassing te maken die zich fysiek in deze toepassingsmap bevindt. Wanneer u een alias voor de toepassingsmap maakt, gebruikt u IISHostedCalc.
Maak een nieuw bestand met de naam service.svc in de toepassingsmap. Bewerk dit bestand door het volgende @ServiceHost element toe te voegen.
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>
Maak een App_Code submap in de toepassingsmap.
Maak een codebestand met de naam Service.cs in de submap App_Code.
Voeg de volgende using
instructies toe aan het begin van het Service.cs-bestand.
using System;
using System.ServiceModel;
Voeg de volgende naamruimtedeclaratie toe na de using
instructies.
namespace Microsoft.ServiceModel.Samples
{
}
Definieer het servicecontract in de naamruimtedeclaratie, zoals wordt weergegeven in de volgende code.
[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);
}
<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
Implementeer het servicecontract na de definitie van het servicecontract, zoals wordt weergegeven in de volgende code.
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;
}
}
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
Maak een bestand met de naam Web.config in de toepassingsmap en voeg de volgende configuratiecode toe aan het bestand. Tijdens runtime gebruikt de WCF-infrastructuur de informatie om een eindpunt te maken waarmee clienttoepassingen kunnen communiceren.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">
<!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="wsHttpBinding"
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>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehaviors">
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
In dit voorbeeld worden expliciet eindpunten in het configuratiebestand opgegeven. Als u geen eindpunten aan de service toevoegt, voegt de runtime standaardeindpunten voor u toe. Zie Vereenvoudigde configuratie en vereenvoudigde configuratie voor WCF-services voor meer informatie over standaardeindpunten, bindingen en gedrag.
Als u wilt controleren of de service correct wordt gehost, opent u een browser en bladert u naar de URL van de service: http://localhost/IISHostedCalc/Service.svc
Hier volgt een volledige lijst van de code voor de door IIS gehoste rekenmachineservice.
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;
}
}
}
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
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">
<!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="wsHttpBinding"
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>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehaviors">
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
gebeurtenis
17 mrt, 21 - 21 mrt, 10
Neem deel aan de meetup-serie om schaalbare AI-oplossingen te bouwen op basis van praktijkgebruiksvoorbeelden met collega-ontwikkelaars en experts.
Nu registrerenTraining
Module
Een webtoepassing hosten met Azure App Service - Training
Maak een website met C#, Java, Python of Node.js en implementeer deze via het gehoste web-app-platform in Azure-app Service.