Monthly Archives: January 2013

What is Dependency Injection (DI)

Dependency Injection allows you to inject object into the class, rather then relying on class to create object.

http://msdn.microsoft.com/en-us/magazine/cc163739.aspx

http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx

http://www.mikesdotnetting.com/Article/117/Dependency-Injection-and-Inversion-of-Control-with-ASP.NET-MVC

Advertisements

Leave a comment

Filed under C#

Differences Article

Response.Redirect vs Server.transfer

http://quickdotnetsolutions.blogspot.in/2011/05/difference-between-responseredirect-and.html

 

wcf service over web services.

http://quickdotnetsolutions.blogspot.in/2011/05/advantages-of-wcf-service-over-web.html?utm_source=blog&utm_medium=gadget&utm_campaign=bp_random

Leave a comment

Filed under ASP.Net, C#, WCF

Wcf Duplex Contract – Subscriber-Publisher usng wsDualHttpBinding

Any real time Application (like score-card etc) that involves update are follow either of two process i.e Pulling or Pushing.

Pulling– client constantly sending request to server for updates. This implementation works fine when there are frequent updates but pulling can be an overhead when updates are very less.

Pushing– Server sends update to client when available. This is the best approach over pulling where we have very less updates from server.

wsDualHttpBinding is used Pushing implementation.
In .net netTcp & namedPipe binding support bi-directional binding but http not, so in WCF they introduced a new binding wsHttpBinding().
It has two channels, one is client to server & other is server to client.
Here we basically have two contracts,
->one contract which is used by service.
->other is used by Client which is a Callback contract.

[ServiceContract(CallbackContract = typeof(IMessageCallback))]
public interface IMessage
{
}

interface IMessageCallback
{
      [OperationContract(IsOneWay = true)]
      void OnMessageAdded(string message, DateTime timestamp);
}

Leave a comment

Filed under WCF

What can’t be saved in ViewState?

Any data (Serialized) that you want to access over the postback can be saved in ViewState.

DataReader can’t be serialized as it is in Connected mode. So you can save DataReader in ViewState.

Leave a comment

Filed under C#

Cookies

Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class.

You can also set a cookie’s date and time expiration. Expired cookies are deleted by the browser when a user visits the site that wrote the cookies. you can set the expiration date to be 50 years from now.

If you do not set the cookie’s expiration, the cookie is created but it is not stored on the user’s hard disk. When the user closes the browser, the cookie is discarded. Those are called non- Persistant cookies.
eg: non-persistent cookies are useful if the user is working on a public computer, where you do not want to write the cookie to disk due to security reason.

You can add cookies to the Cookies collection in a number of ways.

Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

Cookies with More Than One Value
You can store multiple name-value pairs in a single cookie.
eg: instead of creating two separate cookies named userName and lastVisit, you can create a single cookie named userInfo that has the subkeys userName and lastVisit.
Benefits
1) cookie attribute such as expiration can be applied to single cookie file instead of each cookie individually.
2) as we know we can’t store more then 20 cookies per site, so by using single cookie like above we save on number of cookies per site.

Response.Cookies["userInfo"]["userName"] = "patrick";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

Controlling Cookie Scope
By default, all cookies for a site are stored together on the client, and all cookies are sent to the server with any request to that site. In other words, every page in a site gets all of the cookies for that site. However, you can set the scope of cookies in two ways:
Limiting Cookies to a Folder or Application
To limit cookies to a folder on the server, set the cookie’s Path property, as in the following example:

HttpCookie appCookie = new HttpCookie("AppCookie");
appCookie.Value = "written " + DateTime.Now.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
appCookie.Path = "/Application1";
Response.Cookies.Add(appCookie);

The path can either be a physical path under the site root or a virtual root. The effect will be that the cookie is available only to pages in the Application1 folder. So cookie will be available to path http://www.contoso.com/Application1/ or any page beneath that folder.

Limiting Cookie Domain Scope
By default, cookies are associated with a specific domain. For example, if your site is http://www.contoso.com, the cookies you write are sent to the server when users request any page from that site. If your site has subdomains—for example, contoso.com, sales.contoso.com, and support.contoso.com—then you can associate cookies with a specific subdomain.

Response.Cookies["domain"].Value = DateTime.Now.ToString();
Response.Cookies["domain"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["domain"].Domain = "support.contoso.com";

Reading Cookies
When a browser makes a request to the server, it sends the cookies for that server along with the request. In your ASP.NET applications, you can read the cookies using the HttpRequest object, which is available as the Request property of your Page class. structure of the HttpRequest is same as HttpResponse object, so you can read them much the same way as you wrote them.

if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);

if(Request.Cookies["userName"] != null)
{
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}

HtmlEncode method was called to encode the contents of a cookie to make sure executable script are not added into the cookie.
Similarly reading a subkey: Server.HtmlEncode(Request.Cookies[“userInfo”][“userName”]);
The subkeys in a cookie are typed as a collection of type NameValueCollection. Therefore, another way to get an individual subkey is to get the subkeys collection and then extract the subkey value by name, as shown in the following example:
System.Collections.Specialized.NameValueCollection
UserInfoCookieCollection;

UserInfoCookieCollection = Request.Cookies["userInfo"].Values;
Label1.Text =
Server.HtmlEncode(UserInfoCookieCollection["userName"]);
Label2.Text =
Server.HtmlEncode(UserInfoCookieCollection["lastVisit"]);

Leave a comment

Filed under C#