热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.netcore(RESTful).netcore控制台程序使用依赖注入(Autofac)

大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.netcore(RESTful)近来在考虑一个服务选型,dotnet提供了众多的远程服务形

大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)

近来在考虑一个服务选型,dotnet提供了众多的远程服务形式。在只考虑dotnet到dotnet的情形下,我们可以选择remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)
其中我考察的重点是前4项的效率差异,而在我的测试项目中他们共用同一个接口定义

[ServiceContract]
public interface ICalc
{
    [OperationContract]
    [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            RespOnseFormat= WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "cmd")]
    CalcInfo Calc(CalcInfo pInfo);
}

 

先来对比传入较为简单的CalcInfo,该类的json只有如下,其中集合Items是空的

{
    "id": 0,
    "method": "test",
    "para1": 0,
    "para2": 0,
    "result": 999,
    "items": []
}

测试从客户端将一个CalcInfo的实例传入服务端,服务端稍加修改Method返回给客户端的过程,主要测试序列化与传输的用时。记录执行10000次请求的总用时

Remoting用时 3.76s
WCF(http) 用时 21.92s
WCF(tcp)用时 5.96s
WCF(RESTful)用时 2.26s
asp.net Core(RESTfull)用时 16.59s 

 

再来一下比较传输一个较大的CalcInfo,该类的json如下,其中items集合有100个子实例

{
    "id": 0,
    "method": "test",
    "para1": 0,
    "para2": 0,
    "result": 999,
    "items": [
        {
            "name": "test 1",
            "para1": 1,
            "para2": 0
        },
        {
            "name": "test 2",
            "para1": 2,
            "para2": 0
        }
//....共有100个子项
    ]
}

Remoting用时 12.94s
WCF(http) 用时  47.38s
WCF(tcp)用时 9.2s
WCF(RESTful)用时 10.67s
asp.net Core(RESTfull)用时 26.08s

把两个时间放在一起对比一下就很有意思了。小流量时remoting比WCF强,WCF(TCP)莫名的高用时,比json的还要高。在大流量的时候WCF的优势体现出来了,基本符合网上大致的观点。其中WCF(RESTful)让kevin-y印象深刻,几乎与TCP一样的成绩

小流量

Remoting用时 3.76s
WCF(http) 用时 21.92s
WCF(tcp)用时 5.96s
WCF(RESTful)用时 2.26s
asp.net Core(RESTfull)用时 16.59s 

大流量

Remoting用时 12.94s
WCF(http) 用时  47.38s
WCF(tcp)用时 9.2s
WCF(RESTful)用时 10.67s
asp.net Core(RESTfull)用时 26.83s

测试的源代码来这
https://files.cnblogs.com/files/kevin-Y/SrvSpeed2.zip

 
 
 

.net core 控制台程序使用依赖注入(Autofac)
 

1、Autofac IOC 容器 ,便于在其他类获取注入的对象

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Reflection;
using  Autofac;
using  Autofac.Core;
using  Autofac.Extensions.DependencyInjection;
using  Microsoft.Extensions.DependencyInjection;
 
namespace  BackToCOS.IoC
{
     ///
     /// Autofac IOC 容器
     ///
     public  class  AutofacContainer
     {
         private  static  ContainerBuilder _builder =  new  ContainerBuilder();
         private  static  IContainer _container;
         private  static  string [] _otherAssembly;
         private  static  List _types =  new  List();
         private  static  Dictionary _dicTypes =  new  Dictionary();
 
         ///
         /// 注册程序集
         ///
         /// 程序集名称的集合
         public  static  void  Register( params  string [] assemblies)
         {
             _otherAssembly = assemblies;
         }
 
         ///
         /// 注册类型
         ///
         ///
         public  static  void  Register( params  Type[] types)
         {
             _types.AddRange(types.ToList());
         }
         ///
         /// 注册程序集。
         ///
         ///
         ///
         public  static  void  Register( string  implementationAssemblyName,  string  interfaceAssemblyName)
         {
             var  implementatiOnAssembly= Assembly.Load(implementationAssemblyName);
             var  interfaceAssembly = Assembly.Load(interfaceAssemblyName);
             var  implementatiOnTypes=
                 implementationAssembly.DefinedTypes.Where(t =>
                     t.IsClass && !t.IsAbstract && !t.IsGenericType && !t.IsNested);
             foreach  ( var  type  in  implementationTypes)
             {
                 var  interfaceTypeName = interfaceAssemblyName +  ".I"  + type.Name;
                 var  interfaceType = interfaceAssembly.GetType(interfaceTypeName);
                 if  (interfaceType.IsAssignableFrom(type))
                 {
                     _dicTypes.Add(interfaceType, type);
                 }
             }
         }
         ///
         /// 注册
         ///
         ///
         ///
         public  static  void  Register()  where  TImplementation : TInterface
         {
             _dicTypes.Add( typeof (TInterface),  typeof (TImplementation));
         }
 
         ///
         /// 注册一个单例实体
         ///
         ///
         ///
         public  static  void  Register(T instance)  where  T: class
         {
             _builder.RegisterInstance(instance).SingleInstance();
         }
 
         ///
         /// 构建IOC容器
         ///
         public  static  IServiceProvider Build(IServiceCollection services)
         {
             if  (_otherAssembly !=  null )
             {
                 foreach  ( var  item  in  _otherAssembly)
                 {
                     _builder.RegisterAssemblyTypes(Assembly.Load(item));
                 }
             }
 
             if  (_types !=  null )
             {
                 foreach  ( var  type  in  _types)
                 {
                     _builder.RegisterType(type);
                 }
             }
 
             if  (_dicTypes !=  null )
             {
                 foreach  ( var  dicType  in  _dicTypes)
                 {
                     _builder.RegisterType(dicType.Value).As(dicType.Key);
                 }
             }
 
             _builder.Populate(services);
             _cOntainer= _builder.Build();
             return  new  AutofacServiceProvider(_container);
         }
 
         ///
         ///
         ///
         ///
         ///
         public  static  T Resolve()
         {
             return  _container.Resolve();
         }
 
         public  static  T Resolve( params  Parameter[] parameters)
         {
             return  _container.Resolve(parameters);
         }
 
         public  static  object  Resolve(Type targetType)
         {
             return  _container.Resolve(targetType);
         }
 
         public  static  object  Resolve(Type targetType,  params  Parameter[] parameters)
         {
             return  _container.Resolve(targetType, parameters);
         }
     }
}

  2、用nuget安装

       using Microsoft.Extensions.Configuration;
       using Microsoft.Extensions.DependencyInjection;

      3、Program类如下

      

 1 using BackToCOS.IoC;
 2 using log4net;
 3 using Microsoft.Extensions.Configuration;
 4 using Microsoft.Extensions.DependencyInjection;
 5 using Microsoft.Extensions.Logging;
 6 using Myvas.AspNetCore.TencentCos;
 7 using System;
 8 using System.IO;
 9 using Topshelf;
10 
11 namespace BackToCOS
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             var cOnfiguration= new ConfigurationBuilder()
18               .SetBasePath(Directory.GetCurrentDirectory())
19               .AddJsonFile("appsettings.json", true, true)
20               .AddJsonFile("appsettings.Development.json", true, true)
21               .Build();
22             IServiceCollection services = new ServiceCollection();
23           
24             services.AddTencentCos(optiOns=>
25             {
26                 options.SecretId = configuration["TencentCos:SecretId"];
27                 options.SecretKey = configuration["TencentCos:SecretKey"];
28             });
29             services.AddLogging(builder => builder
30                .AddConfiguration(configuration.GetSection("Logging"))
31                .AddConsole());
32             //注入
33             services.AddSingleton();
34             //用Autofac接管
35             AutofacContainer.Build(services);
36             log4net.Config.XmlConfigurator.ConfigureAndWatch(LogManager.CreateRepository("NETCoreRepository"), new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
37             HostFactory.Run(x =>
38             {
39                 x.UseLog4Net();
40                 x.Service();
41                 x.RunAsLocalSystem();
42                 x.SetDescription("备份到cos的服务");
43                 x.SetDisplayName("备份到cos的服务");
44                 x.SetServiceName("BackToCOS");
45                 x.EnablePauseAndContinue();
46             });
47         }
48     }
49 
50 }

4、用容器获取事例(非构造函数)

 1 using log4net;
 2 using Microsoft.Extensions.Options;
 3 using Myvas.AspNetCore.TencentCos;
 4 using Quartz;
 5 using System;
 6 using System.Collections.Generic;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 
10 namespace BackToCOS.Jobs
11 {
12     //DisallowConcurrentExecution属性标记任务不可并行
13     [DisallowConcurrentExecution] 
14     public class BackupJob : IJob
15     {
16         private readonly ILog _log = LogManager.GetLogger("NETCoreRepository", typeof(BackupJob));
17         public Task Execute(IJobExecutionContext context)
18         {
19             try
20             {
21                 _log.Info("测试任务,当前系统时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
22                 ITencentCosHandler tencentCosHandler = IoC.AutofacContainer.Resolve();
23                 var ss = tencentCosHandler.AllBucketsAsync();
24                 return Task.CompletedTask;
25             }
26             catch (Exception ex)
27             {
28                 JobExecutionException e2 = new JobExecutionException(ex);
29                 _log.Error("测试任务异常", ex);
30             }
31             return Task.CompletedTask;
32         }
33     }
34 }

 


推荐阅读
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 基于词向量计算文本相似度1.测试数据:链接:https:pan.baidu.coms1fXJjcujAmAwTfsuTg2CbWA提取码:f4vx2.实验代码:imp ... [详细]
  • Spring 源码阅读 74:事务管理的原理BeanFactoryTransactionAttributeSourceAdvisor 分析
    本文通过对BeanFactoryTransactionAttributeSourceAdvisor类的分析,了解了Spring是如何通过AOP来完成事务的管理的&#x ... [详细]
  • 背景后端使用Nginx并更改本地host文件,起本地服务。将aaa.bbbb.com代理至本地IP地址(10.26.36.156)。使用$.ajax调用后端restful接口,要求 ... [详细]
  • 本文为Codeforces 1294A题目的解析,主要讨论了Collecting Coins整除+不整除问题。文章详细介绍了题目的背景和要求,并给出了解题思路和代码实现。同时提供了在线测评地址和相关参考链接。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Java的集合及其实现类,包括数据结构、抽象类和具体实现类的关系,详细介绍了List接口及其实现类ArrayList的基本操作和特点。文章通过提供相关参考文档和链接,帮助读者更好地理解和使用Java的集合类。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 文章接口
    用户关注列表(难点)原型效果图需求分析:内容:互相关注和已关注举例:当前用户:curry关注列表:kobe&# ... [详细]
author-avatar
可心
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有