zoukankan      html  css  js  c++  java
  • wCF REST

    关于UriTemplate

    通过浏览器直接访问需要使用WebGet标注

    使用WebInvoke(UriTemplate = "ip/{btime}/{clientId}") 这样的属性时,方法参数列表需要时字符串类型
    使用ip?btime={bitme}&clientId={clientId} 则可使用对应类型的方法定义
    即UriTemplate部分的参数需要使用字符串理想而Query部分(?后面)可以使用对应类型

    关于连接问题
    测试了下面的代码(使用 netstat -p tcp 查看)

    View Code
            static List<object> list = new List<object>();
            static ChannelFactory<IDynamicIpService> channelFactory = new ChannelFactory<IDynamicIpService>("DynamicIpService");
    
            static void Main(string[] args)
            {
                //for (int i = 1; i < 10; i++)
                //{
                    
                   
    
                //        IDynamicIpService proxy = channelFactory.CreateChannel();
                //        IDynamicIpService proxy2 = channelFactory.CreateChannel();
                //        IDynamicIpService proxy3 = channelFactory.CreateChannel();
                //        proxy.RegIp("1000");
                //        proxy2.RegIp("1001");
                //        proxy3.RegIp("1002");
    
    
                //        //(proxy as ICommunicationObject).Close();
                //        //(proxy2 as ICommunicationObject).Close();
                //        //(proxy3 as ICommunicationObject).Close();
    
                //        //channelFactory.Close();
                    
                    
                //}
                IDynamicIpService proxy4 = channelFactory.CreateChannel();
                proxy4.RegIp("1005");
                Console.ReadKey();
    
                Console.WriteLine("再高次!");
                //IDynamicIpService proxy4 = channelFactory.CreateChannel();
                proxy4.RegIp("1004");
                Console.WriteLine("又搞好一次!");
                Console.ReadKey();
            }

    发现一个console进程会开启一个tcp连接,tcp连接数跟proxy个数,channelFactory个数没关心,显式关闭proxy,channelFactory都不影响tcp连接的关闭,
    tcp连接在到了其超时时间后自动关闭,而且再次使用proxy时会重新生成tcp连接(当然对于的channelFactory与proxy不要使用close()关闭);

    具体Demo

    下载

    注意点:服务器启动代码(console)

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel.Web;
    using System.ServiceModel.Description;
    using System.ServiceModel.Channels;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.Web.Routing;
    namespace DynamicIP.Server
    {
        class Program
        {
            static void Main(string[] args)
            {
                //不使用配置文件时需要给出Uri
                //WebServiceHost host = new WebServiceHost(typeof(ServiceImp), new Uri("http://192.168.1.30:5555"))
    
                //使用配置文件时,配置文件endpoint的name属性必需是ServiceImp的完全限定名
                //多个服务需要使用多个host,不过一般用iis来做
                //或者ServiceImp实现N多个接口(部分类)
                using (WebServiceHost host = new WebServiceHost(typeof(ServiceImp)))
                {
    
    
                    host.Open();
                    Console.WriteLine("服务器启动");
                    Console.Read();
                }
    
    
            }
        }
    }

    服务端配置文件:

    View Code
    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="DynamicIP.Server.ServiceImp">
            <endpoint address="http://192.168.1.30:5555"
                      binding="webHttpBinding"
                      contract="DynamicIP.Interface.IService"/>
            <endpoint address="http://192.168.1.30:5555/Math"
              binding="webHttpBinding"
              contract="DynamicIP.Interface.IMyMath"/>
          </service>
        </services>
      </system.serviceModel>
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
    
    </configuration>
  • 相关阅读:
    jQuery 语法
    jQuery 简介
    把数据存储到 XML 文件
    XML 注意事项
    XML DOM (Document Object Model) 定义了访问和操作 XML 文档的标准方法。
    通过 PHP 生成 XML
    XML 命名空间(XML Namespaces)
    XML to HTML
    XMLHttpRequest 对象
    使用 XSLT 显示 XML
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3016184.html
Copyright © 2011-2022 走看看