Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Det här avsnittet beskriver de grundläggande steg som krävs för att skapa och dynamiskt uppdatera routningskonfigurationen. I det här exemplet hämtas den inledande routningskonfigurationen från konfigurationsfilen och dirigerar alla meddelanden till regularCalc-kalkylatortjänsten. Den uppdateras dock programmatiskt för att ändra målslutpunkten för avrundningstjänstenCalc.
Kommentar
I många implementeringar är konfigurationen helt dynamisk och förlitar sig inte på någon standardkonfiguration. Det finns dock vissa scenarier, till exempel det i det här avsnittet, där det är önskvärt att ha ett standardkonfigurationstillstånd när tjänsten startar.
Kommentar
Dynamiska uppdateringar sker endast i minnet och resulterar inte i att konfigurationsfilerna ändras.
Både regularCalc och roundingCalc stöder samma åtgärder för att lägga till, subtrahera, multiplicera och dividera. RoundingCalc avrundar dock alla beräkningar till närmaste heltalsvärde innan de returneras. En konfigurationsfil används för att konfigurera tjänsten för att dirigera alla meddelanden till regularCalc-tjänsten. När routningstjänsten har startats ApplyConfiguration används den för att konfigurera om tjänsten för att dirigera meddelanden till avrundningstjänstenCalc.
Implementera inledande konfiguration
Skapa den grundläggande konfigurationen för routningstjänsten genom att ange tjänstslutpunkterna som exponeras av tjänsten. I följande exempel definieras en enda tjänstslutpunkt som ska användas för att ta emot meddelanden. Den definierar också en klientslutpunkt som ska användas för att skicka meddelanden till regularCalc.
<services> <service behaviorConfiguration="routingConfiguration" name="System.ServiceModel.Routing.RoutingService"> <host> <baseAddresses> <add baseAddress="http://localhost/routingservice/router" /> </baseAddresses> </host> <!--Set up the inbound endpoint for the Routing Service--> <endpoint address="calculator" binding="wsHttpBinding" name="routerEndpoint" contract="System.ServiceModel.Routing.IRequestReplyRouter" /> </service> </services> <client> <!--set up the destination endpoint--> <endpoint name="regularCalcEndpoint" address="net.tcp://localhost:9090/servicemodelsamples/service/" binding="netTcpBinding" contract="*" /> </client>Definiera det filter som används för att dirigera meddelanden till målslutpunkterna. I det här exemplet används filtret MatchAll för att dirigera alla meddelanden till den regularCalcEndpoint som definierades tidigare. I följande exempel definieras filter- och filtertabellen.
<filters> <!--define the message filter--> <filter name="MatchAllFilter" filterType="MatchAll"/> </filters> <filterTables> <filterTable name="filterTable1"> <!--add the filter to the message filter table--> <add filterName="MatchAllFilter" endpointName="regularCalcEndpoint"/> </filterTable> </filterTables>Om du vill utvärdera inkommande meddelanden mot filtren i filtertabellen måste du associera filtertabellen med tjänstslutpunkterna med hjälp av routningsbeteendet. I följande exempel visas hur du associerar "filterTable1" med tjänstslutpunkten.
<behaviors> <!--default routing service behavior definition--> <serviceBehaviors> <behavior name="routingConfiguration"> <routing filterTableName="filterTable1" /> </behavior> </serviceBehaviors> </behaviors>
Implementera dynamisk konfiguration
Dynamisk konfiguration av routningstjänsten kan bara utföras i kod genom att skapa en ny RoutingConfiguration och använda ApplyConfiguration för att ersätta den aktuella konfigurationen. I det här exemplet är routningstjänsten lokalt installerad i ett konsolprogram. När programmet har startat kan du ändra routningskonfigurationen genom att ange "vanlig" eller "avrundning" i konsolfönstret för att konfigurera målslutpunkten som meddelanden dirigeras till. regularCalc när "vanlig" anges, annars avrundningCalc när "avrundning" anges.
Följande
usingdirektiv måste läggas till för att stödja routningstjänsten.using System; using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; using System.ServiceModel.Routing;Följande kod används för att själv vara värd för routningstjänsten som ett konsolprogram. Detta initierar routningstjänsten med hjälp av konfigurationen som beskrivs i föregående steg, som finns i programkonfigurationsfilen. While-loopen innehåller koden som används för att ändra routningskonfigurationen.
// Host the service within this EXE console application. public static void Main() { // Create a ServiceHost for the CalculatorService type. using (ServiceHost serviceHost = new ServiceHost(typeof(RoutingService))) { // Open the ServiceHost to create listeners // and start listening for messages. Console.WriteLine("The Routing Service configured, opening...."); serviceHost.Open(); Console.WriteLine("The Routing Service is now running."); Console.WriteLine("Type 'quit' to terminate router."); Console.WriteLine("<ENTER> to change routing configuration."); while (Console.ReadLine() != "quit") { .... } } }Om du vill uppdatera routningskonfigurationen dynamiskt måste en ny routningskonfiguration skapas. Detta måste innehålla alla slutpunkter, filter och filtertabeller som krävs för den nya routningskonfigurationen, eftersom den helt ersätter den befintliga routningskonfigurationen. För att kunna använda den nya routningskonfigurationen måste du anropa ApplyConfiguration och skicka den nya konfigurationen.
Lägg till följande kod i while-loopen som definierats tidigare så att tjänsten kan konfigureras om baserat på användarindata.
Console.WriteLine("Enter 'regular' or 'rounding' to set the destination endpoint:"); string destEndpoint = Console.ReadLine(); // Create a new RoutingConfiguration RoutingConfiguration rc = new RoutingConfiguration(); // Determine the endpoint to configure for switch (destEndpoint) { case "regular": // Define the destination endpoint ServiceEndpoint regularCalc = new ServiceEndpoint( ContractDescription.GetContract(typeof(IRequestReplyRouter)), new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9090/servicemodelsamples/service/")); // Create a MatchAll filter and add to the filter table rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { regularCalc }); // Use ApplyConfiguration to update the Routing Service serviceHost.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc); Console.WriteLine("Applied new routing configuration."); break; case "rounding": // Define the destination endpoint ServiceEndpoint roundingCalc = new ServiceEndpoint( ContractDescription.GetContract(typeof(IRequestReplyRouter)), new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8080/servicemodelsamples/service/")); // Create a MatchAll filter and add to the filter table rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { roundingCalc }); // Use ApplyConfiguration to update the Routing Service serviceHost.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc); Console.WriteLine("Applied new routing configuration."); break; default: Console.WriteLine("Incorrect value entered, no change."); break; }Kommentar
Eftersom metoden för att tillhandahålla en ny RoutingConfiguration finns i RoutingExtension-tjänsttillägget kan nya RoutingConfiguration-objekt tillhandahållas var som helst i WCF-utökningsmodellen som har eller kan hämta en referens till ServiceHost eller ServiceExtensions (till exempel i en annan ServiceExtension).
Exempel 1
Följande är en fullständig lista över konsolprogrammet som används i det här exemplet:
//-----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Routing;
namespace Microsoft.Samples.AdvancedFilters
{
public class Router
{
// Host the service within this EXE console application.
public static void Main()
{
// Create a ServiceHost for the CalculatorService type.
using (ServiceHost serviceHost =
new ServiceHost(typeof(RoutingService)))
{
// Open the ServiceHost to create listeners
// and start listening for messages.
Console.WriteLine("The Routing Service configured, opening....");
serviceHost.Open();
Console.WriteLine("The Routing Service is now running.");
Console.WriteLine("Type 'quit' to terminate router.");
Console.WriteLine("<ENTER> to change routing configuration.");
while (Console.ReadLine() != "quit")
{
Console.WriteLine("Enter 'regular' or 'rounding' to set the destination endpoint:");
string destEndpoint = Console.ReadLine();
// Create a new RoutingConfiguration
RoutingConfiguration rc = new RoutingConfiguration();
// Determine the endpoint to configure for
switch (destEndpoint)
{
case "regular":
// Define the destination endpoint
ServiceEndpoint regularCalc = new ServiceEndpoint(
ContractDescription.GetContract(typeof(IRequestReplyRouter)),
new NetTcpBinding(),
new EndpointAddress("net.tcp://localhost:9090/servicemodelsamples/service/"));
// Create a MatchAll filter and add to the filter table
rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { regularCalc });
// Use ApplyConfiguration to update the Routing Service
serviceHost.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc);
Console.WriteLine("Applied new routing configuration.");
break;
case "rounding":
// Define the destination endpoint
ServiceEndpoint roundingCalc = new ServiceEndpoint(
ContractDescription.GetContract(typeof(IRequestReplyRouter)),
new NetTcpBinding(),
new EndpointAddress("net.tcp://localhost:8080/servicemodelsamples/service/"));
// Create a MatchAll filter and add to the filter table
rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { roundingCalc });
// Use ApplyConfiguration to update the Routing Service
serviceHost.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc);
Console.WriteLine("Applied new routing configuration.");
break;
default:
Console.WriteLine("Incorrect value entered, no change.");
break;
}
}
}
}
}
}
Exempel 2
Följande är en fullständig lista över konfigurationsfilen som används i det här exemplet:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright (c) Microsoft Corporation. All rights reserved -->
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="routingConfiguration"
name="System.ServiceModel.Routing.RoutingService">
<host>
<baseAddresses>
<add baseAddress="http://localhost/routingservice/router" />
</baseAddresses>
</host>
<!--Set up the inbound endpoint for the Routing Service-->
<endpoint address="calculator"
binding="wsHttpBinding"
name="routerEndpoint"
contract="System.ServiceModel.Routing.IRequestReplyRouter" />
</service>
</services>
<behaviors>
<!--default routing service behavior definition-->
<serviceBehaviors>
<behavior name="routingConfiguration">
<routing filterTableName="filterTable1" />
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<!--set up the destination endpoint-->
<endpoint name="regularCalcEndpoint"
address="net.tcp://localhost:9090/servicemodelsamples/service/"
binding="netTcpBinding"
contract="*" />
</client>
<routing>
<filters>
<!--define the message filter-->
<filter name="MatchAllFilter" filterType="MatchAll"/>
</filters>
<filterTables>
<filterTable name="filterTable1">
<!--add the filter to the message filter table-->
<add filterName="MatchAllFilter" endpointName="regularCalcEndpoint"/>
</filterTable>
</filterTables>
</routing>
</system.serviceModel>
</configuration>