Friday, March 27, 2009

Is it possible to host Tcp endpoint wcf service in IIS?

IIS 5.1 and IIS 6 can only host HTTP bindings. IIS7 has WAS (Windows Activation Service) which allows hosting of endpoints bound to any transport protocol... so it would be capable of TCP.
If you must host with IIS 6, then you're stuck with the HTTP bindings. If not, consider self-hosting in a Windows Service.

Wednesday, March 25, 2009

what is the difference between pure and virtual pure function C#?

When a function is virtual it can have a function body. If you want to overload that function you can, but you don't have to, which is probably what you want in most cases of inheritance.

Pure virtual functions have no body and MUST be overloaded. You cannot create an instance of a class with a pure virtual function, so something else has to inherit from it and overload all pure virtual functions. This is generally what is done for Interface Inheritance, where the interface class is just a bunch of pure virtual functions and all the actual code is in classes that inherit it.

what is pure virtual function in C#?

When you define only function prototype in a base class without and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.
A pure virtual function is a function that must be overridden in a derived class and need not be defined.
A virtual function is declared to be "pure" using the curious "=0"
syntax:
class Base
{
public: void f1(); // not virtual
virtual void f2(); // virtual, not pure
virtual void f3() = 0; // pure virtual
};

Advantage of "using" keyword C#

if u declare any managed/un-managed object in using block,
u don't need to explicitely dispose those object.. once the block execution is over, the Dispose will be called automatically.

using (SqlConnection cn = new SqlConnection(connectionString))
{// do something}

is the same as:

SqlConnection cn = new SqlConnection(connectionString)
try
{// do something...}
finally
{cn.Dispose();}
The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;

Tuesday, March 24, 2009

difference between aspnet_wp.exe and w3wp.exe?

The way an ASP.NET request is handled by IIS is quite different in IIS 6.0 when compared with 5.0. In 5.0, the ASP.NET worker process is handed off control by the aspnet_isapi extension in IIS. The aspnet_isapi dll runs in the inetinfo.exe process in IIS and functions what is known as the CLR host (a CLR host is the piece of unmanaged code which is responsible for loading the CLR into the memory). So aspnet_isapi “hands over” the processing to the worker process named aspnet_wp.exe, where the request passes through a series of HttpModules and an HttpHandler.

But in IIS 6.0, there is a driver named http.sys which listens to all the incoming requests (aspnet_isapi.dll is not in the picture at this point). The moment an ASP.NET specific request comes in, this driver starts an IIS 6.0 worker process (which is not related to ASP.NET at all) named w3wp.exe. This process now loads the aspnet_isapi.dll (CLR host) and the request follows through a similar sequence of HttpModules and HttpHandlers.

Tuesday, March 17, 2009

Threading C#

The .NET Framework defines two types of threads:
foreground and background.

By default when you create a thread, it is a foreground thread, but you can change it to a background thread.
The only difference between a foreground and background thread is that a background thread will be automatically terminated when all foreground threads in its process have stopped.

Thread Methods:
Suspend() -> Suspends the execution of a thread till Resume() is called on that.
Resume() -> Resumes a suspended thread. Can throw exceptions for bad state of the thread.
Sleep() -> A thread can suspend itself by calling Sleep(). Takes parameter in form of milliseconds. We can use a special timeout 0 to terminate the current time slice and give other thread a chance to use CPU time
Join()-> Called on a thread makes other threads wait for it till it finishes its task. States of a Thread.

States of a Thread:
States of a thread can be checked using ThreadState enumerated property of the Thread object which contains a different value for different states.
Aborted -> Aborted already.
AbortRequested -> Responding to an Abort() request.
Background -> Running in background. Same as IsBackground property.
Running -> Running after another thread has called the start()
Stopped -> After finishing run() or Abort() stopped it.
Suspended -> Suspended after Suspend() is called. Unstarted -> Created but start() has not been called.
WaitSleepJoin -> Sleep()/Wait() on itself and join() on another thread. If a thread Thread1 calls sleep() on itself and calls join() on the thread Thread2 then it enters WaitSleepJoin state. The thread exists in this state till the timeout expires or another thread invokes Interrupt() on it.

Properties of a Thread:
Thread.CurrentThread -> Static method gives the reference of the thread object which is executing the current code.
Name -> Read/Write Property used to get and set the name of a thread
ThreadState -> Property used to check the state of a thread.
Priority -> Property used to check for the priority level of a thread.
IsAlive -> Returns a Boolean value stating whether the thread is alive or not.
IsBackground -> Returns a Boolean value stating the running in background or foreground.


ThreadPriority levels: of thread is set or checked by using an enumeration i.e. ThreadPriority. The valid values are for this enumeration are;
Highest AboveNormal Normal BelowNormal Lowest

Synchronization in Threads:-
Using the Lock Keyword
Converting the Code to Enable Synchronization using the Lock Keyword

public void PrintNumbers()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
}

-Using the Monitor Type
The C# lock keyword is just a notation for using System.Threading.Monitor class type. The lock scope actually resolves to the Monitor class after being processed by the C# compiler.
Converting the Code to Enable Synchronization using the Monitor Class

public void PrintNumbers()
{
Monitor.Enter(this);
try
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
finally
{
Monitor.Exit(this);
}
}

Ref : http://www.codeproject.com/KB/threads/thread_synchronization.aspx

C# language's Preprocessor directives?

The C# language's preprocessor directives are as follows:
�#if
�#else
�#elif
�#endif
�#define
�#undef
�#warning
�#error
�#line
�#region
�#endregion

Main use of directives are:
1.Conditional compilation. Using special preprocessing directives, you can include or exclude parts of the program according to various conditions.

2.Line control. If you use a program to combine or rearrange source files into an intermediate file, which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

3.Error and Warning reporting. The directive `#error' causes the preprocessor to report a fatal error and the directive `#warning' is like the directive `#error', but causes the preprocessor to issue a warning and continue preprocessing.
Ref : http://www.csharphelp.com/archives/archive36.html