1. Creational Patterns:
2. Behavioural Patterns:
Command Pattern
http://www.dotnetspark.com/kb/2839-command-pattern---undo-example.aspx
Visitor Pattern
http://weblogs.asp.net/cazzu/articles/25488.aspx
3. Structural Patterns:
Tuesday, November 15, 2011
Why we require to have both an Is-a and a Has-a relationship between Decorator and Component classes.
A decorator Is-a Component (inherits from the component) so that it can be used anywhere a component can (method calls etc..) A decorator Has-a Component (has a composed instance-reference) so that a particular decorator may wrap a particular component (or other decorator) instance and act as a proxy which adds behavior.
Monday, November 7, 2011
WebService - Ways to communicate
Various Ways to communicate to WebService
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
How to authenticate web services
1. Pass username password in the method itself
2. Pass credentials in SoapHeader.
1. Pass username password in the method itself : This is not a good way because we mix the business code with the authentication code.
2. Pass credentials in SoapHeader: It will separate the Authentication code from the business code. Do the following for SoapHeader:
Server Side:
Create a class derived from SoapHeader say AuthHeader with properties UserName and Password.
Link:
How to intercept the soap message before and after serialize:
We need to use SoapExtensions for intercepting the call. Link for the details
http://www.4guysfromrolla.com/articles/012104-1.aspx
Calling Web Service Functions Asynchronously
1. Using BeginMethodName/EndMethodName strategy.
2. Calling MethodNameAsyc (e.g. GetCustomersAsync, where GetCustomers is the methodname in webservice) and attaching MethodNameCompletedEvent (e.g GetCustomersCompleted) to a function before the method call on client side. This function will be a callback function from the web service.
http://weblogs.asp.net/stevewellens/archive/2010/04/02/calling-web-service-functions-asynchronously-from-a-web-page.aspx
"Delayed Render: Say you want to verify a credit card, look up shipping costs and confirm if an item is in stock. You could have three web service calls running in parallel and not render the page until all were finished. Nice. You can send information back to the client as part of the rendered page when all the services are finished.
Immediate Render: Say you just want to start a service running and return to the client. You can do that too. However, the page gets sent to the client before the service has finished running so you will not be able to update parts of the page when the service finishes running.
Summary:
YourFunctionAsync() and an EventHandler will not render the page until the handler fires.
BeginYourFunction() and a CallBack function will render the page as soon as possible."
How to use a handler on WebService Side to validate the username/password by extracting form SoapMessage Directly:
http://progtutorials.tripod.com/Authen.htm
namespace NsWSHttpModuleTest
{
public class MySoapHeader : SoapHeader
{
public string UserName;
public string Password;
}
public class Service1 : System.Web.Services.WebService
{
public MySoapHeader mSoapHeader = null;
public Service1()
{
InitializeComponent();
}
[WebMethod]
[SoapHeader("mSoapHeader", Direction = SoapHeaderDirection.InOut)]
public string HelloWorld()
{
return "User name: " + Context.User.Identity.Name +
"\nAuthenticated: " + Context.User.Identity.IsAuthenticated.ToString() +
"\nIn \"Whatever\" role: " + Context.User.IsInRole("Whatever").ToString();
}
}
}
HTTP module:
namespace NsWSHttpModuleTest
{
public class AuthenticateRequestHttpModule : IHttpModule
{
private HttpApplication mHttpApp;
public void Init(HttpApplication httpApp)
{
this.mHttpApp = httpApp;
mHttpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
}
void OnAuthentication(object sender, EventArgs a)
{
HttpContext context = ((HttpApplication)sender).Context;
Stream stream = context.Request.InputStream;
if (((HttpApplication)sender).Context.Request.ServerVariables["HTTP_SOAPACTION"] == null)
return;
long lStreamPosition = stream.Position;
XmlDocument doc = new XmlDocument();
doc.Load(stream);
stream.Position = lStreamPosition;
string strUser = doc.GetElementsByTagName("UserName").Item(0).InnerText;
string strPwd = doc.GetElementsByTagName("Password").Item(0).InnerText;
if (strUser == "silanliu" && strPwd == "thepassword")
{
string [] astrRoles = {"Whatever"};
GenericIdentity i = new GenericIdentity("Whoever");
context.User = new GenericPrincipal(i, astrRoles);
}
}
public void Dispose()
{}
}
}
Corresponding entry in the web.config file:
Client:
static void Main(string[] args)
{
Service1 s = new Service1();
MySoapHeader header = new MySoapHeader();
header.UserName = "silanliu";
header.Password = "thepassword";
s.MySoapHeaderValue = header;
Console.WriteLine(s.HelloWorld());
Console.ReadLine();
}
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
How to authenticate web services
1. Pass username password in the method itself
2. Pass credentials in SoapHeader.
1. Pass username password in the method itself : This is not a good way because we mix the business code with the authentication code.
2. Pass credentials in SoapHeader: It will separate the Authentication code from the business code. Do the following for SoapHeader:
Server Side:
Create a class derived from SoapHeader say AuthHeader with properties UserName and Password.
Link:
How to intercept the soap message before and after serialize:
We need to use SoapExtensions for intercepting the call. Link for the details
http://www.4guysfromrolla.com/articles/012104-1.aspx
Calling Web Service Functions Asynchronously
1. Using BeginMethodName/EndMethodName strategy.
2. Calling MethodNameAsyc (e.g. GetCustomersAsync, where GetCustomers is the methodname in webservice) and attaching MethodNameCompletedEvent (e.g GetCustomersCompleted) to a function before the method call on client side. This function will be a callback function from the web service.
http://weblogs.asp.net/stevewellens/archive/2010/04/02/calling-web-service-functions-asynchronously-from-a-web-page.aspx
"Delayed Render: Say you want to verify a credit card, look up shipping costs and confirm if an item is in stock. You could have three web service calls running in parallel and not render the page until all were finished. Nice. You can send information back to the client as part of the rendered page when all the services are finished.
Immediate Render: Say you just want to start a service running and return to the client. You can do that too. However, the page gets sent to the client before the service has finished running so you will not be able to update parts of the page when the service finishes running.
Summary:
YourFunctionAsync() and an EventHandler will not render the page until the handler fires.
BeginYourFunction() and a CallBack function will render the page as soon as possible."
How to use a handler on WebService Side to validate the username/password by extracting form SoapMessage Directly:
http://progtutorials.tripod.com/Authen.htm
namespace NsWSHttpModuleTest
{
public class MySoapHeader : SoapHeader
{
public string UserName;
public string Password;
}
public class Service1 : System.Web.Services.WebService
{
public MySoapHeader mSoapHeader = null;
public Service1()
{
InitializeComponent();
}
[WebMethod]
[SoapHeader("mSoapHeader", Direction = SoapHeaderDirection.InOut)]
public string HelloWorld()
{
return "User name: " + Context.User.Identity.Name +
"\nAuthenticated: " + Context.User.Identity.IsAuthenticated.ToString() +
"\nIn \"Whatever\" role: " + Context.User.IsInRole("Whatever").ToString();
}
}
}
HTTP module:
namespace NsWSHttpModuleTest
{
public class AuthenticateRequestHttpModule : IHttpModule
{
private HttpApplication mHttpApp;
public void Init(HttpApplication httpApp)
{
this.mHttpApp = httpApp;
mHttpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
}
void OnAuthentication(object sender, EventArgs a)
{
HttpContext context = ((HttpApplication)sender).Context;
Stream stream = context.Request.InputStream;
if (((HttpApplication)sender).Context.Request.ServerVariables["HTTP_SOAPACTION"] == null)
return;
long lStreamPosition = stream.Position;
XmlDocument doc = new XmlDocument();
doc.Load(stream);
stream.Position = lStreamPosition;
string strUser = doc.GetElementsByTagName("UserName").Item(0).InnerText;
string strPwd = doc.GetElementsByTagName("Password").Item(0).InnerText;
if (strUser == "silanliu" && strPwd == "thepassword")
{
string [] astrRoles = {"Whatever"};
GenericIdentity i = new GenericIdentity("Whoever");
context.User = new GenericPrincipal(i, astrRoles);
}
}
public void Dispose()
{}
}
}
Corresponding entry in the web.config file:
Client:
static void Main(string[] args)
{
Service1 s = new Service1();
MySoapHeader header = new MySoapHeader();
header.UserName = "silanliu";
header.Password = "thepassword";
s.MySoapHeaderValue = header;
Console.WriteLine(s.HelloWorld());
Console.ReadLine();
}
WebService - Ways to communicate
Various Ways to communicate to WebService :-
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
WebService - Ways to communicate
Various Ways to communicate to WebService :-
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
1. Using Proxy to send Soap request/response
2. HttpGet
3. HttpPost
1. Using Proxy:
Create a proxy through VS environment and create an object of proxy class. Call the methods through proxy class.
2. Using HttpGet:
Enable HttpGet in Web.confing at web service level
Send the request through url to WebService like the one below:-
http://localhost/service.asmx/Add?a=1&b=2
localhost - server
service.asmx - service name
Add - Method in Webservice which is being called
a, b - Parameters that Add method expects. Parameters are not taken by order, but they are taken by name
In the above case we have a method called as Add(int a, int b).
3. Using HttpPost:
Enable HttpPost in Web.confing at web service level
Code for Client to Post data to WebService:-
//Creating the Web Request.
HttpWebRequest httpWebRequest = HttpWebRequest.Create("http://localhost/WS1/Service.asmx/Add") as HttpWebRequest;
//Specifing the Method
httpWebRequest.Method = "POST";
//Data to Post to the Page, itis key value pairs; separated by "&"
string data = "a=gj&b=gj1";
//Setting the content type, it is required, otherwise it will not work.
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
//Getting the request stream and writing the post data
using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
sw.Write(data);
}
//Getting the Respose and reading the result.
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
lblMessage.Text = sr.ReadToEnd();
}
Thursday, September 29, 2011
Good Links for Life
How to Mange:
http://zenhabits.net/a-simple-guide-to-setting-and-achieving-your-life-goals/
http://zenhabits.net/a-simple-guide-to-setting-and-achieving-your-life-goals/
Wednesday, September 21, 2011
Technical Links
Entity Framework:
http://blogs.msdn.com/b/wriju/archive/2008/10/16/ado-net-entity-insert-update-and-delete-with-relationship.aspx
http://blogs.msdn.com/b/wriju/archive/2008/10/16/ado-net-entity-insert-update-and-delete-with-relationship.aspx
how to execute a program in another domain
Create shortcut of the program.
Go to properties and view the path.
For sql server it will be something like this:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
Just add this at the front in the shortcut path:
C:\Windows\System32\runas.exe /user:NEWDOMAIN\USERNAME /netonly "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
and voila..you are done.
You will be able to access the resource in another domain from a different domain.
Go to properties and view the path.
For sql server it will be something like this:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
Just add this at the front in the shortcut path:
C:\Windows\System32\runas.exe /user:NEWDOMAIN\USERNAME /netonly "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
and voila..you are done.
You will be able to access the resource in another domain from a different domain.
Sunday, May 15, 2011
Working in Linux/Unix - Basics
Download Tool Putty.
Putty has various exe's that can be used for various purposes:
1. Putty.exe can be used to establish a command prompt connection to the linux server.
2. Pscp.exe is used for transferring files from windows to linux machine.
3. WINSCP is GUI to transfer files from one box(windows) to unix box. It is just like filezilla.
Click on the putty.exe and enter the server name and click Login.
It will ask for username and password.
Enter and login to the box.
[Use the command "sudo bash". It will ask for password again and then you will enter into the user's root.]
Copy command:
cp sourcefile destinationfolder
cp /configfile.xml /abc/def/config/
The above command will copy file with the name "configfile.xml" from the current directory to the /abc/def/config/ folder with the same name.
Putty has various exe's that can be used for various purposes:
1. Putty.exe can be used to establish a command prompt connection to the linux server.
2. Pscp.exe is used for transferring files from windows to linux machine.
3. WINSCP is GUI to transfer files from one box(windows) to unix box. It is just like filezilla.
Click on the putty.exe and enter the server name and click Login.
It will ask for username and password.
Enter and login to the box.
[Use the command "sudo bash". It will ask for password again and then you will enter into the user's root.]
Copy command:
cp sourcefile destinationfolder
cp /configfile.xml /abc/def/config/
The above command will copy file with the name "configfile.xml" from the current directory to the /abc/def/config/ folder with the same name.
Subscribe to:
Posts (Atom)