beyondjay

 

2008年3月25日

将windows services制作成MSI安装程序

1、创建新的“安装项目”项目;
2、在解决方案资源管理器中添加相应的所有项目文件(在项目上右键“添加”—>“文件”);
3、在解决方案资源管理器上方 点击“自定义操作编辑器”;
4、在“自定义操作”视图中,点击“安装”,右击“添加自定义操作”,在弹出窗口“选择项目中的项”中选择对应的windows service所在的EXE文件,点击确定;
5、在“安装”下点击上一步选择的windows service程序文件,在属性窗口中设置参数(Arguments)为 “/install” ;(这一步可让安装程序MSI自动安装好Windows service)
6、同样在“卸装”项中添加windows service所在的EXE文件,并在属性窗口中设置参数(Arguments)为 “/uninstall” ;(这一步可以在“添加/删除程序”时自动卸装Windows service)
7、生成项目文件就OK了。 


o build your service project

  1. In Solution Explorer, right-click your project and select Properties from the shortcut menu. The project's Property Pages dialog box appears.
  2. In the left pane, select the General tab in the Common Properties folder.
  3. From the Startup object list, choose MyNewService. Click OK.
  4. Press Ctrl+Shift+B to build the project. 

Service Project Property Page

Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project, you will need to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed.

To create a setup project for your service

  1. On the File menu, point to Add Project, and then choose New Project.
  2. In the Project Types pane, select the Setup and Deployment Projects folder.
  3. In the Templates pane, select Setup Project. Name the project MyServiceSetup.

A setup project is added to the solution. Next you will add the output from the Windows service project, MyNewService.exe, to the setup.

Service Setup Project

To add MyNewService.exe to the setup project

  1. In Solution Explorer, right-click MyServiceSetup, point to Add, then choose Project Output. The Add Project Output Group dialog box appears.
  2. MyNewService is selected in the Project box.
  3. From the list box, select Primary Output, and click OK.

    A project item for the primary output of MyNewService is added to the setup project. Now add a custom action to install the MyNewService.exe file.

To add a custom action to the setup project

  1. In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears.
  2. In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action. The Select Item in Project dialog box appears.
  3. Double-click the application folder in the list box to open it, select primary output from MyNewService (Active), and click OK. The primary output is added to all four nodes of the custom actions — Install, Commit, Rollback, and Uninstall.
  4. Build the setup project.

posted @ 2008-07-09 10:23 Tony Zhou 阅读(15) | 评论 (0)编辑

生成repeater分页的页码,可以想datagrid一样分页。需要配合PagedDataSource 。

生成repeater分页的页码,可以想datagrid一样点击。需要配合PagedDataSource 。

public string CreatePagerLinks(PagedDataSource objPds, string BaseUrl)
  {
   int totalPageRanger = 3;//set page range is 5, means display five hyperlink for page selection
   int pageRanger = (totalPageRanger-1)/2;

   StringBuilder sbPager = new StringBuilder();

   if (!objPds.IsFirstPage)
   {
    // first page link
    sbPager.Append("<a href=\"");
    sbPager.Append(BaseUrl);
    sbPager.Append("\">|<</a> ");
    if (objPds.CurrentPageIndex != 1)
    {
     // previous page link
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(objPds.CurrentPageIndex.ToString());
     sbPager.Append("\" alt=\"Previous Page\"><<</a>  ");
    }
   }


   int intLow = objPds.CurrentPageIndex + 1 - pageRanger;//pageindex + 1 = pagenumber
   int intHigh = objPds.CurrentPageIndex + 1 + pageRanger;//pageindex + 1 = pagenumber

   if (intLow < 1) intLow = 1;
   if (intHigh > objPds.PageCount) intHigh = objPds.PageCount;

   if (intHigh - intLow < totalPageRanger)
   {
    intHigh = intLow + totalPageRanger - 1;
    if (intHigh > objPds.PageCount)
     intHigh = objPds.PageCount;
   }
   if (intHigh - intLow < totalPageRanger)
   {
    intLow = intHigh - totalPageRanger + 1;
    if (intLow < 1)
     intLow = 1;
   }

   for (int x = intLow; x <= intHigh ; x++)
   {
    // numeric links
    if (x == objPds.CurrentPageIndex + 1) sbPager.Append(x.ToString() + "  ");
    else
    {
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(x.ToString());
     sbPager.Append("\">");
     sbPager.Append(x.ToString());
     sbPager.Append("</a>  " );
    }
   }
   if (!objPds.IsLastPage)
   {
    if ((objPds.CurrentPageIndex + 2) != objPds.PageCount)
    {
     // next page link
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(Convert.ToString(objPds.CurrentPageIndex + 2));
     sbPager.Append("\">>></a>  ");
    }
    // last page link
    sbPager.Append("<a href=\"");
    sbPager.Append(BaseUrl);
    sbPager.Append("&page=");
    sbPager.Append(objPds.PageCount.ToString());
    sbPager.Append("\">>|</a>");
   }
   // conver the final links to a string and assign to labels
   return sbPager.ToString();
  }


lilPage是一个literal,直接调用上面那个方法赋值就可以。

private void BindRepeater()
  {
   DataSet ds = new DataSet();

    ds = ......

   DataTable dt = ds.Tables[0];
   
   foreach (DataRow dr in dt.Rows)
   {
    if (dr["RewardImageUrl"]==null||dr["RewardImageUrl"].ToString().Length ==0)
     dr["RewardImageUrl"] = ConfigVar.Get("NO_PIC").ToString();
   }

   PagedDataSource pagedDataSource = new PagedDataSource();
   pagedDataSource.DataSource = ds.Tables[0].DefaultView;
   pagedDataSource.AllowPaging = true;
   pagedDataSource.PageSize = int.Parse(giftPageSize);

   //start paging
   if (GetPage() > -1)
   {
    // subtract 1 because the PagedDataSource uses a zero-based index
    int pageIndex = GetPage() - 1 ;
    // correct for a page index less than 0 or greater than the last page
    if (pageIndex < 0) pageIndex = 0;
    if (pageIndex > pagedDataSource.PageCount)
     pageIndex = pagedDataSource.PageCount - 1;
    pagedDataSource.CurrentPageIndex = pageIndex;
   }
   this.RepGiftList.DataSource = pagedDataSource;
   this.RepGiftList.DataBind();

   lilPage.Text = CreatePagerLinks(pagedDataSource, GetAbsolutUri());
  }

posted @ 2008-06-17 17:04 Tony Zhou 阅读(21) | 评论 (0)编辑

2008.6.18

    只有注册用户登录后才能阅读该文。阅读全文

posted @ 2008-06-16 17:45 Tony Zhou 阅读(1) | 评论 (0)编辑

在看ajax in action,看不懂他介绍模式的几章,头昏阿。还是例子好懂些。

在看ajax in action,看不懂他介绍模式的几章,头昏阿。还是例子好懂些。

posted @ 2008-06-12 16:49 Tony Zhou 阅读(15) | 评论 (0)编辑

用httpmodule自动加密解密query string查询字符串

项目中经常遇到query string的加密解密问题,如果在页面或者page base里面处理的话总有点不爽。
所以最近尝试着用httpmodule解决。在网上看到了几篇文章,综合了一下,解决了postback加密过的url丢失的问题。
HttpModule:
#region Using

using System;
using System.IO;
using System.Web;
using System.Text;
using System.Security.Cryptography;

#endregion

/// <summary>
/// Summary description for QueryStringModule
/// </summary>
public class QueryStringModule : IHttpModule
{

    #region IHttpModule Members

    public void Dispose()
    {
        // Nothing to dispose
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    #endregion

    private const string PARAMETER_NAME = "enc=";
    private const string ENCRYPTION_KEY = "key";

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.Request.Url.OriginalString.Contains("aspx") && context.Request.RawUrl.Contains("?"))
        {
            string query = ExtractQuery(context.Request.Url.ToString());
            string path = GetVirtualPath();

            if (query.StartsWith(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase))
            {
                // Decrypts the query string and rewrites the path.
                context.Items["OriginalUrl"] = context.Request.Url.ToString();
                string rawQuery = query.Replace(PARAMETER_NAME, string.Empty);
                string decryptedQuery = Decrypt(rawQuery);
                context.RewritePath(path, string.Empty, decryptedQuery);
            }
            else if (context.Request.HttpMethod == "GET")
            {
                // Encrypt the query string and redirects to the encrypted URL.
                // Remove if you don't want all query strings to be encrypted automatically.
                string encryptedQuery = Encrypt(query);
                context.Items["OriginalUrl"] = path + encryptedQuery;
                context.Response.Redirect(path + encryptedQuery);
            }
        }
    }

    /// <summary>
    /// Parses the current URL and extracts the virtual path without query string.
    /// </summary>
    /// <returns>The virtual path of the current URL.</returns>
    private static string GetVirtualPath()
    {
        string path = HttpContext.Current.Request.RawUrl;
        path = path.Substring(0, path.IndexOf("?"));
        path = path.Substring(path.LastIndexOf("/") + 1);
        return path;
    }

    /// <summary>
    /// Parses a URL and returns the query string.
    /// </summary>
    /// <param name="url">The URL to parse.</param>
    /// <returns>The query string without the question mark.</returns>
    private static string ExtractQuery(string url)
    {
        int index = url.IndexOf("?") + 1;
        return url.Substring(index);
    }

    #region Encryption/decryption

    /// <summary>
    /// The salt value used to strengthen the encryption.
    /// </summary>
    private readonly static byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());

    /// <summary>
    /// Encrypts any string using the Rijndael algorithm.
    /// </summary>
    /// <param name="inputText">The string to encrypt.</param>
    /// <returns>A Base64 encrypted string.</returns>
    public static string Encrypt(string inputText)
    {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] plainText = Encoding.Unicode.GetBytes(inputText);
        PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainText, 0, plainText.Length);
                    cryptoStream.FlushFinalBlock();
                    return "?" + PARAMETER_NAME + Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
    }

    /// <summary>
    /// Decrypts a previously encrypted string.
    /// </summary>
    /// <param name="inputText">The encrypted string to decrypt.</param>
    /// <returns>A decrypted string.</returns>
    public static string Decrypt(string inputText)
    {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] encryptedData = Convert.FromBase64String(inputText);
        PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
        {
            using (MemoryStream memoryStream = new MemoryStream(encryptedData))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                {
                    byte[] plainText = new byte[encryptedData.Length];
                    int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                    return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                }
            }
        }
    }

    #endregion

}


PageBase:需要继承一下这个pagebase
public class PageBase:Page
    {

        protected override void OnLoadComplete(EventArgs e)
        {
            string originalUrl = Context.Items["OriginalUrl"] as string;

            if (!string.IsNullOrEmpty(originalUrl)) //So this page have been urlrewriten, after the page onloaded, rewrite the url of this page to original url
            {
                string query = string.Empty;
                int pos = originalUrl.IndexOf('?');
                if (pos >= 0)// check if has query paramet
                    query = originalUrl.Substring(pos + 1);
                //                    originalUrl = originalUrl.Substring(0, pos);
                Context.RewritePath(QueryStringModule.GetVirtualPath(), string.Empty, query);
            }
            base.OnLoadComplete(e);
        }
    }


参考
http://msdn.microsoft.com/en-us/library/ms972974.aspx
http://www.webpronews.com/expertarticles/2007/01/25/aspnet-httpmodule-for-query-string-encryption

posted @ 2008-06-06 17:46 Tony Zhou 阅读(34) | 评论 (0)编辑

NETWORK SERVICE错误

启动时出现一个矩形框:

“至少一个服务或应用程序错误”

解决的方法:

1. (点击打开)“开始”\“管理工具”\“事件查看器”\(“应用程序”/“安全”/“系统”中寻找并右击错误行)\“事件详细信息——描述”,(如显示:“来源:DCOM    事件:10016   用户:NETWORK SERVICE 应用程序-特定 权限设置未将COM 服务器应用程序(CLSID 为{BA126AD1-2166-11D1-B1D0-00805FC1270E})的本地激活权限授予用户 NT AUTHORITY\NETWORK SERVICE SID (S- 1-5-20)。”,则明白这是错误所在)\(点击关闭。)

  

      2. (点击打开)“开始”/“控制面板”/“管理工具”/“组件服务”。

    

     3. 打开树目录“计算机”/“我的电脑”/“DCOM配置”/右击“netman” / “属性”/“安全”/“启动和激活权限” /“自定义”-“编辑”/“添加-‘ NETWORK SERVICE ’” /“确定”/“‘本地启动-允许’;‘本地激活-允许’” /“确定”/“应用”/“确定”(完成)。

 

posted @ 2008-04-26 16:12 Tony Zhou 阅读(32) | 评论 (0)编辑

Command模式

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Design_Pattern_Command : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Controller controller = new Controller();

        Hello hello = new Hello();
        HelloCommand helloCommand = new HelloCommand(hello);
        controller.SetCommand(0, helloCommand);

        Bye bye = new Bye();
        ByeCommand byeCommand = new ByeCommand(bye);
        controller.SetCommand(1, byeCommand);

        Good good = new Good();
        GoodCommand goodCommand = new GoodCommand(good);
        controller.SetCommand(2, goodCommand);

        controller.OnPush(2);
        controller.OnPush(0);
        controller.OnPush(1);

    }
}

public class Hello
{
    public void SayHello()
    {
        System.Web.HttpContext.Current.Response.Write("Hello!");
    }
}

public class Bye
{
    public void SayBye()
    {
        System.Web.HttpContext.Current.Response.Write("Bye!");
    }
}

public class Good
{
    public void SayGood()
    {
        System.Web.HttpContext.Current.Response.Write("Good!");
    }
}

public interface ICommand
{
    void Execute();
}

public class HelloCommand : ICommand
{
    private Hello _hello;

    public HelloCommand(Hello hello)
    {
        this._hello = hello;
    }

    public void Execute()
    {
        this._hello.SayHello();
    }
}

public class ByeCommand : ICommand
{
     private Bye _bye;

     public ByeCommand(Bye bye)
     {
        this._bye = bye;
     }

    public void Execute()
    {
        this._bye.SayBye();
    }
}

public class GoodCommand : ICommand
{
    private Good _good;

    public GoodCommand(Good good)
    {
        this._good = good;
    }

    public void Execute()
    {
        this._good.SayGood();
    }
}

public class Controller
{
    ICommand[] commands = new ICommand[3];

    public void SetCommand(int slot, ICommand command)
    {
        commands[slot] = command;
    }

    public void OnPush(int slot)
    {
        commands[slot].Execute();
    }
   
}

posted @ 2008-03-25 15:40 Tony Zhou 阅读(16) | 评论 (0)编辑

导航

统计

公告

目前一天5小时花在英语上 7-24 11:51

与我联系

常用链接

留言簿

我参与的团队

我的标签

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜