beyondjay

 

2008年4月26日

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 阅读(21) | 评论 (0)编辑

2008年3月25日

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 阅读(11) | 评论 (0)编辑

2008年3月3日

Decorator模式

根据《Head Of Design Pattern》里面的Java代码改写的Decorator模式

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_Decorator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Beverage beverage1 = new Espresso();
        beverage1 = new Mocha(beverage1);
        beverage1 = new Sugar(beverage1);
        Response.Write(beverage1.cost());
        Response.Write(beverage1.GetDescription());
    }
}

public abstract class Beverage
{
    protected string description = "Unknown Beverage";

    public virtual string GetDescription()
    {
        return description;
    }

    public abstract double cost();
}

public class Espresso : Beverage
{
    public Espresso()
    {
        this.description = "Espresso";
    }

    public override double cost()
    {
        return 1.99;
    }
}

public class HouseBlend : Beverage
{
    public HouseBlend()
    {
        this.description = "House Blend";
    }

    public override double cost()
    {
        return 0.89;
    }
}

public abstract class CondimentDecorator : Beverage
{
   public override abstract string GetDescription();
}

public class Mocha : CondimentDecorator
{
    Beverage beverage;

    public Mocha(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public override string GetDescription()
    {
        this.description = beverage.GetDescription() + ", Mocha";
        return this.description;
    }

    public override double cost()
    {
        return beverage.cost() + 0.2;
    }
}

public class Sugar : CondimentDecorator
{
    Beverage beverage;

    public Sugar(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public override string GetDescription()
    {
        return beverage.GetDescription() + ", Sugar";
    }

    public override double cost()
    {
        return beverage.cost() + 0.12;
    }
   
}

 

posted @ 2008-03-03 13:46 Tony Zhou 阅读(5) | 评论 (0)编辑

2008年2月14日

百无聊赖设计模式系列-singleton

今天继续百无聊赖。

许多公司在招人的时候都会要求写一个singleton,我觉得这个本身就蛮可笑的。这个有啥写的,不会的人看5分钟都背出来了。

一般这么写会让你通过。
public sealed class Singleton
 {
     static Singleton instance=null;
 
     Singleton()
     {
     }
 
     public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

但是多线程的时候这个会有多个实例出来的。
所以比较好的还是用下面这个,我把test page和class包括了,还有点注释。我喜欢用page而不是console做test,呵呵。
参考了:
http://www.aspcool.com/lanmu/browse1.asp?ID=1138&bbsuser=csharp
这个作者还有另外一种实现,但是我觉得lazy instantiation还要客户端去单独写代码保证线程同步是有问题的。最好还是在singlton类内部做。

using System;
using System.Threading;
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_Singleton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread thread0 = Thread.CurrentThread;
        thread0.Name = "Thread 0";

        Thread thread1 = new Thread(new ThreadStart(this.DoSomeWork));
        thread1.Name = "Thread 1";

        Thread thread2 = new Thread(new ThreadStart(this.DoSomeWork));
        thread2.Name = "Thread 2";

        Thread thread3 = new Thread(new ThreadStart(this.DoSomeWork));
        thread3.Name = "Thread 3";

        thread1.Start();
        thread2.Start();
        thread3.Start();

        DoSomeWork(); // thread0 do the same work
    }

     public void DoSomeWork()
     {
         MySingleton mySingleton = MySingleton.GetInstance();

         for (int i = 1; i < 5; i++)
         {
             mySingleton.Add();
             Response.Write("Thread" + Thread.CurrentThread.Name.ToString() + "report: the current counter number is " + mySingleton.GetNumber().ToString() + "<br>");
         }

     }

}


public class MySingleton
{
    private MySingleton() { }//it is must for a singleton
    private int totalNum = 0;//for test in test page.
    private static MySingleton mySingleton = new MySingleton();//that means early instantiation, it's safe for multi-thread

    //client get the instance by this method
    public static MySingleton GetInstance()
    {
        return mySingleton;
    }

    // add the value by 1
    public void Add()
    {
        totalNum++;
    }

    //get the count number, for test in test page.
    public int GetNumber()
    {
        return totalNum;
    }
}


 

posted @ 2008-02-14 15:26 Tony Zhou 阅读(18) | 评论 (0)编辑

2008年2月13日

今天百无聊赖的成果

照着UML图,随便写了个Abstract Factory模式



Load assembly的时候有点不一样,因为那个目录很怪很长,是个临时目录,所以我用location得到了它。很奇怪阿。怎么放那里,看来vs2008是不一样阿。

 string location = Assembly.GetAssembly(Type.GetType(factoryName)).Location; 
 factory = (IFactory)Assembly.LoadFile(location).CreateInstance(factoryName);

代码没啥说的,就是连连手而已。小时候看图说话,现在看图写代码,哈哈。

using System;
using System.Collections;
using System.Configuration;
using System.Reflection;
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 FactoryMethod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string factoryName = ConfigurationManager.AppSettings.Get("factoryName");
        string location = Assembly.GetAssembly(Type.GetType(factoryName)).Location;

        IFactory factory;
        factory = (IFactory)Assembly.LoadFile(location).CreateInstance(factoryName);

        Response.Write(factory.CreateProduct1());
        Response.Write(factory.CreateProduct2());
    }
}


//interface for productA
public interface IProduct1
{
    string GetProductName();
}

//interface for productB
public interface IProduct2
{
    string GetProductCode();
}

public class ProductA1 : IProduct1
{
    public string GetProductName()
    {
        return "ProductA1";
    }
}

public class ProductA2 : IProduct2
{
    public string GetProductCode()
    {
        return "ProductA2";
    }
}

public class ProductB1 : IProduct1
{
    public string GetProductName()
    {
        return "ProductB1";
    }
}

public class ProductB2 : IProduct2
{
    public string GetProductCode()
    {
        return "ProductB2";
    }
}

interface IFactory
{
    IProduct1 CreateProduct1();
    IProduct2 CreateProduct2();
}

public class FactoryA : IFactory
{
    public IProduct1 CreateProduct1()
    {
        return new ProductA1();
    }

    public IProduct2 CreateProduct2()
    {
        return new ProductA2();
    }
}

public class FactoryB : IFactory
{
    public IProduct1 CreateProduct1()
    {
        return new ProductB1();
    }

    public IProduct2 CreateProduct2()
    {
        return new ProductB2();
    }
}


 

posted @ 2008-02-13 17:39 Tony Zhou 阅读(229) | 评论 (2)编辑

导航

统计

与我互动

常用链接

留言簿

我参与的团队

我的标签

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜