Routing Service در WCF
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۱۱/۲۳ ۸:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
اهداف:
موارد زیر اهداف و مزایای استفاده از Routing Service است:
»Service versioning
»Content-based routing scenario
»Service partitioning
»Protocol bridging
هر کدام از موارد بالا در طی پستهای جداگانه شرح داده خواهند شد.
بررسی یک مثال:
دو Contract به صورت زیر تعریف میکنیم:
[ServiceContract]
public interface ICalculatorV1
{
[OperationContract]
int Add(int a, int b);
}
[ServiceContract]
public interface ICalculatorV2
{
[OperationContract]
int Sub(int a, int b);
} public class CalculatorV1 : ICalculatorV1
{
public int Add(int a, int b)
{
return a + b;
}
}
public class CalculatorV2 : ICalculatorV2
{
public int Sub(int a, int b)
{
return a - b;
}
} system.serviceModel>
<services>
<service name="WCFRoutingSample.CalculatorV1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/CalculatorServiceV1/" />
</baseAddresses>
</host>
<endpoint address ="" binding="basicHttpBinding" contract="WCFRoutingSample.ICalculatorV1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service name="WCFRoutingSample.CalculatorV2">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/CalculatorServiceV2/" />
</baseAddresses>
</host>
<endpoint address ="" binding="basicHttpBinding" contract="WCFRoutingSample.ICalculatorV2">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> class Program
{
static void Main(string[] args)
{
var host = new ServiceHost(typeof(RoutingService));
host.Open();
Console.WriteLine("Server is running.");
Console.ReadLine();
host.Close();
}
} <system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="routingBehv">
<routing routeOnHeadersOnly="false" filterTableName="filters"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<routing>
<filters>
<filter name="CalV1ServiceFilter" filterType="EndpointName" filterData="Calv1Service"/>
<filter name="CalV2ServiceFilter" filterType="EndpointName" filterData="Calv2Service"/>
</filters>
<filterTables>
<filterTable name="filters">
<add filterName="CalV1ServiceFilter" endpointName="Calv1Service" />
<add filterName="CalV2ServiceFilter" endpointName="Calv2Service"/>
</filterTable>
</filterTables>
</routing>
<services>
<!-- Routing service with endpoint definition -->
<service name="System.ServiceModel.Routing.RoutingService"
behaviorConfiguration="routingBehv">
<endpoint
address="/Calv1"
binding="basicHttpBinding"
contract="System.ServiceModel.Routing.IRequestReplyRouter"
name="Calv1Service"/>
<endpoint
address="/Calv2"
binding="basicHttpBinding"
contract="System.ServiceModel.Routing.IRequestReplyRouter"
name="Calv2Service"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000/CalculatorService"/>
</baseAddresses>
</host>
</service>
</services>
<client>
<endpoint address="http://localhost:8732/CalculatorServiceV1"
binding="basicHttpBinding"
contract="*"
name="Calv1Service"/>
<endpoint address="http://localhost:8733/CalculatorServiceV2"
binding="basicHttpBinding"
contract="*"
name="Calv2Service"/>
</client>
</system.serviceModel>