遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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>
复制代码

 

posted on   遗忘海岸  阅读(781)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2012-04-12 一个触发器
点击右上角即可分享
微信分享提示