dotnetcore迁移方法初步


dotnet core出2.0了。把一些现有代码试着做了下迁移,出乎意料的顺利。


这里分享一些有用的nuget包,和有用的代码:

nuget包名引用原因其他
Microsoft.AspNetCore.HttpHttp处理HttpContext.Current方法需要替代方案
Microsoft.AspNetCore.Mvc.CoreHttp处理 
Microsoft.Extensions.Caching.MemoryMemoryCache需要补充Contains方法
Microsoft.Extensions.Configuration配置文件 
Newtonsoft.JsonJson 
StackExchange.RedisRedis 
System.Configuration.ConfigurationManagerAppSettings和ConnectionStringapp.config需要自己手动增加
System.Data.SqlClientSQLConnection注意从linux访问的时候,sqlserver需要2008sp4以上版本。
System.Net.HttpHttp处理 
Microsoft.PinYinConverter中文处理这个nuget包比较特别,是4.6.1的,但是dotnetcore声称可以直接引用。需要测试。
Magick.NET-Q8-AnyCPU图像处理Bitmap都要改用ImageMagick.MagickImage。
不能直接替换。坐标和字体需要注意。
   
   


再贴点有用的代码:

//代替HttpContext.Current
public partial class MyHttpContext
{
    public static IServiceProvider ServiceProvider;

    /// <summary>
    /// 注意多线程下这个方法可能不准确
    /// </summary>
    public static Microsoft.AspNetCore.Http.HttpContext Current
    {
        get
        {
            object factory = ServiceProvider.GetService(
                typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
            Microsoft.AspNetCore.Http.HttpContext context = 
                ((Microsoft.AspNetCore.Http.HttpContextAccessor)factory).HttpContext;
            return context;
        }
    }

}

//为MemoryCache补上Contains方法
public static class DefaultExtentions
{
    public static bool Contains(this MemoryCache mc, string key)
    {
        if (mc.Get(key) == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

//dotnetcore目前没有自带hex处理
public static byte[] HexStringToBytes(string hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

//
public static string BytesToHexString(byte[] ba)
{
    StringBuilder hex = new StringBuilder(ba.Length * 2);
    foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
    return hex.ToString();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值