Wednesday, October 31, 2007

mojoPortal Wins Best Other Open Source CMS 2007

Yesterday, mojoPortal Wins Best Other Open Source Content Management System.
mojoPortal, is an open source web site framework and content management system written in C# that runs under ASP.NET on Windows or under Mono on Linux or Mac OS X.

About The Award:
The Packt Open Source Content Management System Award is designed to encourage, support, recognize and reward an Open Source Content Management System (CMS) that has been selected by a panel of judges and visitors to www.PacktPub.com. Following on from the success of 2006, Packt has expanded the Award for 2007 with an increase in prize money and the addition of new categories.

The 2007 Award will continue to support open source Content Management Systems and in order to reward more than one project, Packt has developed new categories for a wider variety of CMS's to benefit from. These are broken down into five different categories including the overall winner and the most promising Open Source CMS:


After more than 18,000 votes, we've now closed the voting for the  2007 Open Source CMS Awards. Votes are currently being counted and the judges decisions are coming through . The winners will be announced starting from Monday 29 October, in the following order:

Monday 29 October:
Best Open Source Social Networking CMS

Winner:
WordPress
Joint Runners up: Drupal & Elgg

Tuesday 30 October:
Best Other Open Source CMS

Winner: mojoPortal
First Runner up: Plone
Second Runner up: Silva


you can read the full story here

Monday, October 29, 2007

Sample Code Download

During last week I got some mails and comments about problem with downloading sample codes of posts. I just fixed the problem and now all of the post 's sample code link is working properly. But anyway if there was any problem please let me know.

Masoud_TB

Saturday, October 27, 2007

Search .Net

Actually finding something about .NET is easy by using Google or any other search engine, but a custom search engine will give you better and more reliable result.
If you want to have a customize search engine about .NET topics, you can take a look at SearchDotNet.com. You can also recommend your favorite web site or book about .NET.
Here is the site features as it said:

  • OpenSearch support: If you're using Firefox 2.0 or IE7 (or other OpenSearch client), you can add SearchDotNet to your list of search engines.
  • Google Gadgets: Add SearchDotNet to your website, Google home page or Google Desktop.
  • Component search engine: Distinct Custom Search Engine includes the sites of hundreds of component vendors.
  • Book site: Top rated .NET related books.

Monday, September 10, 2007

How to get list of windows user in C#

If you want to have the list of an specific windows group in C# you can get this list by these lines of code below. Notice that you have to add a reference to System.DirectoryServices .

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators","group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
    DirectoryEntry member = new DirectoryEntry(groupMember);
    lstUsers.Items.Add(member.Name);
}

you can download the source code here:
http://www.tabatabaei.info/csharpsamples/WindowsGroupMember.rar

Saturday, September 8, 2007

Detecting is current user an Administrator

In some cases in your windows application you may want to know is the current user a member of Aministrators group or not?
To detect this you can get an object of WindowsIdentity like this:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

Then create an instance of WindowsPrincipan by :

WindowsPrincipal principal = new WindowsPrincipal(identity);

and finally check it by using IsInRole() method like this:

string role = "BUILTIN\\Administrators";
bool IsAdmin = principal.IsInRole(role));

then you can use the IsAdmin variable to determine whether the current user is an Admin or not.

Wednesday, September 5, 2007

Remoting in C#

While you are developing distributed application by csharp, you might need to have communication between objects that run in different processes.
.NET remoting enables client applications to use objects in other processes on the same computer or on any other computer available on its network.(MSDN)

Each remoting application consist of three part:

  • A remotable object.

  • A host application domain to listen for requests for that object.

  • A client application domain that makes requests for that object.

We have two kind of remotable objects, one Marshal-by-value objects and Marshal-by-reference objects.
  • Marshal-by-value objects are either inherited from ISerializable interface or using a Serializable attribute, which are copied and passed from the application domain.
  • Marshal-by-reference objects are the objects from a class which is inherited from MarshalByRefObject class.
Notice that the objects from other classes which are not in that two types cannot be used in remoting.

Part 1: Remotable Types
In my first post about remoting I will create a class named "MyRemotableType" which is a Marshal-by-reference type, and I will put this class into a class library with name MyRemotableTypes.dll.

public class MyRemotableType:MarshalByRefObject
{
public MyRemotableType()
{
Console.WriteLine("A New MarshalByRefObject created");
}
public void AddNumbers(int a,int b)
{
Console.WriteLine("Sum is : {0}",a+b);
}

public string CaldSum(int a,int b)
{
return string.Format("Sum is: {0}", a + b);
}

}


Notice that Marshal-by-reference objects needs to be activated. Activation for Marshal-by-reference objects has two types:
  • Server activation: which means that objects will be created by server at the first method call, but not when the object is initializing by calling new keyword.
  • Client activation: which means that objects will be created by server when the client calls the new keyword.
Server activation it self contains two types, which can be specified with using WellKnownObjectMode enumeration items:
  • Singleton: It means that there will always be only one instance, regardless of how many clients there are for that object, and which have a default lifetime.
  • SingleCall: It means that the system creates a new object for each client method invocation.
Part 2: Host Application
Now I need another application which will listen to request from clients. In this sample I 'm going to create a console application. First I have to add a reference to System.RunTime.Remoting. Then I have to prepare a communication line between clients and server. To do this you can use a TCP or HTTP channel. Like this:

HttpChannel channel = new HttpChannel(1234);
ChannelServices.RegisterChannel(channel);

Then you have register the remotable types you want to prepare. As I said before you can choose with activation you want to use. If you want to have Server Activation use this line of code:

RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotableType),"RemotingTest.soap", WellKnownObjectMode.Singleton);
Console.WriteLine("Remote server started ...\r\nPress enter to stop");
Console.ReadLine();

Notice that in line above I used the Singleton but you can change it to SingleCall, also.You can also use RegisterActivatedServiceType . Notice that here in server application I used ReigsterXXXXServicesType
but in the client side you have to use RegisterXXXXClientType. And I also passed a name for this channel " RemotingTest.soap", this name is used to find the remote channel.

Part 3: Client Application
Now I want to use MyRemotableType and create instance from that but as a remote object. To do this first I add a reference to my MyRemotableTypes.dll. The I have to set the channel in my client application. I do this by using the RegisterWellKnownClientType method of RemotingConfiguration, as I said before.

RemotingConfiguration.RegisterWellKnownClientType(typeof(MyRemotableType),"http://RemoteServerName/RemotingTest.soap");

But notice that you give the type you want to use by remoting and also the url of remote server as parameters.
Now try to create some objects from the MyRemotableType.

MyRemotableType t = new MyRemotableType();
t.AddNumbers(10,10);
Console.WriteLine(t.CaldSum(10,10));

Part 4: Test the Applications
Now for testing the application, first run the Server Application,then while the server is running start the client application. And see the result.

As you may see, you will get just one line printed in client :

Sum is: 20


But in server side you got two printed line:
A New MarshalByRefObject created
ُS um is: 20

It means that you the object is created on server and the first method call is Writing to Console of server, but the result of method is accessible in client.

It will discuss more about remoting in my next posts.
You can download the sample code at:
http://www.tabatabaei.info/csharpsamples/firstremoting.rar

Tuesday, September 4, 2007

C# Copy Semantics

Let 's talk about Copy Semantics a little bit in C# Tuning
There is three type of object copy in C#.

  1. Reference copy
  2. Shallow copy
  3. Deep copy
As you may now the default behavior of c# compiler is the first one. Let me explain it by a sample:

Person p = new Person("Ali", 40);

Person p2 = p;
p2.Name = "Reza";
Console.WriteLine(p.Name); // ==>> the result is Reza
Console.WriteLine (p2.Name); // ==>> the result is Reza

By default when you have the code above you will get a reference copy of your p object. It means that, if you change the value of p2 it will effect the values of p.
So if I want a real copy of my object what I have to do?
There is two way to do this, but with a different. Imagine that I have a class named Invoice which has a reference to Person class, like this:

public class Invoice
{
public int No;
public DateTime Date;
public Person Customer;
//.............
}

Now I want to have a copy of my Invoice object inc.

Invoice inc = new Invoice("1001",DateTime.Now,new Person("Reza",40));

// Invoice inc2 = inc; // It 's not what I really want.

So I have to use the second type of object copy which is Shallow copy. In Shallow Copy you will get a new object with all the values copies to the new object. But the point is that you just have a reference copy of you related references types (like Customer: Person). To get a Shallow Copy of your object you can use MemberwiseClone() method of object. I've created a method called ShallowCopy() in my Invoice class.

public Invoice ShallowCopy()
{
return (Invoice)this.MemberwiseClone();
}

Then if you create an object copy of your invoice and change No or Date values this will not effect to the inc object values. But changing the value of it 's Customer, will do:

Invoice inc2 = inc.ShallowCopy();
inc2.No = 1002;
inc2.Customer.Name = "Masoud";
Console.WriteLine("Invoice No: {0}, Customer Name :{1}",inc.No,inc.Customer.Name); // ==>
I nvoice No: 1001, Customer Name : Masoud
Console.WriteLine("Invoice No: {0}, Customer Name :{1}",inc2.No,inc2.Customer.Name);// ==> Invoice No: 1002, Customer Name : Masoud

To get a Deep Copy of you object, you have to implement IClonable interface for Invoice and all of it 's related classes:

public class Invoice: IClonable
{
public int No;
public DateTime Date;
public Person Customer;
//.............

public object Clone()
{
Invoice myInvoice = (Invoice)this.MemberwiseClone();
myInvoice.Customer = (Person) this.Customer.Clone();
return myInvoice;
}
}

public class Person: IClonable
{
public string Name;
public int Age;

public object Clone()
{
return this.MemberwiseClone();
}
}

Now you have a real deep copy of you invoice object.

Invoice inc3 = (Invoice) inc.Clone();
inc3.No = 1003;
inc3.Customer.Name = "Mohammad";
Console.WriteLine("Invoice No: {0}, Customer Name :{1}",inc.No,inc.Customer.Name); // ==>
I nvoice No: 1001, Customer Name : Masoud
Console.WriteLine("Invoice No: {0}, Customer Name :{1}",inc2.No,inc2.Customer.Name);// ==> Invoice No: 1002, Customer Name : Masoud
Console.WriteLine("Invoice No: {0}, Customer Name :{1}",inc3.No,inc3.Customer.Name);// ==> Invoice No: 1003, Customer Name : Mohammad

You can download the sample code at:
http://www.tabatabaei.info/csharpsamples/copysemantics.rar

Sunday, August 19, 2007

Service Controller Sample

In this sample I've used the ServiceController class to view services installed on local or any machine.
You can download the source code here:
http://www.tabatabaei.info/csharpsamples/serviceController.rar

Please let me know if there was any question.

Windows Service Applications

In some cases you may want to have an application which is performing since the computer has been turned on. For instance consider an application which is logging changes in an specific directory in your server, or you might think about Anti Viruses or any other application like that.

Windows Services a kind of windows application without any user interface, which is working in background of your system. In many situation you may want to have an application which is working on your system, event before any user has been logged in.

There is a class names ServiceBase in System.ServiceProcess namespace which has to be derived if you want to have your own Windows Service application. The ServiceBase classs has OnStart() method which will occur when the service has been started using SCM (Service Control Manager) which is accessible by Services icon on Administrative Tools of Control Panel. You can override the OnStart() method for doing any special task in your service. You can also stop your service tasks by overriding the OnStop() method of ServiceBase.

Consider that every service class needs an Service Installer to install the service. So for creating the service installer just goto design mode of your service class and Right Click on it and then click on Add Installer then you will see that a new class has been added to your project. Consider that your services will be run with an specific User of system, to change this behavior you have to change the Account property of ServiceProcessIntaller instance in the new added class to LocalSystem. In the next post I will talk more about other accounts and properties of this class.

َFor running a service you SCM first you have to install. To achieve this goal you can use the InstallUtil application with your assembly (the exe file) passed as parameter. You may also want to uninstall a service which can be done by passing /U at the end of InstallUtil.

I've prepared a sample which will log all the changes in a specified directory within the configuration file. The log file path is also can be set within the configuration file.

You can download the source code here:
http://www.tabatabaei.info/csharpsamples/systemwatcher.rar

Monday, July 16, 2007

Adding a control into Menu or Toolbar

You can add a control into a menu, context menu or toolbar by using a ToolStripControlHost class.
Just pass the control reference to constructor of ToolStripControlHost class and then add your ToolStripControlHost instance as a MenuItem in you Menu or .... Just take a look at the code below:

private void Form1_Load(object sender, EventArgs e)
{
MonthCalendar picker = new MonthCalendar();
picker.DateSelected += new DateRangeEventHandler(picker_DateSelected);
ToolStripControlHost host = new ToolStripControlHost(picker);
fileToolStripMenuItem.DropDownItems.Insert(2,host);
}

void picker_DateSelected(object sender, DateRangeEventArgs e)
{
MonthCalendar picker = ((MonthCalendar)sender);
this.Text = picker.SelectionStart.ToString("yyyy/MMM/dd");
}


You can download the sample code:
http://www.tabatabaei.info/csharpsamples/CalendarContext.rar


نسخه فارسي اين پست رو مي تونين از طريق لينك زير پيدا كنين:
DeveloperCenter.ir