Static Constructor
Ø
Static constructor is first block of code to
execute in class.
Ø
A static constructor is a constructor declared using a static
modifier.
Ø
A static constructor is use to initialize static
data
Ø
Static constructor does not take any parameters.
Ø
It is the first block of code executed in a class. With that,
a static constructor executes only once in the life cycle of class.
class Demo
{
static int val1;
int val2;
static Demo()
{
Console.WriteLine("This is Static
Constructor");
val1 =
70;
}
public Demo(int val3)
{
Console.WriteLine("This is Instance
Constructor");
val2 =
val3;
}
private void show()
{
Console.WriteLine("First Value = " + val1);
Console.WriteLine("Second Value = " + val2);
}
static void Main(string[] args)
{
Demo d1 = new Demo(110);
Demo d2 = new Demo(200);
d1.show();
d2.show();
Console.ReadKey();
}
}
Ø Private Constructor in C#?
Ø Private constructor is constructor that is preceded by private
access specifier. For example, below class has a private constructor.
Ø We know that if we don’t write constructor in the class then by
default constructor gets called on object creation which is public. Or if we
want to allow object creation of the class then we write public constructor or
else we explicitly specify private access specifier on class constructor.
Ø Can we create object of class with private constructor in C#?Ø No, object of a class having private constructor cannot be
instantiated from outside of the class. However, we can create object of the
class inside class methods itself.
|
class A
{
//private
constructor
private
A()
{
}
}
class Program
{
static
void Main(string[] args)
{
A
obj = new A();//Error cannot access private constructor
}
}
|
Following
are the scenarios when we can make use of private constructor in C# code.
If we want to stop object
creation of a class, then we can make the class constructor private. So, if we
try to create an object from the main program or other classes, then compiler
will flash an error.
2) Use in Singleton class
We know
that singleton class allows only single instance of it throughout the execution
of the application. When we design a singleton class, we use private
constructor, so, no user can create object of this class to stop multiple
object creation. And singleton class itself provide the same object over and
over on demand.
3) Stop a class to be inherited
Ø If we don’t want a class to be inherited, then we make the class
constructor private. So, if we try to derive another class from this class then
compiler will flash an error. Why compiler will flash an error? We know the
order of execution of constructor in inheritance that when we create an object
of a derived class then first constructor of the base call will be called then
constructor of derived class. Since, base class constructor is private, hence,
derived class will fail to access base class constructor.
Ø In below c sharp code example, compiler flashes error on
creation of object of derived class or as soon as you write constructor of
derived class.
|
//base
class B
{
private
B()
{
}
}
//Derived
class D : B
{
public
D()
{
}
}
class Program
{
static
void Main(string[] args)
{
D obj = new D();//error
}
}
|
Ø Difference between
Singleton Class and Static Class
Static Class:
- You cannot create the instance
of static class.
- Loaded automatically by the
.NET Framework common language runtime (CLR) when the program or namespace
containing the class is loaded.
- Static Class cannot have
constructor.
- We cannot pass the static class
to method.
- We cannot inherit Static class
to another Static class in C#.
Singleton:
- You can create one instance of
the object and reuse it.
- Singleton instance is created
for the first time when the user requested.
- Singleton class can have
constructor.
- You can create the object of
singleton class and pass it to method.
- Singleton class does not say
any restriction of Inheritance.
- We can dispose the objects of a
singleton class but not of static class.
WCF Predefined Bindings
1.
basicHttpBinding
basicHttpBinding is best
when you have traditional ASMX(Active Server Methods) web services and needs to
be replace with WCF. It supports text as well as MTOM encodings and it does not
support WS-* standards like WS-Addressing, WS-Security and
WS-ReliableMessaging.
basicHttpBinding uses HTTP or HTTPS protocols. You can configure SSL for Transport Layer security with basicHttpBinding.
basicHttpBinding uses HTTP or HTTPS protocols. You can configure SSL for Transport Layer security with basicHttpBinding.
2.
WsHttpBinding
This is secure and
interoperable bindings uses SOAP over HTTP. With WsHttpBinding messages are
encrypted by default and achieve message level security. It supports reliability, transactions and
security over internet. It supports HTTP or HTTPS protocols and text as well as
MTOM encoding.
The difference between
basicHttpBinding and WsHttpBinding is WsHttpBinding does
support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging
3.
wsDualHttpBinding
wsDualHttpBinding is best
when you required bidirectional communication with client. In some scenario
when client makes call to WCF service, after processing of long running request
service has to call client application for example updating shipment details to
client application.
It supports reliability,
transactions and security over internet. It supports HTTP or HTTPS protocols
and text as well as MTOM encoding. You can implement Duplex message exchange pattern with wsDualHttpBinding.
4.
webHttpBinding
webHttpBinding is best
when you wish to implement RESTful WCF service. This is secure and
interoperable binding which sends information directly over HTTP or HTTPS
without creating SOAP messages. It allows HTTP request to use plain old XML
(POX) style messaging which reduces the message size on wire compare to SOAP
messages.
5.
NetTcpBinding
netTcpBinding is best
when WCF service and its clients are in intranet infrastructure. As it supports
only TCP protocol and not HTTP so service cannot be accessed over internet.
This is secure binding is
used to send binary encoded SOAP messages with in intranet computers. It
supports reliability, transaction and security. If your using netTcpBinding and
host WCF service in IIS, you need to make some settings on system and IIS this article will help you for required settings.
6.
netNamedPipeBinding
When your WCF service and
its clients reside on same computer netNamedPipeBinding is the best choice and
gives best performance over other bindings. This is secure bindings. Binary
encoded SOAP messages are sent over named pipes.
7.
netPeerTcpBinding
netPeerTcpBinding is best
when you require more security for peer to peer communication as netTcpBinding
does not provide it. It is secure binding and supports TCP protocols.
8.
WsFederationHttpBinding
It is secure and
interoperable binding supports federated security. It supports HTTP and HTTPS
transport protocols as well as text and MTOM encodings.
9.
NetMsmqBinding
netMsmqBinding is best
when you have to execute service operations in queued manner. Service requests
are placed in queue and executed one by one. With netMsmqBinding service
operations will always be one way and does not return any response to client.
- How implement security in WCF
No comments:
Post a Comment