常见.NET功能代码汇总

23,获取和设置分级缓存

获取缓存:首先从本地缓存获取,如果没有,再去读取分布式缓存
写缓存:同时写本地缓存和分布式缓存

  private static T GetGradeCache<T>(string key) where T:struct
{
MemoryCacheManager localCache = MemoryCacheManager.Instance;
if (!localCache.IsSet(key))
{
//本地不存在此缓存
T remoteValue = MemCacheManager.Instance.Get<T>(key);
if (!ValueType.Equals(remoteValue, default(T)))
{
//如果远程有
localCache.Set(key, remoteValue, );
}
else
{
localCache.SetFromSeconds(key, default(T), );
}
return remoteValue;
}
T value = localCache.Get<T>(key);
return value;
} private static void SetGradeCache<T>(string key,T Value,int time) where T : struct
{
MemoryCacheManager localCache = MemoryCacheManager.Instance;
localCache.Remove(key);
localCache.Set(key, Value, time);
MemCacheManager.Instance.Remove(key);
MemCacheManager.Instance.Set(key, Value, time);
}

24,求相对目录的绝对路径

有时候,我们需要求相对于当前根目录的相对目录,比如将日志文件存储在站点目录之外,我们可以使用 ../logs/ 的方式:

 string vfileName = string.Format("../logs/{0}_{1}_{2}.log", logFileName, System.Environment.MachineName, DateTime.Now.ToString("yyyyMMdd"));
string rootPath = HttpContext.Current.Server.MapPath("/");
string targetPath = System.IO.Path.Combine(rootPath, vfileName);
string fileName = System.IO.Path.GetFullPath(targetPath);
string fileDir = System.IO.Path.GetDirectoryName(fileName);
if (!System.IO.Directory.Exists(fileDir))
System.IO.Directory.CreateDirectory(fileDir);

这个代码会在站点目录之外的日志目录,建立一个 代机器名称的按照日期区分的日志文件。

25,多次尝试写日志文件方法

日志文件可能会并发的写入,此时可能会提示“文件被另外一个进程占用”,因此可以多次尝试写入。下面的方法会递归的进行文件写入尝试,如果尝试次数用完才会最终报错。

  /// <summary>
/// 保存日志文件
/// </summary>
/// <param name="logFileName">不带扩展名文件名</param>
/// <param name="logText">日志内容</param>
/// <param name="tryCount">如果出错的尝试次数,建议不大于100,如果是0则不尝试</param>
public static void SaveLog(string logFileName, string logText, int tryCount)
{
string vfileName = string.Format("..\\logs\\{0}_{1}_{2}.log", logFileName, System.Environment.MachineName, DateTime.Now.ToString("yyyyMMdd"));
string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
string targetPath = System.IO.Path.Combine(rootPath, vfileName);
string fileName = System.IO.Path.GetFullPath(targetPath);
string fileDir = System.IO.Path.GetDirectoryName(fileName);
if (!System.IO.Directory.Exists(fileDir))
System.IO.Directory.CreateDirectory(fileDir); try
{
System.IO.File.AppendAllText(fileName, logText);
tryCount = ;
}
catch (Exception ex)
{
if (tryCount > )
{
System.Threading.Thread.Sleep();
logText = logText + "\r\nSaveLog,try again times =" + tryCount + " ,Error:" + ex.Message;
tryCount--;
SaveLog(logFileName, logText, tryCount);
}
else
{
throw new Exception("Save log file Error,try count more times!");
}
}
}

26,ASP.NET获取客户端的IP地址

      string GetRemoteIP()
{
string result = HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Request.ServerVariables["REMOTE_ADDR"];
} if (null == result || result == String.Empty)
{
result = HttpContext.Request.UserHostAddress;
}
return result;

27,ASP.NET MVC 在Action里面获取请求的URL

可以分为3种方式,
1)ASP.NET MVC 在控制器的默认Action里面获取请求其它Action的路径
比如在默认的 Index Action里面获取路径,如下:

string sso_url= "http://" + Request.Url.Authority + Request.Url.AbsolutePath + "/SSO?id=" + userid;

2)在其它Action里面获取当前控制器的路径

string ctrName = RouteData.Values["controller"].ToString();
string redirectUrl = "http://" + Request.Url.Authority + "/" + ctrName + "/SSO?id=" + userid;

3)直接获取当前Action请求的路径

string url=Request.Url.ToString();

28,ASP.NET MVC Action返回可以在浏览器直接查看的纯文本信息

需要指定Context的contentType 为“text/plain”,代码如下:

 public ActionResult SendMessage()
{
string txt="你好!";
return Content(text, "text/plain", System.Text.Encoding.UTF8);
}

29,使用Linq2XML读写XML

这里主要使用XDocument,XElement对象来操作XML内容,如下代码:

    public static class XDocumentExtentsion
{
//生成XML的申明部分
public static string ToStringWithDeclaration(this XDocument doc, SaveOptions options = SaveOptions.DisableFormatting)
{
return doc.Declaration.ToString() + doc.ToString(options);
}
} public string CreateMsgResult(string loginUserId,string corpid, string msg,string ts)
{
var xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("result",
new XElement("corpid", corpid),
new XElement("userid", loginUserId),
new XElement("ts", ts),
new XElement("sendmsg", msg)
));
return xDoc.ToStringWithDeclaration();
} public ResponseMessage ParseXMLString(string xml)
{
var xDoc = XDocument.Parse(xml);
if (xDoc == null) return null;
var root = xDoc.Element("result");
if(root==null)
throw new Exception ("not found the 'result' root node,input XML\r\n"+xml);
ResponseMessage result =
new ResponseMessage()
{
ErrorCode = root.Element("rescode").Value,
ErrorMessage = root.Element("resmsg").Value,
RedirectUrl = root.Element("redirect_url") == null ? "" : root.Element("redirect_url").Value
}; return result;
}

30,访问Web内容的自定义代码

使用 HttpWebRequest和HttpWebResponse 对象完成Web访问,如果是.NET 4.5,建议直接使用 HttpClient对象:

        /// <summary>
/// 获取请求结果
/// </summary>
/// <param name="requestUrl">请求地址</param>
/// <param name="timeout">超时时间(秒)</param>
/// <param name="requestXML">请求xml内容</param>
/// <param name="isPost">是否post提交</param>
/// <param name="encoding">编码格式 例如:utf-8</param>
/// <param name="errorMsg">抛出的错误信息</param>
/// <returns>返回请求结果</returns>
public static string HttpWebRequest(string requestUrl, int timeout, string requestXML, bool isPost, string encoding, out string errorMsg, string contentType = "application/x-www-form-urlencoded")
{
errorMsg = string.Empty;
string result = string.Empty;
try
{
byte[] bytes = System.Text.Encoding.GetEncoding(encoding).GetBytes(requestXML);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Referer = requestUrl;
request.Method = isPost ? "POST" : "GET";
request.Timeout = timeout * ;
if (isPost)
{
request.ContentType = contentType;// "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, , bytes.Length);
requestStream.Close();
}
} HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(encoding));
result = reader.ReadToEnd();
reader.Close();
responseStream.Close();
request.Abort();
response.Close();
return result.Trim();
}
}
catch (Exception ex)
{
errorMsg =string.Format("Error Message:{0},Request Url:{1},StackTrace:{2}", ex.Message ,requestUrl , ex.StackTrace);
} return result;
}

31,自定义浏览器协议(伪协议),实现web程序调用本地程序

(转自 http://blog.sina.com.cn/s/blog_4a77f0630100hav3.html
最近项目遇到这么个问题。客户要求用web页面,点击一个链接,调用本地的一个程序。

参考了一下qq的方式。
tencent://Message/?Uin=000000&websiteName=qzone.qq.com&Menu=yes

在注册表里面添加下面,就能实现,详细内容见原文

32,线程安全的向集合添加元素

有时候,向一个List对象调用Add 方法,会出现“索引超出了数组界限”这样的问题,此时可以考虑使用线程安全的集合,但对于业务上设定了集合的最大值的情况下,用线程安全集合就有点重了,效率不高,此时可以通过 Interlocked.CompareExchange 来实现,具体代码如下:

private int length=;
private int maxLength=;
private int[] Arr=new int[maxLength]; //使用循环数组,安全的添加元素
void Add(int value){
int p= Interlocked.CompareExchange(ref length,,maxLength);
if(p==length)
{
//说明length变量并且没有达到最大值,并安全的返回length当时的值
Arr[p]=value;
}
else
{
//数组元素已经达到上限,需要触发另外的操作,比如将数组全部输出
// To Do
//之后,再将当前位置的元素写入
//此时,length可能是0,也可能是其它值
Arr[length]=value;
}
Interlocked.Increment(ref length);
}

33,WPF绑定异步更新的数据集合

最近做一个WPF项目,后端API推送过来的数据要更新WPF界面的数据,发现有些数据没有跟后端数据状态一致。通常情况下,WPF绑定的Model数据集合都是继承于ObservableCollection 的,但是在当前情况下会有问题,这是可以封装一个异步的数据集合:

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
//获取当前线程的SynchronizationContext对象
private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
public AsyncObservableCollection() { }
public AsyncObservableCollection(IEnumerable<T> list) : base(list) { }
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{ if (SynchronizationContext.Current == _synchronizationContext)
{
//如果操作发生在同一个线程中,不需要进行跨线程执行
RaiseCollectionChanged(e);
}
else
{
//如果不是发生在同一个线程中
//准确说来,这里是在一个非UI线程中,需要进行UI的更新所进行的操作
_synchronizationContext.Post(RaiseCollectionChanged, e);
}
}
private void RaiseCollectionChanged(object param)
{
// 执行
base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param);
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (SynchronizationContext.Current == _synchronizationContext)
{
// Execute the PropertyChanged event on the current thread
RaisePropertyChanged(e);
}
else
{
// Post the PropertyChanged event on the creator thread
_synchronizationContext.Post(RaisePropertyChanged, e);
}
}
private void RaisePropertyChanged(object param)
{
// We are in the creator thread, call the base implementation directly
base.OnPropertyChanged((PropertyChangedEventArgs)param);
}
}

更多信息,请参考:

WPF多线程UI更新——两种方法

绑定到异步的ObservableCollection

常见.NET功能代码汇总 (2)的更多相关文章

  1. 常见.NET功能代码汇总

    1,在Web上修改指定文件位置的Web.config 这里需要使用 WebConfigurationManager 类,但必须使用WebConfigurationFileMap类来指定文件位置,看代码 ...

  2. 常见.NET功能代码汇总 (3)

    33,彻底关闭Excel进程 .NET中使用Excel属于使用非托管资源,使用完成后一般都要用GC回收资源,但是,调用GC的位置不正确,Excel进程可能无法彻底关闭,如下面的代码: static v ...

  3. java开发功能代码汇总

    多文件上传 http://smotive.iteye.com/blog/1903606 java 常用代码 Struts2 前后台(Action ,jsp)传值.取值 Action public Li ...

  4. js 技巧 (六)弹窗代码汇总

    弹窗代码汇总 [0.超完美弹窗代码] 功能:5小时弹一次+背后弹出+自动适应不同分辩率+准全屏显示 代码: <script> function openwin(){ window.open ...

  5. leetcode常见算法与数据结构汇总

    leetcode刷题之后,很多问题老是记忆不深刻,因此特意开此帖: 一.对做过题目的总结: 二.对一些方法精妙未能领会透彻的代码汇总,进行时常学习: 三.总结面试笔试常见题目,并讨论最优解法及各种解法 ...

  6. 原生JS实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  7. Magento Table Rate运费国家代码汇总

    Magento Table Rate是三种内置未调用第三方API运费方式中最强大的一个.通过设置国家,区域,邮编,价格来划分不同的运费等级.该方式基本能够满足轻量级的B2C商城的运费模式.这里收集下国 ...

  8. 通过javascript库JQuery实现页面跳转功能代码

    通过javascript库JQuery实现页面跳转功能代码的四段代码实例如下. 实例1: 1 2 3 4 $(function(){ var pn = $("#gotopagenum&quo ...

  9. 矩阵分解(rank decomposition)文章代码汇总

    矩阵分解(rank decomposition)文章代码汇总 矩阵分解(rank decomposition) 本文收集了现有矩阵分解的几乎所有算法和应用,原文链接:https://sites.goo ...

随机推荐

  1. Android 透明度百分比对应的 十六进制

    Android 透明度百分比对应的 十六进制 先把结果放在这里,方便大家查询,也方便自己,UI太喜欢用百分比表示了=.=! 透明度百分比对应的十六进制: (说明:百分比计算出来会有小数,按照常规的四舍 ...

  2. The currently selected variant "arm-debug" uses split APKs, but none of the 1 split apks are compatible with the current device with density "213" and ABIs "x86".

    出现这种错误一般是在电脑上用模拟器运行APK的吧. 可以在build.gradle中这样配置下: android{ ... defaultConfig { applicationId "XX ...

  3. eCharts动态加载各省份的数据

    假如从数据库读出以下数据,如何将数据展示在地图之上 1.部门的名称数据: List deptname=[联通事业部-上海联通项目组, 联通事业部-河南联通项目组, 联通事业部-贵州联通项目组, 联通事 ...

  4. 使用未付费的账号真机调试 iOS 程序,过几天后程序一打开就会闪退

    使用未付费的苹果开发者账号真机调试 iOS 程序,过几天后程序一打开就会闪退.   解决办法: 删除 Provisioning Profile,重新配置一次. 终极解决办法:花钱购买苹果开发者账号. ...

  5. js的作业题

    <script type="text/javascript"> //var a=3; //switch(a) // { // case 1: // alert(&quo ...

  6. Why Namespace? - 每天5分钟玩转 OpenStack(102)

    上一节我们讨论了 Neutron 将虚拟 router 放置到 namespace 中实现了不同 subnet 之间的路由.今天探讨为什么要用 namespace 封装 router? 回顾一下前面的 ...

  7. angularjs 请求后端接口请求了两次

    用angularjs的过程中发现,每次打开页面,请求后端的接口都请求了两次 如下图可以看到, http://192.168.1.109:8080/zdh/api/v1/goods/54 这个页面loa ...

  8. EntityFramework 分页问题探讨之 OrderBy

    应用场景 最近被应用程序中页面加载慢的问题所折磨,看似容易的问题,其实并不容易(已经持续两天时间了),经过"侦查",发现了两个"嫌疑犯": EntityFram ...

  9. Int,Long比较重使用equal替换==

    首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(参考),超过范围则不行,那么使用equal则代替则完全ok public stati ...

  10. DB2 Enterprise Server Edition(DB2 ESE)9.1在Windows Server 2008 下出现无法新建数据库的情况,及解决办法

    在安装有,DB2 9.1版本的Windows Server 2008 上面,使用默认的安装方式导致无法创建数据库,相关的错误提示: "SQL3012C 发生系统错误(原因码= "& ...