随笔 - 20  文章 - 0  评论 - 6 
  2011年11月19日

一、测试环境

机器A:Windows7 简体中文旗舰版,已安装.net framework2.0和4.0

机器B:Windowsxp 英文专业版,已安装.net framework2.0和4.0

机器C:Windowsxp 简体中文专业版,只安装了.net framework2.0

程序是基于.net framework2.0写的

 

二、测试代码

 

            string result;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                CookieContainer cookieContainer = new CookieContainer();
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.Timeout = 5000;
                Stream responseStream = httpWebRequest.GetResponse().GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
                result = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }
            return result;

 

三、问题

很简单的一段代码,很久以前写的了,这天又拿出来用,却发现不能用了,总是抛异常(请求超时),试了好多不同的url都是一样,还以为是没写UserAgent或者UserAgent写的有问题,抓了个浏览器的包,把浏览器的UserAgent拷出来再试,也不行。把以前可以用的程序直接拿出来跑,也是一样。左思右想,代码一直没人动,变了的是操作系统由以前的window xp变成了现在的windows 7(机器A),立即拿到windows xp虚拟机(机器C)里去跑跑试试,直接从Debug里把文件拷过去,一跑可以用。由于windows xp虚拟机(机器C)只装了.net framework2.0,是不是.net framework4.0影响的呢?第二天直接拿到公司电脑(机器B)也可以用。

不能这么杯具吧?难道windows7就不行了吗?Microsoft不想让大家用HttpWebRequest了?

 

四、解决

经过几天在网上搜索,终于找到了一个和我差不多的问题,并给出了解决方法,不过他说是.net framework4.0,而我的是在windows7上就出问题。

最终根据给出的解决方法分析,可能是和系统不同的“默认代理和用户验证策略”有关。大家有知道准确原因的请指教。

解决方法如下:在app.config里加入以下代码,原来的代码C#代码无需修改

 

 <system.net>   
    <defaultProxy   
        
enabled="false"   
        useDefaultCredentials
="false" >   
      <proxy/>   
      <bypasslist/>   
      <module/>   
    </defaultProxy>   
  </system.net> 

 

五、参考

http://msdn2.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx

 

posted @ 2011-11-19 15:28 初始小花 阅读(59) 评论(0) 编辑
  2011年10月3日

IIS7.5中(仅win7,win2008 SP2,win2008 R2支持),应用程序池的运行帐号,除了指定为LocalService,LocalSystem,NetWorkService这三种基本类型外,还新增了一种ApplicationPoolIdentify 

 

win7的官方帮助上是这么说的:

ApplicationPoolIdentity – 默认情况下,选择“应用程序池标识”帐户。启动应用程序池时动态创建“应用程序池标识”帐户,因此,此帐户对于您的应用程序来说是最安全的。

也就是说"ApplicationPoolIdentity"帐号是系统动态创建的“虚拟”帐号(说它是虚拟的,是因为在用户管理里看不到该用户或用户组,在命令行下输入net user也无法显示,但该帐号又是确实存在的) 

如何验证该帐号确实是存在的的?打开任务管理器,观察一下:

w3wp.exe即iis进程,上图中高亮部分表明该iis进程正在以帐号luckty运行(注意这里的luckty即为上图中的应用程序池名称)


好了,搞清楚这个有什么用?

先来做一个测试,比如我们在iis里新建一个站点,主目录设置为c:\2\,应用程序池就指定刚才图中的luckty

假如我们在该站点的default.aspx.cs里写入这样一行代码 :

File.AppendAllText("C:\\TestDir\\1.txt",DateTime.Now.ToString());

前提是c盘必须先建一个目录TestDir,同时除Administrator,System保留完全控制权外,其它帐号的权限都删除掉

运行后,会提示异常: 对路径“C:\TestDir\1.txt”的访问被拒绝。

原因很明显:该站点运行时是以应用程序池(luckty)对应的虚拟帐号运行的,而这个虚拟帐号不具备c:\TestDir的访问权限

这种情况在web服务器(iis6)安全配置中很常见,比如我们把图片上传目录,常常放在主目录之外,同时以虚拟目录形式挂于站点之下,另外在IIS6中不指定该目录任何执行权限 ,这样即使有人非法上传了asp/aspx木马上去,也无法运行搞不成破坏!

言归正传,要想让那一行测试代码正常运行,解决办法很简单,把虚拟帐号的权限加入文件夹安全权限中即可,但是问题来了:这个虚拟帐号我们是不可见的,如果你直接添加名为luckty的用户到文件夹安全帐号里,根本通不过(提示找不到luckty用户),说明这个虚拟帐号名称并不是"luckty"


关键:手动输入 IIS AppPool\luckty (即IIS AppPool\应用程序池名),再确定,这回ok了.


 

当然除了用"IIS AppPool\应用程序池名"外,windows内部还有一个特殊的用户组Authenticated Users,把这个组加入TestDir的安全权限帐号里也可以,不过个人觉得没有"IIS AppPool\应用程序池名"来得精确.


结束语:
IIS7.5的虚拟帐号设计确实很棒,想想传统IIS6的时候,为了把同一服务器上的各站点权限分开(以防止木马捣乱),不得不创建一堆iuser_XXX,iwam_XXX帐号并指定密码,再一个个站点分配过去,累死人!而虚拟帐号设计则让这类管理轻松多了,也不用担心密码过于简单或过期问题。So,还在等什么,赶紧升级到win7/win2008 R2吧!

原文地址:http://www.cnblogs.com/yjmyzz/archive/2009/10/26/1590033.html

posted @ 2011-10-03 11:33 初始小花 阅读(88) 评论(0) 编辑
  2011年8月30日

一个项目中需要用到图片无缝滚动,拿出几年前用过的一用结果不能用了,网上一顿搜索结果没找到太好用的,要不就是要写很多html和js,要不就是对滚动的内容有严格的要求,只好参考着改善了一下,没有做太详细的测试,不过在自己的项目里使用没有什么问题,记下来以备后用,有什么新的需要再扩展吧。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="Js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(
function()
{
    $(
"#imageMarquee").marquee();
});
    
(
function($) {
    $.fn.marquee 
= function(o) {
        o 
= $.extend({
            speed:        parseInt($(
this).attr('speed')) || 30// 滚动速度
            step:        parseInt($(this).attr('step')) || 1// 滚动步长
            direction:    $(this).attr('direction'|| 'left' // 滚动方向
        }, o || {});
        
var dIndex = jQuery.inArray(o.direction, ['right''down']);
        
if (dIndex > -1) {
            o.direction 
= ['left''up'][dIndex];
            o.step 
= -o.step;
        }
        
var mid;
        
var e = $(this);
        e.wrap(
"<div class='wrap'><ul><li></li></ul></div>");
        
var p = e.parent().parent().parent();
        //alert(e.width());
        p.find(
'ul>li:first').after(p.find('ul>li:first').clone());
        p.find(
'ul:first').css("listStyle","none");
        
if(o.direction == 'left')
        {
            p.find(
'ul>li').css("float","left");
            p.find(
'ul:first').width(e.width() * 2);
        }
        
else
        {
            p.find(
'ul:first').height(e.height() * 2);
        }
        mid 
= setInterval(_marquee, o.speed);
        p.hover(
            
function(){clearInterval(mid);},
            
function(){mid = setInterval(_marquee, o.speed);}
        );
        
function _marquee() {
            
if (o.direction == 'left') {
                
if(p.scrollLeft() >= e.width()){
                    p.scrollLeft(
0);
                }
                
else{
                    p.scrollLeft(p.scrollLeft() 
+ o.step);
                }
            }
            
else{
                
if(p.scrollTop() >= e.height()){
                    p.scrollTop(
0);
                }
                
else{
                    p.scrollTop(p.scrollTop() 
+ o.step);
                }
            }
        }
    };
})(jQuery);
</script>
<style type="text/css">
.wrap
{ width:800px; height:180px; overflow:hidden;}
#imageMarquee td 
{ padding:5px; }
#imageMarquee img 
{border:solid 1px #999999;}
</style>
</head>

<body>
<table width="500" border="0" cellspacing="0" cellpadding="0" id="imageMarquee">
  
<tr>
    
<td><img src="Images/1.jpg" width="145" height="160" /></td>
    
<td><img src="Images/2.jpg" width="145" height="160" /></td>
    
<td><img src="Images/3.jpg" width="145" height="160" /></td>
    
<td><img src="Images/4.jpg" width="145" height="160" /></td>
    
<td><img src="Images/5.jpg" width="145" height="160" /></td>
  
</tr>
</table>
</body>
</html>

 

posted @ 2011-08-30 17:14 初始小花 阅读(55) 评论(0) 编辑
  2011年8月12日
一.习题
http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html

二.参考
http://msdn.microsoft.com/zh-cn/library/system.linq.expressions.expression.aspx
http://msdn.microsoft.com/zh-cn/library/bb361179%28v=VS.100%29.aspx

三.思路
先写出lambda,参照lambda拼出Expression

四.做题
(1).-a

Expression<Func<intint>> lambdaexpression = a => -a;

ParameterExpression p1 
= Expression.Parameter(typeof(int), "a");
Expression body 
= Expression.Negate(p1);
LambdaExpression expression 
= Expression.Lambda(body, p1);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.Negate方法返回类型是UnaryExpression,一元运算表达式

(2).a + b * 2

Expression<Func<intintint>> lambdaexpression = (a, b) => a + b * 2;

ParameterExpression p1 
= Expression.Parameter(typeof(int), "a");
ParameterExpression p2 
= Expression.Parameter(typeof(int), "b");
Expression body 
= Expression.Add(p1, Expression.Multiply(p2, Expression.Constant(2)));
LambdaExpression expression 
= Expression.Lambda(body, p1, p2);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.Add和Expression.Multiply都返回BinaryExpression,二元运算表达式,
Expression.Constant返回ConstantExpression,常量表达式

(3).Math.Sin(x) + Math.Cos(y)

Expression<Func<doubledoubledouble>> lambdaexpression = (x, y) => Math.Sin(x) + Math.Cos(y);

ParameterExpression p1 
= Expression.Parameter(typeof(double), "x");
ParameterExpression p2 
= Expression.Parameter(typeof(double), "y");
Expression body 
= Expression.Add(
    Expression.Call(
nulltypeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), p1)
    , Expression.Call(
nulltypeof(Math).GetMethod("Cos", BindingFlags.Static | BindingFlags.Public), p2));
LambdaExpression expression 
= Expression.Lambda(body, p1, p2);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.Call返回MethodCallExpression,方法调用表达式

(4).new StringBuilder(“Hello”)

Expression<Func<StringBuilder>> lambdaexpression = () => new StringBuilder("Hello");

Expression body 
= Expression.New(typeof(StringBuilder).GetConstructor(new Type[] { typeof(string) }),
    Expression.Constant(
"Hello"));
LambdaExpression expression 
= Expression.Lambda(body);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.New返回NewExpression,构造函数调用表达式

(5).new int[] { a, b, a + b}

Expression<Func<intintint[]>> lambdaexpression = (a, b) => new int[] { a, b, a + b };

ParameterExpression p1 
= Expression.Parameter(typeof(int), "a");
ParameterExpression p2 
= Expression.Parameter(typeof(int), "b");
Expression body 
= Expression.NewArrayInit(typeof(int), p1, p2, Expression.Add(p1, p2));
LambdaExpression expression 
= Expression.Lambda(body, p1, p2);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.NewArrayInit返回NewArrayExpression,创建数组表达式

(6).a[i – 1] * i

Expression<Func<int[], intint>> lambdaexpression = (a, i) => a[i - 1* i;

ParameterExpression p1 
= Expression.Parameter(typeof(int[]), "a");
ParameterExpression p2 
= Expression.Parameter(typeof(int), "i");
Expression body 
= Expression.Multiply(Expression.ArrayIndex(p1, Expression.Subtract(p2, Expression.Constant(1))), p2);
LambdaExpression expression 
= Expression.Lambda(body, p1, p2);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.ArrayIndex返回也是BinaryExpression,这个有点意外,我以为会是MethodCallExpression呢

(7).a.Length > b | b >= 0

Expression<Func<int[], intbool>> lambdaexpression = (a, b) => a.Length > b | b >= 0;

ParameterExpression p1 
= Expression.Parameter(typeof(int[]), "a");
ParameterExpression p2 
= Expression.Parameter(typeof(int), "b");
Expression body 
= Expression.Or(Expression.GreaterThan(Expression.ArrayLength(p1), p2),
    Expression.GreaterThanOrEqual(p2, Expression.Constant(
0)));
LambdaExpression expression 
= Expression.Lambda(body, p1, p2);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.GreaterThan、Expression.Or和Expression.ArrayLength都返回BinaryExpression,二元运算表达式,
Expression.ArrayLength和(6)中一样误解了

(8).(高难度)new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) }

Expression<Func<double, System.Windows.Point>> lambdaexpression = a => new System.Windows.Point() { X = Math.Sin(a), Y = Math.Cos(a) };

ParameterExpression p1 
= Expression.Parameter(typeof(double), "a");
Expression body 
= Expression.MemberInit(Expression.New(typeof(System.Windows.Point))
    , 
new MemberBinding[] {
        Expression.Bind(
typeof(System.Windows.Point).GetProperty("X"),
        Expression.Call(
nulltypeof(Math).GetMethod("Sin", BindingFlags.Static | BindingFlags.Public), p1)),
        Expression.Bind(
typeof(System.Windows.Point).GetProperty("Y"),
        Expression.Call(
nulltypeof(Math).GetMethod("Cos", BindingFlags.Static | BindingFlags.Public), p1))
    });
LambdaExpression expression 
= Expression.Lambda(body, p1);

Console.WriteLine(lambdaexpression.ToString());
Console.WriteLine(expression.ToString());

Expression.MemberInit返回MemberInitExpression,如方法名字一样是成员初始化表达式。
果然像题目所写,难度不低,一开始以为使用Expression.New后面的参数,后来还是看了评论才会写。

五.执行结果

a => -a
a => -a

(a, b) => (a + (b * 2))
(a, b) => (a + (b * 2))

(x, y) => (Sin(x) + Cos(y))
(x, y) => (Sin(x) + Cos(y))

() => new StringBuilder("Hello")
() => new StringBuilder("Hello")

(a, b) => new [] {a, b, (a + b)}
(a, b) => new [] {a, b, (a + b)}

(a, i) => (a[(i - 1)] * i)
(a, i) => (a[(i - 1)] * i)

(a, b) => ((ArrayLength(a) > b) Or (b >= 0))
(a, b) => ((ArrayLength(a) > b) Or (b >= 0))

a => new Point() {X = Sin(a), Y = Cos(a)}
a => new Point() {X = Sin(a), Y = Cos(a)}
Press any key to continue . . .

作完这些习题,感觉让我这个Expression新手真的进步了不少
posted @ 2011-08-12 15:58 初始小花 阅读(80) 评论(0) 编辑
  2011年7月27日
<object name="logExceptionHandler" type="Spring.Aspects.Exceptions.LogExceptionHandler, Spring.Aop">
    
<property name="LogName" value="BTSS.ExceptionLogger" />
    
<property name="LogLevel" value="Debug"/>
  
</object>

  
<object name="exceptionAdvice" type="Spring.Aspects.Exceptions.ExceptionHandlerAdvice, Spring.Aop">
    
<property name="ExceptionHandlerDictionary">
      
<dictionary>
        
<entry key="log" value-ref="logExceptionHandler"/>
      
</dictionary>
    
</property>
    
<property name="exceptionHandlers">
      
<list>
        
<value>on exception (#e is T(System.Exception)) log 'Logging an exception thrown from method ' + #method.Name </value>
      
</list>
    
</property>
  
</object>
<object id="txAttributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop">
    
<property name="Attribute" value="Spring.Transaction.Interceptor.TransactionAttribute, Spring.Data"/>
  
</object>

  
<aop:config>

    
<aop:advisor id="exceptionProcessAdvisor" order="1"
               advice-ref
="exceptionAdvice"
               pointcut-ref
="txAttributePointcut"/>
  
</aop:config>
posted @ 2011-07-27 11:16 初始小花 阅读(656) 评论(0) 编辑
  2011年3月24日
摘要: 一. .Net 中 获取当前应用程序启动目录的几个方法AppDomain.CurrentDomain.BaseDirectory代表的是程序集所在的目录,它具有读取和写入的属性。AppDomainSetup.ApplicationBase代表包含应用程序的目录位置,它具有读取和写入的属性。Environment.CurrentDirectory获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。但是用了组件里面的打开,保存,选择路径,就出现了问题。后来进过尝试发现,打开和保存会修改当前程序目录,也就是修改Environment.CurrentDirectory的值,但是folderBr阅读全文
posted @ 2011-03-24 11:04 初始小花 阅读(368) 评论(0) 编辑
  2011年2月7日
摘要: 一.安装失败时要卸载干净 例如: SQL Server 2005 SQL Server 2005 Analysis Services SQL Server 2005 Reporting Services SQL Server 2005 Notification Services SQL Server 2005 Integration Services SQL Server 2005 Tools SQL Server 2005 联机图书 MSXML 6.0 Parser SQLXML4 SQL Server VSS Writer SQL Server 2005 Backward Compatib阅读全文
posted @ 2011-02-07 22:58 初始小花 阅读(1419) 评论(0) 编辑
  2011年1月30日
摘要: 1.Orchard.Web.MvcApplication,Orchard.Web[代码]2.Orchard.Environment.OrchardStarter,Orchard.Framework[代码]3.Orchard.Environment.ShellBuilders.ShellContainerFactory,Orchard.Framework[代码]4.来自Orchard官方网站Documentation中的说明Dependency injectionThe standard way of creating injectable dependencies in Orchard is 阅读全文
posted @ 2011-01-30 16:31 初始小花 阅读(719) 评论(0) 编辑
  2010年10月13日
摘要: [代码]发现:看到CodeSmith里ScriptTableData生成的脚本,呵呵,INSERT 原来可以这样,基础知识没学好啊。说明:PersonId 是标示(IDENTITY)字段用途:项目数据的初始化,尤其是现有项目增加功能的时候。阅读全文
posted @ 2010-10-13 10:52 初始小花 阅读(284) 评论(0) 编辑
  2010年6月2日
摘要: 当成员可能被同名的其他实体隐藏时,能够访问全局命名空间中的成员非常有用。例如,在下面的代码中,Console 在 System 命名空间中解析为 TestApp.Console 而不是 Console 类型。由于类 TestApp.System 隐藏了 System 命名空间,因此使用 System.Console 仍然会导致错误:但是,可以通过使用 global::System.Console ...阅读全文
posted @ 2010-06-02 11:38 初始小花 阅读(91) 评论(0) 编辑