Thursday, February 17, 2011

ABC and Endpoint of WCF

Address: WCF services must have an address. The address specifies the location of the service which will be exposed for clients that will use it to communicate with the service. The address's protocol that WCF can provided: HTTP , TCP ,NamedPipe , Peer2Peer ,MSMQ.
Binding: Specifies how a service is accessible. In other words: how the two parties will communicate in terms of transport (HTTP , TCP ,NamedPipe , Peer2Peer ,MSMQ) ,encoding (text, binary etc.) and protocols (like transactional support or reliable messaging).
Contract: Used to specify what your service can do. For example: give you the square when providing 2 numbers.

Endpoints:

All communications with the WCF service will happen via the endpoints. The endpoints specify a Contract that defines which methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods. The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted. WCF provides Windows Activation Services which can be used to host the WCF service. Otherwise the WCF service can also be hosted in IIS or in any process by using the Service Host class, which is provided by WCF. Services can also be self-hosted.

Wednesday, January 12, 2011

Encapsulation and Abstraction

Two concepts that go together in the object oriented approach are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of an object, while Encapsulation is the hiding of the non-essential features.

Think of a person driving a car. He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much turning the steering wheel needs, etc (Abstraction).

Consider the example from the programmer’s perspective who wants to allow the user to add items to a list. The user only needs to click a button to add an item (Abstraction). The mechanism of how the item is added to the list is not essential for him (Encapsulation).

By using encapsulation & abstraction, we reduce complexity by ignoring unimportant details and we maintain effectiveness by representing important ones.

Tuesday, December 28, 2010

method for getting all IP addresses

public static string GetIPAddress()
{

String strHostName = string.Empty;
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string IPAddress=string.Empty;
for (int i = 0; i < addr.Length; i++)
{
IPAddress = addr[1].ToString();
}
return IPAddress;
}

Saturday, August 21, 2010

Custom settings in Web.Config

Ever felt the need to switch settings in your web application based on the server it was running on? Well, I have. Suppose you have a connection string in your Web.Config (encrypted, sure). You don't want to be changing the configuration manually everytime you switch from development, to testing and to production. So here's what I do.
First, you create a new section in the Web.Config file.


The mySettings element is inserted as a child of the configuration root element in your Web.Config.

[mySettings]

[!-- development --]

[Configuration url="http://localhost"]

[add key="ConnectionString" value="server=(local);database=Northwind;user id=sa;password=..." /]

[add key="SettingWhatEver" value="true" /]

[/Configuration]

[!-- testing --]

[Configuration url="http://testserver"]

[add key="ConnectionString" value="server=testserver;database=Northwind;user id=dbusr_test;password=..." /]

[add key="SettingWhatEver" value="true" /]

[/Configuration]

[!-- production --]

[Configuration url="http://liveserver"]

[add key="ConnectionString" value="server=prodserver;database=Northwind;user id=dbusr_prod;password=..." /]

[add key="SettingWhatEver" value="false" /]

[/Configuration]

[/mySettings]

*** note here [=< and ]=>

Then, you create a custom section handler for the Web.Config file.

using System.Collections;


using System.Xml;

using System.Configuration;

public class MySettings : Hashtable, IConfigurationSectionHandler
{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{

string HostUrl = HttpContext.Current.Request.Url.Host;

XmlNode NodeSetting = null;

XmlNodeList NodeSettings = null;

Hashtable Settings = new Hashtable();

NodeSettings = section.SelectNodes("Configuration[contains(@url,'" + HostUrl + "')]/add");

foreach (XmlNode NodeSetting_loopVariable in NodeSettings) {

NodeSetting = NodeSetting_loopVariable;

this.Add(NodeSetting.Attributes.GetNamedItem("key").Value, NodeSetting.Attributes.GetNamedItem("value").Value);

}
return this;
}
}


This section handler will read from the mySettings element, and select the node with the url-attribute that contains the HostUrl value. This is a simple XPath expression, perhaps you want to finetune it depending on your needs.

Finally, you go back to the Web.Config and make sure the section is understood and read by adding this element to the Web.Config





The first attribute holds the custom section name in the Web.Config. The second attribute holds the class that will handle this section (WebApp.MySettings) and the assembly where this class can be found. The section handler is located right under the configuration root element, but before the mySettings element mentioned above. Be aware that XML is case-sensitive.
Now that your done, you can access the desired setting through:

string constr = ConfigurationManager.GetSection("mySettings")("ConnectionString");

Depending on the host you are running the web application on, this will return the development, testing or production connection string.

Tuesday, July 13, 2010

.Net framework architecture image

.Net framework Architecture

This image is to understand .Net framework architecture or to have a good idea of the flow