zoukankan      html  css  js  c++  java
  • WCF技术解剖2-TcpTracer路由解析代码

    TcpTrace路由解析,参考页面-http://www.cnblogs.com/artech/archive/2008/09/19/1294227.html

    TcpTrace工具下载地址:http://www.piaodown.com/soft/43538.htm

    原理:模拟路由的方式进行客户端的消息转发,回复。如图:

    代码结构如下:

    分别为ICalculator:

    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    
    namespace Artech.TcpTraceDemo.Contracts
    {
        [ServiceContract(Namespace = "http://www.artech.com/")]
        public interface ICalculator
        {
            [OperationContract]
            double Add(double x, double y);
        }
    }
    View Code

    Service:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Artech.TcpTraceDemo.Contracts;
    
    namespace Artech.TcpTraceDemo.Services
    {
        public class CalculatorService:ICalculator
        {
            
            public double Add(double x, double y)
            {
                return x + y;
            }
        }
    }
    View Code

    Hosting

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using Artech.TcpTraceDemo.Services;
    
    namespace Artech.TcpTraceDemo.Hosting
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
                {
                    serviceHost.Open();
                    Console.Read();
                }
            }
        }
    }
    View Code

    Hosting,Appconfig

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
      
        <bindings>
          <customBinding>
            <binding name="SimpleBinding">
              <textMessageEncoding />
              <httpTransport />
            </binding>
          </customBinding>
        </bindings>
        <services>
          <service name="Artech.TcpTraceDemo.Services.CalculatorService">
            <endpoint address="http://127.0.0.1:9999/CalculatorService" binding="customBinding"
                      bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculator" listenUri="http://127.0.0.1:8888/CalculatorService"></endpoint>
          </service>
          
        </services>
        
      </system.serviceModel>
    </configuration>
    View Code

    Clients:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Artech.TcpTraceDemo.Contracts;
    using System.ServiceModel;
    
    namespace Artech.TcpTraceDemo.Clients
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ChannelFactory<ICalculator> ChannelFactory = new ChannelFactory<ICalculator>("CalculatorService"))
                {
                    ICalculator calculator = ChannelFactory.CreateChannel();
                    using (calculator as IDisposable)
                    {
                        Console.WriteLine("x+y={2} where x={0} and y={1}", 1, 2, calculator.Add(1,2));
                        
                    }
                }
    
                Console.ReadLine();
            }
        }
    }
    View Code

    Clients,Appconfig

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <customBinding>
            <binding name="SimpleBinding">
              <textMessageEncoding />
              <httpTransport />
            </binding>
          </customBinding>
        </bindings>
        <client>
            <endpoint address="http://127.0.0.1:9999/CalculatorService" binding="customBinding" 
                      bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculator"
                      name="CalculatorService"></endpoint>
        </client>
      </system.serviceModel>
    </configuration>
    View Code
  • 相关阅读:
    Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
    js javascript 容错处理代码屏蔽js错误
    springmvc访问项目默认先访问后台再返回首页
    杂记-格式化Date默认格式,日期加一天,jstl判断字符类型,ajax模拟from表单后台跳转页面,jstl访问数据库并在页面显示
    测试覆盖率工具EclEmma安装与使用
    GSON使用之对特殊字符的转换的处理
    eclipse启动自己添加的tomcat时tomcat的webapps下没有项目
    接口和抽象类
    枚举
    Java异常
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3362425.html
Copyright © 2011-2022 走看看