分类目录归档:.net

.NET Core 2.0


点击查看原图

  下载 Visual Studio 2017 version 15.3

  下载 .NET Core 2.0

  下载 Visual Studio for Mac

点击查看原图

  微软今天发布了.NET Core 2.0 版本,属于一次非常大的版本迭代。

  .NET Core 2.0 主要包括一些让 .NET Core 更容易使用的改进,并增强其平台能力。亮点如下:

  Runtime

  SDK

  Visual Studio

  • Live Unit Testing supports .NET Core

  • Code navigation improvements

  • C# Azure Functions support in the box

  • CI/CD support for containers

点击查看原图

  主要更新方面,包括对两个关键组成部分 Runtime(CoreCLR)和 Framework Libraries(CoreFX,框架库)进行了完整的性能优化,由此可见,进程管理、JIT 编译器以及服务器系统的体验将会更好。

  同时,引入 .NET Standard 2.0,使得开发人员可利用的 API 数量翻了不止两倍。另外,微软还强调,.NET Core 2.0 已经可以用于部署 Azure Web 应用。

  更多细节可查阅发行说明


  .NET Standard 2.0 发布,增大 API 范围

  .NET Standard 2.0 规范现已完成,支持以下平台:

  • .NET Framework 4.6.1

  • .NET Core 2.0

  • Mono 5.4

  • Xamarin.iOS 10.14

  • Xamarin.Mac 3.8

  • Xamarin.Android 7.5

  • 即将推出 UWP 版本 (预计今年晚些时候)

  .NET Standard 2.0 自 .NET Standard 1.X 的基础上大大增加了 API 范围,这意味着将现有代码从
.NET Framework 移植到 .NET Standard 变得更加容易。它还添加了一种兼容性模式,用于引用 .NET Standard
中现有的 .NET Framework 二进制文件。

点击查看原图

  更多细节和内容请查阅发行说明


  ASP.NET Core 2.0 发布,引进 Razor Pages 编码范例

  ASP.NET 团队宣布 ASP.NET Core 2.0 发布,此版本与 .NET Core 2.0 兼容,支持 Visual Studio 2017 15.3 版本,并引进了新的 Razor Pages 用户界面设计范例。

  有关更新的完整列表,可以阅读更新日志

  最新的 SDK 和工具可从 https://dot.net/core 下载。

  ASP.NET Core 2.0 添加了许多新功能,使 Web 应用的构建和监控更加轻松,并提高性能。

  将项目更新至 ASP.NET Core 2.0

  ASP.NET Core 2.0 在 .NET Framework 4.6.1 和 .NET Core 2.0 上运行,因此 1.x 版本的 .NET Core 需要将项目中的目标框架更新为 netcoreapp2.0 。详情

点击查看原图

  Razor Pages

  这个新的编码范例,旨在让编写基于页面的场景比目前的模型 - 视图 - 控制器架构更容易。Razor Pages 是一个页面优先(page-first)的结构,可让你专注于用户界面,并通过编写 PageModel 对象来简化服务器端的体验。详情

  模板更新

点击查看原图

点击查看原图

  此外还包括 Razor 引擎支持 C#7.1 、简化应用主机配置、提供性能分析、错误报告和诊断集成等改进,详情查阅发行说明


  Entity Framework Core 2.0 正式版发布

  Entity Framework Core 2.0 正式版本已发布,它是 Entity Framework 的轻量级、可扩展和跨平台版本,是 .NET 的对象/关系映射(O / RM)框架。

  更新亮点:

  • .NET Standard 2.0

  • Improved LINQ translation

  • Like query operator

  • Owned entities and Table Splitting

  • Global query filters

  • DbContext Pooling

  • String interpolation in raw SQL methods

  各项具体细节和使用方式请查阅发行说明


  值得一提的是,今天,微软还发放了 Visual Studio 2017 v15.3 和 Visual Studio for Mac v7.1。

点击查看原图

点击查看原图

  Visual Studio 2017 version 15.3

  该版本包含 1700 多项改进,主要专注于可用性的改进,尤其是在 low-vision 和 no-vision 模式下使用 Visual Studio 2017 感觉会尤为明显。

  主要包括:

  • 调试更易于使用

  • VS 编辑器的文字修饰会让开发者了解一系列代码上特定的功能

  • 修复可靠性问题来提高性能

  • Azure Functions 支持

  • Broad Azure 登录支持

  • 容器支持改进

  • 内置持续交付工具

  完整的改进清单,请查看 Visual Studio 2017 15.3 的更新日志发行说明

  Visual Studio for Mac version 7.1

  Visual Studio for Mac 7.1 增加了对 .NET Core 2.0 的支持,它还可以在项目中创建 .NET Standard 2.0 ,以跨项目共享更多代码。此外,也包括许多可靠性改进,减少内存占用,改进性能,减少崩溃。详情

.NET技术

问题

大家是否想过:

  • .NET究竟包含哪些技术呢?
  • 我所掌握的技术这个子集,在.NET技术大系这个超集里面占地比例是什么呢?
  • 我究竟还没有掌握多少.NET技术呢?
  • 面试的时候会考哪些技术呢?

相信大家也看过Java的技术栈,一个金字塔形状的(我一下子没找回,如果你知道,烦请告诉我),包含了繁多的技术:
点击查看原图

查看大图

这些年来,看过n多.Net架构图/技术栈,搜索了很久,没有找到一个符合我要求的“较为全面”地表述.Net技术大系的图表。相信微软内部有更全面的,但可惜我没有找到。

互联网上来去都是那几个简单的按版本新增分类等10来个点的,譬如这个:

 点击查看原图

或者这个按照.NET版本新增技术的:
点击查看原图

圣殿骑士 在 新加坡架构师面试总结 里面发表过一张.NET架构师面试需要的技术总结图:

点击查看原图
 

查看大图

不过这个总结图面向的是.NET架构师需要掌握的技术,和我需要的不一样。

 

.NET技术栈

最近找工作,面试了多家公司,不同公司有不同的技术要求,我把这些技术,结合我已经掌握的技术,总结了一下.Net相关技术,大约10个大类50个技术点,花了一点时间搞个.NET技术栈概览。

 

从最底层的到最顶层:

  • 操作系统
  • CLI
  • CLR
  • CTS/CLS/CIL
  • .NET Fx
  • CLI 语言
  • DLR
  • DLR 语言
  • Desktop 桌面
  • Web 网络
  • SOA
  • SDKs
  • OWIN
  • IDE 编辑器
  • Platforms 平台
  • Productivity 生产力
  • Build 编译
  • Testing 测试
  • Deploy 发布
  • Tools 工具
  • Data 数据存储
  • Components 组件

 

 

这个图表里的分类未必准确,相关技术也难免会有遗漏,欢迎大家指点以便不断改进。

以下是预览图:

点击查看原图


查看大图

摘自: http://www.cnblogs.com/unruledboy/p/NetStack.html

windows 7/windows 2008/ tls1.2 / .net

Disable RC2 RC4 And SSL 2.0

https://support.microsoft.com/en-us/help/245030/how-to-restrict-the-use-of-certain-cryptographic-algorithms-and-protocols-in-schannel.dll

 Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Client]
"DisabledByDefault"=dword:00000001
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server]
"Enabled"=dword:00000000
"DisabledByDefault"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Client]
"Enabled"=dword:00000000
"DisabledByDefault"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0\Server]
"Enabled"=dword:00000000
"DisabledByDefault"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client]
"Enabled"=dword:00000001
"DisabledByDefault"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server]
"Enabled"=dword:00000001
"DisabledByDefault"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Server]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client]
"DisabledByDefault"=dword:00000000
"Enabled"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server]
"Enabled"=dword:00000001
"DisabledByDefault"=dword:00000000

 Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers]

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC4 128/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC4 64/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC4 56/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC4 40/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC2 128/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC2 56/128]
"Enabled"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\RC2 40/128]
"Enabled"=dword:00000000

Protocol

KEA

SYM (bit)

HSH (bit)

CipherSuite

TLS1.0

RSAKeyX

AES (128)

SHA1 (160)

TLS_RSA_WITH_AES_128_CBC_SHA    

SSL3.0

RSAKeyX

RC4 (128)

SHA1 (160)

SSL_RSA_WITH_RC4_128_SHA

SSL2.0

RSAKeyX

RC4 (128)

MD5 (128)

SSL_CK_RC4_128_WITH_MD5

TLS 1.2 and Microsoft.Net

Now lets focus on using TLS 1.2 in .Net world. We need to make sure that
the web sites are served via TLS 1.2 protocol and client apps which are
consuming the same need to support TLS 1.2. .Net is running on top of
operating system and mostly its windows. If host windows supports TLS
1.2 .Net can also support TLS 1.2 as it relies on schannel.dll1
The first task here is to make sure we are using the right tools and technologies.


TLS 1.2 and .Net Framework 4.5


.Net is also versioned. Versions below 4.5*doesn't know how to
communicate via TLS 1.2.In .Net 4.5 the TLS 1.2 is enabled
by default.
Simply compile our applications in ,Net 4.5 and we will get TLS 1.2
communication for our applications.


How to make .Net 4.0 app talk using TLS 1.2


Technically speaking just recompile existing older application to .Net
4.5 to get TLS 1.2 support. It sounds simple as everybody expecting that
there are no breaking changes in .Net 4.5 and our application will
function as is. But if we are serious about delivering quality software
we also need to test entire app in 4.5 before shipping. It really adds
cost.

Lets see if there are any ways to use TLS 1.2 by .Net 4.0 apps.

System.dll overwrite

When we install .Net 4.5 its basically adding changes on top of .Net
4.0. In other words the System.dll used for 4.0 apps will be overwritten
to 4.5 version of System.dll. So there are possibilities that .Net 4.0
apps will execute 4.5 code when they access functions in System.dll.
Which means if we have .Net 4.5 installed in the machine where our .Net
4.0 is running it can take advantage of TLS1.2. All our solutions below
are depending on this factor.
Below links explains the .Net versioning and overwriting.


1.Code change in 4.0 to use TLS 1.2

Now its the matter of changing the default protocol used by 4.0 to TLS
1.2. This can be done by forcefully changing the  protocol as below.

//SecurityProtocolType.Tls1.2;

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

 

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
| SecurityProtocolType.Tls11 
| SecurityProtocolType.Tls;

// comparable to modern browsers
var request = WebRequest.Create("https://www.howsmyssl.com/a/check");
var response = request.GetResponse(); 
var body = new StreamReader(response.GetResponseStream()).ReadToEnd();


If we look at the SecurityProtocolType enum for .Net 4.0, we will not be able to see the TLS1.2. But in 4.5 we can see that. So .Net 4.0 will not compile if we use TLS1.2 enum value. But if we use the TLS1.2 enum number it will compile and at runtime since the .Net 4.0's System.dll is replaced with 4.5 the cast will work. 

 
Please note that this will fail if we are running the same app in a machine which don't have 4.5 installed. Recommended only for servers where its easy to manage the .Net version.


2.Registry change to force .Net 4.0 to use TLS 1.2

If we inspect the .Net 4.5 ServicePointManager source code we can see that the default protocol is depending on the below registry entry.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319
    SchUseStrongCrypto to DWORD 1

The default value will be 0. Simply change this to 1 to get .Net 4.5
System.dll use TLS 1.2. Since our 4.0 application uses 4.5 System.dll
4.0 gets TLS 1.2 support.

http://stackoverflow.com/questions/28286086/default-securityprotocol-in-net-4-5


References

https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Client_.28Browser.29_Configuration
https://www.simple-talk.com/dotnet/.net-framework/tlsssl-and-.net-framework-4.0/
https://msdn.microsoft.com/en-us/library/system.security.authentication.sslprotocols(v=vs.110).aspx
https://istlsfastyet.com/
http://blogs.msdn.com/b/benjaminperkins/archive/2014/11/04/using-tls-1-2-with-wcf.aspx
http://blogs.msdn.com/b/benjaminperkins/archive/2011/10/07/secure-channel-compatibility-support-with-ssl-and-tls.aspx
http://www.dotnetnoob.com/2013/10/hardening-windows-server-20082012-and.html

TLS Cipher Suites in Windows 7
https://msdn.microsoft.com/en-us/library/windows/desktop/mt767780%28v=vs.85%29.aspx
TLS Cipher Suites in Windows 8
https://msdn.microsoft.com/en-us/library/windows/desktop/mt762882%28v=vs.85%29.aspx
TLS Cipher Suites in Windows 8.1
https://msdn.microsoft.com/en-us/library/windows/desktop/mt767781%28v=vs.85%29.aspx
TLS Cipher Suites in Windows 10 v1507
https://msdn.microsoft.com/en-us/library/windows/desktop/mt767769%28v=vs.85%29.aspx
TLS Cipher Suites in Windows 10 v1511
https://msdn.microsoft.com/en-us/library/windows/desktop/mt767768%28v=vs.85%29.aspx
TLS Cipher Suites in Windows 10 v1567
https://msdn.microsoft.com/en-us/library/windows/desktop/mt490158%28v=vs.85%29.aspx

Tools

for .net framework 4.5
SSLTLSCheck.zip

点击查看原图

nuget


下载地址:

命令行: http://nuget.codeplex.com/downloads/get/669083
命令行: http://nuget.org/downloads
图形窗: https://github.com/NuGetPackageExplorer

*.nuspec

Folder Description Action upon package install
tools Powershell scripts and programs accessible from the Package Manager Console Contents are copied to the project folder, and the tools folder is added to the PATH environment variable.
lib Assembly(.dll) files (.dll), documentation (.xml) files, and symbol (.pdb) files Assemblies are added as references; .xml and .pdb copied into project folders.
content Arbitrary files Contents are copied to the project root
build MSBuild .targets and .props files Automatically inserted into the project file (NuGet 2.x) or project.json.lock (NuGet 3.x).
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<!-- The identifier that must be unique within the hosting gallery -->
<id>Contoso.Utility.UsefulStuff</id>
<!-- The package version number that is used when resolving dependencies -->
<version>1.8.3.331</version>
<!-- Authors contain text that appears directly on the gallery -->
<authors>Dejana Tesic, Rajeev Dey</authors>
<!-- Owners are typically nuget.org identities that allow gallery
users to earily find other packages by the same owners.  -->
<owners>dejanatc, rjdey</owners>
<!-- License and project URLs provide links for the gallery -->
<licenseUrl>http://opensource.org/licenses/MS-PL</licenseUrl>
<projectUrl>http://github.com/contoso/UsefulStuff</projectUrl>
<!-- The icon is used in Visual Studio's package manager UI -->
<iconUrl>http://github.com/contoso/UsefulStuff/nuget_icon.png</iconUrl>
<!-- If true, this value prompts the user to accept the license when
installing the package. -->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<!-- Any details about this particular release -->
<releaseNotes>Bug fixes and performance improvements</releaseNotes>
<!-- The description can be used in package manager UI. Note that the
nuget.org gallery uses information you add in the portal. -->
<description>Core utility functions for web applications</description>
<!-- Copyright information -->
<copyright>Copyright ©2016 Contoso Corporation</copyright>
<!-- Tags appear in the gallery and can be used for tag searches -->
<tags>web utility http json url parsing</tags>
<!-- Dependencies are automatically installed when the package is installed -->
<dependencies>
<dependency id="Newtonsoft.Json" version="9.0" />
</dependencies>
</metadata>
<!-- A readme.txt will be displayed when the package is installed -->
<files>
<file src="readme.txt" target="" />
</files>
</package>


命令:

nuget spec
nuget pack ClassLibrary2.csproj -Build -Properties Configuration=Release
nuget push *.nupkg -s http://127.0.0.1 apikey

iis6:
需要添加通配符程序
点击查看原图
点击查看原图
注意64bit/32bit
点击查看原图

ASP.NET URL

Browser Request 的網址相關的屬性與用法:

網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath /
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx
Request.Path /News/Press/Content.aspx/123
Request.RawUrl /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath /News/Press/Content.aspx/123
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1897
Request.Url.Authority localhost:1897
Request.Url.LocalPath /News/Press/Content.aspx/123
Request.PathInfo /123
Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1
Request.Url.Query ?id=1
Request.Url.Fragment  
Request.Url.Segments /
News/
Press/
Content.aspx/
123

.

.NET Platform Standard

    .NET Platform Standard 直译过来就是 .NET 平台规范或标准,它的目的就是使 .NET 各个平台之间更加统一和规范 .NET Standard Library 现在有一个对应程序包NETStandard.Library,它的作用是兼容各个 .NET Platform。

 

.NET Platform Standard 列表:

Target Platform Name Alias
.NET Platform Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6
.NET Core netcoreapp 1.0
.NET Framework net 4.6.3
4.6.2
4.6.1
4.6
4.5.2
4.5.1
4.5
Universal Windows Platform uap 10.0
Windows win 8.1
8.0
Windows Phone wpa 8.1
Windows Phone Silverlight wp 8.1
8.0
Mono/Xamarin Platforms *
Mono *

 

分别参考:

.NET Platform Standard:https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md

Introducing .NET Standard :https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

理解 .NET Platform Standard :http://www.cnblogs.com/xishuai/archive/2016/05/24/understand-dotnet-platform-standard.html

 

imports自身发布不携带依赖的程序包,而是使用系统中安装配置的,net45就是上面说的frameworks配置,win81是系统平台的意思,但不只是特指 Windows 8.1 系统。

Platform NuGet identifier
.NET Framework 2.0 - 4.6 net20 - net46
.NET Core netcoreapp
.NET Micro Framework netmf
Windows 8 win8, netcore45
Windows 8.1 win8, netcore451
Windows Phone Silverlight (8, 8.1) wp8, wp81
Windows Phone 8.1 wpa8.1
Universal Windows Platform 10 uap10, netcore50
Silverlight 4, 5 sl4, sl5
MonoAndroid monoandroid
MonoTouch monotouch
MonoMac monomac
Xamarin iOS xamarinios
Xamarin PlayStation 3 xamarinpsthree
Xamarin PlayStation 4 xamarinpsfour
Xamarin PlayStation Vita xamarinpsvita
Xamarin Watch OS xamarinwatchos
Xamarin TV OS xamarintvos
Xamarin Xbox 360 xamarinxboxthreesixty
Xamarin Xbox One xamarinxboxone

 

 

 

C# UDP 大数据分包传输

如果需要使用UDP传输较大数据,例如传输10M的图片,这突破了UDP的设计原则。UDP的设计是基于"datagram",也就是它假设你发送的每个数据包都能包含在单一的包内。并且设定UDP数据包的最大长度受基础网络协议的限制。

点击查看原图

UDP数据包的理论最大长度限制是 65535 bytes,这包含 8 bytes 数据包头和 65527 bytes 数据。但如果基于IPv4网络传输,则还需减去 20 bytes 的IP数据包头。
则单一的UDP数据包可传输的数据最大长度为:

MaxUdpDataLength = 65535 - 8 - 20 = 65507 bytes

这就需要实现UDP包的分包传输和接收组包功能。

 

分包功能

/// <summary>
/// UDP数据包分割器
/// </summary>
public static class UdpPacketSplitter
{
  /// <summary>
  /// 分割UDP数据包
  /// </summary>
  /// <param name="sequence">UDP数据包所持有的序号</param>
  /// <param name="datagram">被分割的UDP数据包</param>
  /// <param name="chunkLength">分割块的长度</param>
  /// <returns>
  /// 分割后的UDP数据包列表
  /// </returns>
  public static ICollection<UdpPacket> Split(long sequence, byte[] datagram, int chunkLength)
  {
    if (datagram == null)
      throw new ArgumentNullException("datagram");

    List<UdpPacket> packets = new List<UdpPacket>();

    int chunks = datagram.Length / chunkLength;
    int remainder = datagram.Length % chunkLength;
    int total = chunks;
    if (remainder > 0) total++;

    for (int i = 1; i <= chunks; i++)
    {
      byte[] chunk = new byte[chunkLength];
      Buffer.BlockCopy(datagram, (i - 1) * chunkLength, chunk, 0, chunkLength);
      packets.Add(new UdpPacket(sequence, total, i, chunk, chunkLength));
    }
    if (remainder > 0)
    {
      int length = datagram.Length - (chunkLength * chunks);
      byte[] chunk = new byte[length];
      Buffer.BlockCopy(datagram, chunkLength * chunks, chunk, 0, length);
      packets.Add(new UdpPacket(sequence, total, total, chunk, length));
    }

    return packets;
  }
}

..............................

发送分包

..............................

private void WorkThread()
{
  while (IsRunning)
  {
    waiter.WaitOne();
    waiter.Reset();

    while (queue.Count > 0)
    {
      StreamPacket packet = null;
      if (queue.TryDequeue(out packet))
      {
        RtpPacket rtpPacket = RtpPacket.FromImage(
          RtpPayloadType.JPEG, 
          packet.SequenceNumber, 
          (long)Epoch.GetDateTimeTotalMillisecondsByYesterday(packet.Timestamp),
          packet.Frame);

        // max UDP packet length limited to 65,535 bytes
        byte[] datagram = rtpPacket.ToArray(); 
        packet.Frame.Dispose();

        // split udp packet to many packets 
        // to reduce the size to 65507 limit by underlying IPv4 protocol
        ICollection<UdpPacket> udpPackets 
          = UdpPacketSplitter.Split(
            packet.SequenceNumber, 
            datagram, 
            65507 - UdpPacket.HeaderSize);
        foreach (var udpPacket in udpPackets)
        {
          byte[] udpPacketDatagram = udpPacket.ToArray();
          // async sending
          udpClient.BeginSend(
            udpPacketDatagram, udpPacketDatagram.Length,
            packet.Destination.Address,
            packet.Destination.Port,
            SendCompleted, udpClient);
        }
      }
    }
  }
}

接收组包功能

..............................

private void OnDatagramReceived(object sender, UdpDatagramReceivedEventArgs<byte[]> e)
{
  try
  {
    UdpPacket udpPacket = UdpPacket.FromArray(e.Datagram);

    if (udpPacket.Total == 1)
    {
      RtpPacket packet = new RtpPacket(udpPacket.Payload, udpPacket.PayloadSize);
      Bitmap bitmap = packet.ToBitmap();
      RaiseNewFrameEvent(
        bitmap, Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));
    }
    else
    {
      // rearrange packets to one packet
      if (packetCache.ContainsKey(udpPacket.Sequence))
      {
        List<UdpPacket> udpPackets = null;
        if (packetCache.TryGetValue(udpPacket.Sequence, out udpPackets))
        {
          udpPackets.Add(udpPacket);

          if (udpPackets.Count == udpPacket.Total)
          {
            packetCache.TryRemove(udpPacket.Sequence, out udpPackets);

            udpPackets = udpPackets.OrderBy(u => u.Order).ToList();
            int rtpPacketLength = udpPackets.Sum(u => u.PayloadSize);
            int maxPacketLength = udpPackets.Select(u => u.PayloadSize).Max();

            byte[] rtpPacket = new byte[rtpPacketLength];
            foreach (var item in udpPackets)
            {
              Buffer.BlockCopy(
                item.Payload, 0, rtpPacket, 
                (item.Order - 1) * maxPacketLength, item.PayloadSize);
            }

            RtpPacket packet = new RtpPacket(rtpPacket, rtpPacket.Length);
            Bitmap bitmap = packet.ToBitmap();
            RaiseNewFrameEvent(
              bitmap, 
              Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));

            packetCache.Clear();
          }
        }
      }
      else
      {
        List<UdpPacket> udpPackets = new List<UdpPacket>();
        udpPackets.Add(udpPacket);
        packetCache.AddOrUpdate(
          udpPacket.Sequence, 
          udpPackets, (k, v) => { return udpPackets; });
      }
    }
  }
  catch (Exception ex)
  {
    RaiseVideoSourceExceptionEvent(ex.Message);
  }
}

.......................

 

 

 

...

.NET Core dotnet 命令大全

命令目录:

dotnet-new

dotnet-restore

dotnet-run

dotnet-build

dotnet-test

dotnet-pack

dotnet-publish

 

现在来详细讲解。

创建(dotnet new)

首先我们创建一个项目 ,这里我们创建控制台程序。

dotnet new

点击查看原图

 

dotnet 更多命令请参考帮助。

 

还原(dotnet restore 及 dotnet pack)

我们在创建一个 class lib  也就是类库。

dotnet new -t lib

点击查看原图

创建好以后,编写一些自己的代码然后进行打包。

点击查看原图

 

我编写了两个方法,现在来打包。

dotnet restore

dotnet pack

点击查看原图

.NET Core 怎样添加离线引用包呢?现在答案就揭晓了。

打包好以后,我们将applib 添加到 app的 project.json 。

点击查看原图

添加好以后切到app 目录,然后进行还原。

还原使用的命令是:dotnet restore -f E:\dotnet\applib\bin\Debug\

dotnet restore -f 包的路径

点击查看原图

这样我们就能直接在项目里调用applib 里面的方法。

我们在代码里编写对应的调用,然后执行程序。可以看到正确输出class lib 的结果。

点击查看原图

 

运行(dotnet build 及 dotnet run)

以app 为例:

dotnet build 编译代码

点击查看原图

 

dotnet run 执行程序

点击查看原图

 

dotnet xx.dll 也是执行程序

点击查看原图

 

测试(dotnet test)

这里我们要新建一个文件夹及项目了。

新建测试项目: dotnet new -t xunittest 

点击查看原图

 

新建好项目我们可以在里面添加测试方法,这里我直接运行测试。

dotnet restore

dotnet test

点击查看原图

 

发布(dotnet publish)

重点来了,发布项目使其可以跨平台运行。

dotnet publish 默认发布

点击查看原图

发布后定位到publish目录使用 dotnet xx.dll 执行应用。

 

下面来跨平台发布。

可以参考我之前的文章:http://www.cnblogs.com/linezero/p/5475246.html

在 project.json 加入 runtimes 节点 注释掉 "type": "platform"

点击查看原图

首先我们要dotnet restore

这里我还原的时候有些久,大家耐心等待一下。

dotnet publish -r win10-x64

点击查看原图

这里发布后,可以直接定位到发布publish目录,执行 app.exe 即可。 

dotnet publish -r ubuntu.14.04-x64

点击查看原图

将publish 文件夹上传至linux系统 ,设置app的权限为可执行,然后 ./app 就可以运行了。

dotnet publish -r 指定runtimes节点里系统ID

 

.

转自: http://www.cnblogs.com/linezero/p/dotnet.html

参考: https://docs.microsoft.com/en-us/dotnet/articles/core/tools/index

C#堆栈、队列、递归实现的快速排序

参考: https://zh.wikipedia.org/zh-hant/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#C.23

static void Qsort(int[] data, int low, int high)//递归实现
        {
            if (low >= high) return;
            int i, j, pivot;
            i = low;
            j = high;
            pivot = data[low];
            while (i < j)
            {
                while (data[j] > pivot) j--;
                data[i] = data[j];
                while (i < j && data[i] <= pivot) i++;
                data[j] = data[i];
            }
            data[i] = pivot;
            Qsort(data, low, i - 1);
            Qsort(data, i + 1, high);
        }
       //堆栈实现
        public static void StackQS(int[] data)
        {
            int i, j, low, high, pivot;
            Stack<int> MyStack = new Stack<int>();
            MyStack.Push(0);
            MyStack.Push(data.Length - 1);
            while (MyStack.Count > 0)
            {
                j = high = MyStack.Pop();
                i = low = MyStack.Pop();
                if (low >= high) continue;
                pivot = data[low];
                while (i < j)
                {
                    while (data[j] > pivot) j--;
                    data[i] = data[j];
                    while (i < j && data[i] <= pivot) i++;
                    data[j] = data[i];
                }
                data[i] = pivot;
                MyStack.Push(low);
                MyStack.Push(i - 1);
                MyStack.Push(i + 1);
                MyStack.Push(high);
            }
        }
      //队列实现
       public static void QueueQS(int[] data)
        {
            int i, j, low, high, pivot;
            Queue<int> MyQueue = new Queue<int>();
            MyQueue.Enqueue(0);
            MyQueue.Enqueue(data.Length-1);
            while(MyQueue.Count>0)
            {
                i = low = MyQueue.Dequeue();
                j = high = MyQueue.Dequeue();
                if (low >= high) continue;
                pivot = data[low];
                while(i<j)
                {
                    while (data[j] > pivot) j--;
                    data[i] = data[j];
                    while (i < j && data[i] <= pivot) i++;
                    data[j] = data[i];
                }
                data[i] = pivot;
                MyQueue.Enqueue(low);
                MyQueue.Enqueue(i-1);
                MyQueue.Enqueue(i+1);
                MyQueue.Enqueue(high);
            }
        }

C#集合--Dictionary

字典(dictionary)是一个集合,其中每个元素都是一个键/值对。字典(Dictionaries)是常用于查找和排序的列表。

.NET Framework通过IDictionary接口和IDictionary<TKey,TValue>接口,以及一些常用的子典了定义了子典协议。每个类在以下方面各有不同:

  • 元素是否已经排序
  • 元素是否能通过索引或键来获取
  • 字典类是generic的还是非generic的
  • 当字段较大时,根据键值获取元素速度的快慢

下表总结了每个字典类,以及它们在上述这几个方面的差异。它们都是在一个1.5G的PC上执行5000次操作得到的一个平均值。

Type 内部结构 支持索引 内存占用 随机插入的速度(毫秒) 顺序插入的速度(毫秒) 根据键获取元素的速度(毫秒)
未排序字典            
Dictionary<T,V> 哈希表 22 30 30 20
Hashtable 哈希表 38 50 50 30
ListDictionary 链表 36 50000 50000 50000
OrderedDictionary 哈希表
+数组
59 70 70 40
排序字典            
SortedDictionary<K,V> 红黑树 20 130 100 120
SortedList<K,V> 2xArray 20 3300 30 40
SortList 2xArray 27 4500 100 180

从时间复杂度来讲,从字典中通过键获取值所耗费的时间分别如下:

  • Hashtable, Dictionary和OrderedDictionary的时间复杂度为O(1)
  • SortedDictionary和SortList的时间复杂度为O(logN)
  • ListDictinary的时间复杂度为O(n)

n是集合元素的数量。

 

摘自: http://www.cnblogs.com/yang_sy/p/3678905.html