• WCF 路由功能


    最近项目使用到点WCF功能,路由服务,重要还是配置文件App.config

    Routing服务代码

    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Routing;
    
    namespace RoutingServer
    {
        class RoutingServer
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(RoutingService)))
                {
                    host.Opened  = (s, e) => {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Routing...");
                    };
                    host.Open();
    
                    Console.ReadKey();
                }
            }
        }
    }

    Routing中的App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        
        <services>
          <service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="RoutingBehaviors">
            <endpoint name="DuplexSessionRouter" address="" binding="netTcpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" bindingConfiguration="abc"/>
    <!--使用的是EndpointName-->
            <endpoint name="DB" address="DB" binding="netTcpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" bindingConfiguration="abc"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://192.168.1.200:9001"/>
                <add baseAddress="net.tcp://192.168.1.200:9002/V1/Routing"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        
        <behaviors>
          <serviceBehaviors>
            <behavior name="RoutingBehaviors">
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceMetadata httpGetEnabled="true"/>
              <routing filterTableName="FilterTable1"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        
        <bindings>
          <netTcpBinding>
            <binding name="abc">
              <security mode="None"></security>
            </binding>
          </netTcpBinding>
        </bindings>
    
        <routing>
          <filterTables>
            <filterTable name="FilterTable1">
    <!--backupList :备份-->
              <add endpointName="TestEndpoint" filterName="FilterTest" backupList="backupA"/>
            </filterTable>
          </filterTables>
    
          <filters>
            <filter name="FilterTest" filterType="EndpointName" filterData="DB" />
          </filters>
          <backupLists>
            <backupList name="backupA">
              <add endpointName="BackupServerA" />
            </backupList>
          </backupLists>
        </routing>
    
        <client>
          <endpoint address="net.tcp://192.168.1.200:8002/V1" binding="netTcpBinding" contract="*" bindingConfiguration="abc" name="TestEndpoint"/>
    
          <endpoint address="net.tcp://192.168.1.200:6001/V1" binding="netTcpBinding" contract="*" bindingConfiguration="abc" name="BackupServerA"/>
        </client>
        
      </system.serviceModel>
    </configuration>

    8002端口中代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    
    namespace ServerA
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(AServer)))
                {
                    host.Opened  = (s, e) =>
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("server A...");
                    };
                    host.Open();
    
                    Console.ReadKey();
                }
            }
        }
        [ServiceContract(SessionMode= SessionMode.Required)]
        public interface IAServer
        {
            [OperationContract]
            string Send();
        }
    
        public class AServer : IAServer
        {
            public string Send()
            {
                return "Hello world";
            }
        }
    }
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="ServerA.AServer" behaviorConfiguration="AServerBehavior">
            <endpoint address="" binding="netTcpBinding" contract="ServerA.IAServer" bindingConfiguration="TcpBinding"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://192.168.1.200:8000"/>
                <add baseAddress="net.tcp://192.168.1.200:8002/V1"/>
              </baseAddresses>
            </host>
          </service>
        </services>
    
        <bindings>
          <netTcpBinding>
            <binding name="TcpBinding">
              <security mode="None"></security>
            </binding>
          </netTcpBinding>
        </bindings>
    
        <behaviors>
          <serviceBehaviors >
            <behavior name="AServerBehavior">
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        
      </system.serviceModel>
    </configuration>

    backupList中代码跟这8002端口代码一样。

    使用Svcutil.exe生成客户端代码

    svcutil.exe /out:c:\Routing.cs /config:C:\app.config http://192.168.1.200:9001

    svcutil.exe /out:C:\Test.cs /config:c:\app2.config http://192.168.1.200:8000

    然后复制app.config跟Test.cs到客户端调用工程里。

    客户端演示

    一定要修改App.config里的

    <endpoint address="net.tcp://192.168.1.200:9002/V1/Routing/DB" binding="netTcpBinding"
                   bindingConfiguration="DuplexSessionRouter" contract="IAServer"
                   name="DB" />

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <netTcpBinding>
                    <binding name="DuplexSessionRouter" closeTimeout="00:01:00" openTimeout="00:01:00"
                        receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                        transferMode="Buffered" transactionProtocol="OleTransactions"
                        hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                        maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                        maxReceivedMessageSize="65536">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <reliableSession ordered="true" inactivityTimeout="00:10:00"
                            enabled="false" />
                        <security mode="None">
                            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                            <message clientCredentialType="Windows" />
                        </security>
                    </binding>
                </netTcpBinding>
            </bindings>
            <client>
                <endpoint address="net.tcp://192.168.1.200:9002/V1/Routing" binding="netTcpBinding"
                    bindingConfiguration="DuplexSessionRouter" contract="IRequestReplyRouter"
                    name="DuplexSessionRouter" />
                <endpoint address="net.tcp://192.168.1.200:9002/V1/Routing/DB" binding="netTcpBinding"
                    bindingConfiguration="DuplexSessionRouter" contract="IAServer"
                    name="DB" />
            </client>
        </system.serviceModel>
    </configuration>
  • 相关阅读:
    malloc 函数
    如何在Ubuntu 20.04上启用SSH
    E :仓库***没有Release文件
    Invocation of init method failed; nested exception is org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl
    发送方IP地址与路由器
    计算机系统到底是个什么东东
    批量创建10个系统帐号,并设置密码(密码为随机数,要求字符和数字等混合(第一种)
    mysql主从复制
    mysql主从复制和读写分离
    系统命令大全
  • 原文地址:https://www.cnblogs.com/server126/p/2603767.html
Copyright © 2020-2023  润新知