The below code snippet shows how .NET evaluates expressions in if statement.
-------------------------------
string x = null;
if (!string.IsNullOrEmpty(x) && x.ToString().Equals(""))
Console.WriteLine("True");
else
Console.WriteLine("False");
if (string.IsNullOrEmpty(x) || x.ToString().Equals(""))
Console.WriteLine("True");
else
Console.WriteLine("False");
-------------------------------
Output:
False
True
Tuesday, December 8, 2009
Thursday, December 3, 2009
Parsing comma separated string
Following is the simple code to parse comma separated string into a string array.
This StringSplitOptions reduces the overhead on developers.
It trims ',' charaters (ie if any , present in the begining or end).
It removes any empty values (ie "Dinesh,,Uthayakumar").
string Name = "Dinesh,Uthayakumar,Din";
string[] fullname = Name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Console.Write(fullname.Length);
This StringSplitOptions reduces the overhead on developers.
It trims ',' charaters (ie if any , present in the begining or end).
It removes any empty values (ie "Dinesh,,Uthayakumar").
string Name = "Dinesh,Uthayakumar,Din";
string[] fullname = Name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Console.Write(fullname.Length);
Wednesday, December 2, 2009
WCF service hosting within Windows service - Issue
I faced the exception mentioned below when hosting a WCF service in a windows service. When i tried to start the windows serivce i got an message box throwing the following information:
The Service1 service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.
Solution for the below exception:
1. Open the Service Control Manager (Start->Run, type services.msc)
2. Right click on the particular service
3. Click on properties
4. Select Local System Account radio button
Service cannot be started. System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:5755/TestSoapAndRest/Service1/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied
at System.Net.HttpListener.AddAll()
at System.Net.HttpListener.Start()
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject...
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
The Service1 service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service.
Solution for the below exception:
1. Open the Service Control Manager (Start->Run, type services.msc)
2. Right click on the particular service
3. Click on properties
4. Select Local System Account radio button
Service cannot be started. System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:5755/TestSoapAndRest/Service1/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied
at System.Net.HttpListener.AddAll()
at System.Net.HttpListener.Start()
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject...
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
Sunday, October 25, 2009
Guid Validation
Here is a method where we could validate a string as a GUID using Regex pattern matching. It validates the guid case insensitively.
bool IsGuid(string Id)
{
return !string.IsNullOrEmpty(Id) && Regex.IsMatch(Id, "^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$");
}
bool IsGuid(string Id)
{
return !string.IsNullOrEmpty(Id) && Regex.IsMatch(Id, "^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$");
}
Subscribe to:
Posts (Atom)