Thursday, October 28, 2010

10/28: Using Virtual Directory Points To A Remote Share Error In IIS

 

When you use a virtual directory that points to a remote share to host a Microsoft ASP.NET-based application,  may receive an error message that is similar to one of the following:

System.Web.AspNetHostingPermission failed

From

 

http://support.microsoft.com/?id=320268

 

Solution :

(1) Run the following command:

 

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -m -ag 1 -url "\\colstf01\Multisite_Sync\Configuration\*" FullTrust -exclusive on

 

(2) Reset IIS

Wednesday, October 27, 2010

C#三种模拟自动登录和提交POST信息的实现方法(ZT)

 

网页自动登录(提交Post内容)的用途很多,如验证身份、程序升级、网络投票等,以下是用C#实现的方法。
网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest这三个。以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:
HtmlElement ClickBtn =null;
if (e.Url.ToString().ToLower().IndexOf("xxx.htm") > 0) //登陆页面
{
HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count ; i++)
{
if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch (doc.All[i].Name)
{
case "userCtl":
doc.All[i].InnerText = "user01";
break;
case "passCt1":
doc.All[i].InnerText = "mypass";
break;
case "B1":
ClickBtn = doc.All[i]; //提交按钮
break;
}
}
}
ClickBtn.InvokeMember("Click"); //执行按扭操作
}
2、WebClient封装了HTTP的一些类,操作简单,相较于webBrowser,特点是可以自设代理,缺点是对COOKIE的控制
WebClient的运行全在后台,并且提供了异步操作的能力,这样很方便并发多个任务,然后等待结果的返回,再逐个处理。多任务异步调用的代码如下:
private void StartLoop(int ProxyNum)
{
WebClient [] wcArray = new WebClient[ProxyNum]; //初始化
for (int idArray = 0; idArray< ProxyNum;idArray++)
{
wcArray[idArray] = new WebClient();
wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
try
{
......
wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
wcArray[idArray].OpenReadAsync(new Uri("http://xxxx.com.cn/tp.asp?Id=129")); //打开WEB;
proxy = null;
}
catch
{
}
}
}
private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //取返回信息
.....
String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded");
((WebClient)sender).Headers.Add("Accept-Language", "zh-cn");
((WebClient)sender).Headers.Add("Cookie", cookie);
string postData = "......"
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成二进制数组
((WebClient)sender).UploadDataAsync(new Uri("http://xxxxxxy.com.cn/tp.asp?Id=129"), "POST", byteArray);
}
}
private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
{
if (e.Error == null)
{
string returnMessage = Encoding.Default.GetString(e.Result);
......
}
}
3、HttpWebRequest较为低层,能实现的功能较多,Cookie操作也很简单
private bool PostWebRequest()
{
CookieContainer cc = new CookieContainer();
string pos tData = "user=" + strUser + "&pass=" + strPsd;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("http://www.xxxx.com/chk.asp"));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
......
}
HttpWebRequest同样提供了异步操作,有兴趣的朋友自己查MSDN,实现起来也不难。

Thursday, October 14, 2010

10/14/2010: GACUtil Batch Deployment

  • Get the folder of all bins
  • run the following command:

for %1 in (*.dll) do gacutil.exe –i %1

Tuesday, October 12, 2010

10/12/2010: SP Development Life Cycle With VSEWSS

image

10/12/2010: VSeWSS 1.3 Post-Install Configuration (ZT)

http://blogs.msdn.com/b/steve_fox/archive/2009/03/18/vsewss-1-3-post-install-configuration.aspx

In my environment ,  I can not add network service as administrator since domain controller installed

So in step 3  , I use administrator instead of network service with password in identity of SharePoint Central Administration v.3 application pool .

Friday, October 08, 2010

1008: Features In SP2007

  • Features Location :  …\web server extensions\12\Template\features
  • Features Key files
    • Feature.xml
    • Elelment files : stored in <ElementManifests> section of features.xml
    • Resource Files : …\web server extensions\12\Resources
  • Feature Scope:
    • web
    • site
    • webapplication
    • farm
  • Element scoping:
    • Not all elements are available at all scope levels
  • How to create feature in VS
    • Create a class library with sharepont.dll reference
    • Create a feature xml (associating schema to feature)
    • Create element files and add them to feature.xml
    • Copy the whole folder in feature directory(…\web server extensions\12\Template\features)
    • run command line: stsadm.exe –o installfeature –name sdy
    • In share point site, site setting-> site features-> active
  • Feature deployment : install and activate
    • Install feature
      • stsadm.exe command line
      • sharepoint object model
    • Activating feature
      • stsadm.exe
      • sharepoint object model
      • sharepoint site definition(one.xml)
      • admin user interface
    • Another way to deploy multiple  features by using wsp
      • Create manifest.xml
      • create a ddf file
      • using Makecab.ext with manifest.xml and .ddl to generate .cab and .wsp file
      • run stsadm.exe –o addsolution –filename *.wsp
      • on sp admin site , operation->select wsp file-> deploy

Wednesday, October 06, 2010

10/06: Factory Patterns of DP

  • Simple factory pattern
    • the instantiation logic will be written in factory method
      • ….
      •             if (!String.IsNullOrEmpty(fruitType))
                    {
                        switch (fruitType)
                        {
                            case "Apple":
                                return new Apple();
                            case "Orange":
                                return new Orange();
                            case "Pear":
                                return new Pear();
                            case "Banana":
                                return new Banana();
                        }
                    }

    • Against Open-Close rule when adding new instance which need to modify above code
      •  

         

  • Factory Pattern
    • the instantiation logic will be written in client application
      • ….
      •    //首先要实例化一个苹果工厂
                    factory = new AppleFactory();
             //然后通过工厂来获得苹果类对象
                    fruit = factory.GetFruit();
           //访问苹果类对象
        fruit.Display();

          //首先要实例化一个梨子工厂
                    factory = new PearFactory();
        //然后通过工厂来获得梨子类对象
                    fruit = factory.GetFruit();
        //访问梨子类对象
                    fruit.Display();

        工厂方法将这些本来应该存在的判断逻辑移到了客户端代码中,

        (通过客户的需求来选择生成那一种水果实例,比如我的演示中生成的就是苹果和梨子这两种实例

    • Not against open-close rule when adding new instance.
  • Abstract Factory Pattern.
    • Abstract factory pattern is very similar as factory pattern.
    • Difference :
      • Factory pattern is better used for adding a new object under same family( like add a new peachfactory item to fruitfacotry family)
      • Abstract pattern is better used for adding a new brand new family which share similarly with existing family(like database operation component for Oracle , Sqlserver , DB2, SAP, etc).
      • Abstract factory return factory object while factory return its subclass object.(so abstract factory is one level higher than factory )