Sunday, June 13, 2010

Different Hosting models in WCF

This post is to consolidate the different hosting models available for WCF and what is new in .NET 4.0

The following are the different hosting models available for wcf services

I. Self hosting in Managed application

. In a Console application or windows application or WPF application

Scenario & advantages/disadvantages:

Generally whole testing the services.
Not used for production deployment.


II. Windows Services
. WCF services can be hosted in Managed Windows Services .

Scenario & advantages/disadvantages:

Good for long running services
Can be used as a production deployment option.
Life time is controlled by service control manager
Not message activated, means service-host needs to be created in the app domain in order to server the incoming requests for wcf services.

III. Internet Information Server
. Most robust option for production deployment.
. Message based activation supported. IIS takes care of having the servicehost created in the process/app-domain
. Process recycling is supported.
. IIS 5, IIS 6 support only http protocol
. IIS 7 supports all http and tcp protocols.

IV. WAS (Windows Activation Services)
. Provides Message based activation, process recycling etc (what IIS provided to http based wcf services) for all tcp based wcf services along with http protocol also.
. IIS is not required.

Thursday, June 3, 2010

Object oriented concepts

  1. When do you say a programming language is Object oriented?
    1. A language is called object oriented language if it satisfies the following object oriented concepts (OOPS concepts)

i. Abstraction: binding data and methods (behavior) together. Example: Class binds data and methods

ii. Encapsulation: Hiding the data. Access specifiers like public, protected, private, internal are access specifiers

iii. Inheritance: A concept used to create a new class from an existing class there by get the behavior and properties of the base/parent class

iv. Polymorphism: Behaving differently at run time based on the object the behavior is invoked. There are 3 ways we can have polymorphic behavior:

1. Method overriding

2. Method Overloading

3. Operator overloading

  1. Example for Inheritance
    1. Animal is a base class
    2. Tiger, Cow are derived classes
    3. Shout() method on cow says baaa…..
    4. Shout() method on Tiger says grrrrr…..
    5. Both are animals but have different characteristics.
  2. Example for polymorphism
    1. Same as above
  3. What is method overriding?
    1. Overriding means giving a defining the base class method again in the derived class. The base class method must be declared as virtual
    2. In the derived class override (C#) key word should be used to override the method.
  4. What is method overloading?
    1. A method is said to be overloaded when the method signature is different with same name. A signature is different if one of the following is true

i. Number of parameters are different

ii. Sequence of parameters different

iii. Data type of any one parameter is different

    1. Two methods having different return types will not be considered as overloaded
  1. What is the difference between class and an object?
    1. Object is an instance of a class
    2. Class represents a classification, or class is an abstract representation of an object or group or a thing
  2. What is the difference between Abstract class and Interface?
    1. Abstract class should have at least one abstract method.
    2. Abstract class can contain member variables
    3. Abstract class can define methods
    4. Interface cannot have member variables and all the methods should be abstract
    5. An abstract method is nothing but a method declaration without method definition
  3. What is the difference between method declaration and definition?
    1. Declaration means just having the method signature. Like

i. {Return Type} {MethodName} ( {parameters} )


    1. Definition means defining the method.
  1. What is initialization?
    1. Means giving an initial value at the time of declaring the variable
  2. What is the difference between class and a struct?
    1. Class is a reference type and will be stored on heap.
    2. Struct is a value type and will be stored on stack
    3. Class can inherit other classes and implement interface
    4. Struct cannot inherit but can implement interfaces

  1. What is the significance of this keyword?
    1. Each object has a reference “this” which points to itself.
    2. Can be used to refer to the current object.
    3. Also be used by one constructor to explicitly invoke another constructor of the same class.
  2. In which sequence constructors will be called in case of derived classes?
    1. First base classes constructors will be called then derived class constructors will be called
  3. In which sequence destructors will be called in case of derived classes?
    1. First derived classes destructors will be called then base class destructors will be called
  4. What are Static Binding and Dynamic Binding?
    1. Static binding means giving a type to a variable at compile time. Runtime binding mans giving a type to the variable at runtime.
    2. Ex: int x – static biding, object x; x = “some string” --runtime binding

Tuesday, March 30, 2010

Working with MSDTC and System.Transactions in .NET 2.0

Recently I happened to work with System.Transactions namespace and SQL 2000.

I have noticed that if we use TransactionScope, then with SQL 2000 the transaction is automatically enlisted in the MSDTC even if we are using single data base.The code works fine if we deply the SQL 2000 on Win2K server, because MSDTC automatically be started if the code runs. Problem comes only when MSDTC is disabled on this Win2k machine.

This is the exception that occurs:

System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.Transactions.TransactionManagerCommunicationException: Communication with the underlying transaction manager has failed. ---> System.Runtime.InteropServices.COMException (0x8004D01B): Exception from HRESULT: 0x8004D01B at System.Transactions.Oletx.IDtcProxyShimFactory.ConnectToProxy(String nodeName, Guid resourceManagerIdentifier, IntPtr managedIdentifier, Boolean& nodeNameMatches, UInt32& whereaboutsSize, CoTaskMemHandle& whereaboutsBuffer, IResourceManagerShim& resourceManagerShim) at System.Transactions.Oletx.DtcTransactionManager.Initialize() ---

The above problem occurs if we deploy the SQL 2000 on win2k3 server, because by default all security settings are disabled on the MSDTC on Win2K3. So in order to make it work, we need to configure MSDTC on the Win2k3 server to accept remote requests and other stuff.

Go to Control panel -> Adminstrative Services -> Component Services

Component Services -> Computers -> My Computer

go to properties of MyComputer, select MSDTC TAB

click on 'Security Configuration'

Make sure you check "Network DTC Access", "Allow Remote Client","Allow Inbound/Outbound", "Enable TIP"

Now the code should run fine. Else try after restaring the machine.

Note:
This behavior is different if we use SQL 2005 as SQL 2005 promotes the transaction to use MSDTC only when it's required.

Issues with reading Output, Return values from Stored Procedure

The sequence is important when we try to read output/return values from stored procedures using ADO.NET. This article is specific to ExecuteReader and retrieving outparam/return values..

Output or ReturnValues from the storedprocedure cannot be retrieved from the command object unless we completely traverse through the Reader.

for more info check: http://support.microsoft.com/?id=308051

Thanks,
-Krish...

Create .EML or .MSG files programmatically using C#.

In this article I will try to document my experiences about creating the .eml and .msg files using C#.

Problem statement:

The requirement is to send emails to out side world with attachments. The email can contain more than one email as attachment and those attachments should look like as new email message is actually being attached to the outgoing email.

Possible soultions identified:

1. As we need to have email as attachment, so having other kinds of attachments ruled out.
2. Possible option for the email attachments are either .eml or .msg.

.MSG file can be created using Microsoft.Interop.xxx assembly and attach to the email and remember .msg is a Microsoft propriatory standard.

Courtesy : For more information: http://weblogs.asp.net/darrensim/archive/2008/06/29/creating-an-outlook-message-file-with-c.aspx



.eml is the global standard. But how can we create .eml file? This can be done by digging through the System.Net.mail.MailMessage.Send() method.
A very good article is available in codeproject : http://www.codeproject.com/kb/ip/smtpclientext.aspx?msg=2876295