Calling this funtion will redirect user to his last visited page after logging in.
protected void RequestLogin()
{
this.Response.Redirect(FormsAuthentication.LoginUrl +
"?ReturnUrl=" + this.Request.Url.PathAndQuery);
}
Calling this funtion will redirect user to his last visited page after logging in.
protected void RequestLogin()
{
this.Response.Redirect(FormsAuthentication.LoginUrl +
"?ReturnUrl=" + this.Request.Url.PathAndQuery);
}
public string BaseUrl
{
get
{
string url = this.Request.ApplicationPath;
if (url.EndsWith("/"))
return url;
else
return url + "/";
}
}
Mid-Level .NET Developer
Senior Developers/Architects
C# Component Developers
ASP.NET (UI) Developers
Developers using XML
The encryption begins when the owner of the Web site purchases a time-sensitive certificate from a trusted certificate authority such as VeriSign. You can get a certificate anywhere, or even make your own, but is it trusted? Your browser will let you know. This certificate is a security code created specifically for that one user, or even for that one Web site. The code is so complex that no one else on Earth should have a duplicate.
Getting a certificate can be an involved task. All types of information must be recorded so the issuer of the certificate can be a reliable authority on the certificate’s owner. Information that must be provided includes the name of the site and even the name of the server that hosts the site. Complexity makes counterfeiting incredibly difficult.
This makes the issuer a trusted third party. When your browser sees the secure Web site, it uses the information in the certificate to verify that the site is what it claims to be. Browsers commonly indicate security by presenting a picture of a shiny closed lock at the bottom of the screen. This process is not always perfect because of human error. Maybe 53.com is a valid banking site, but 53RD.com is not. We call that phishing. Unscrupulous people phish for careless people. So be cautious. After the identity of the Web site is accepted, the encryption is negotiated between the browser and the Web server, and the data is all but locked up tight.
Using the thread pool is the easiest technique you can use to create a multithreaded application for the following reasons:
You do not have to create, manage, schedule, and terminate your thread, the thread pool class do all of this for you.
There is no worries about creating too many threads and hence affecting system performance. Thread pool size is constrained by the .NET runtime. The number of threads you can use at the same time is limited.
Despite the ease of use, the thread pool has the following limitations or disadvantages when compared to manually managing your threads:
Instead of creating a thread each time we need one, and then destroying it after finishing, the .NET framework introduces the concept of thread pool. In the thread pool technique, and instead of creating a new thread whenever you need one, your application will obtain a thread from the thread pool. After that obtained thread completes its task, it will be returned to the thread pool instead of destroying it - waiting for the next acquiring. This reusability increases the overall application performance, and reduces the cost of excessive thread creation and termination.
To make use of this technique, you need to use the System.Threading.ThreadPool class. The thread pool will be created the first time you use it. The number of total threads in this pool is restricted by default to 25 threads. This means that all of these 25 threads may be busy at some point. If you need to acquire a new thread at this point, your task will be scheduled waiting for a thread to finish its task and return to the pool.
In the "default.aspx.vb" file, import the "system.Threading" name space as shown in the following line of code.
|
Create a time consuming method that will be executed in background in a separate thread. To be able to assign this method to the thread pool, it must have the same signature as the "WaitCallBack" delegate. This delegate has the following declaration:
|
This delegate represents a callback method to be executed by a thread pool thread. The "state" parameter represents the information that will be used by your time consuming method if you need to pass information to it.
So, our time consuming method will be as indicated in the following code snippet:
|
Now, in the button click handler type the following lines of code:
|
In the line #8 of the above code, we used the ThreadPool.QueueUserWorkItem function to queue our time consuming method for execution in a separate thread from the thread pool. The first parameter to this function is a new instance of the WaitCallBack delegate that represents the method to be executed. In our case, the method to be executed is the 'LongTimeTask' method. The second parameter is the parameter to be passed to the 'LongTimeTask' method itself. In our case this parameter is the text entered by the end user in the text box control. This function returns 'True' when the queuing operation succeed, or 'False' when it fails.
Now, run your application and enter any text into the text box, then click the button, and observe the results.
It will be much better to enter long text in the text box, or to increase the number of loop iterations to get better feeling of this operation. You can also add a new text box control to the "default.aspx" page, so you can test your user interface responsiveness while you run the time consuming task at background.
That's it ...
As you can see it is so easy to use this technique to solve your annoying time consuming tasks problem. Be warned then! This ease will cost you.
Multithreading is a technique that can be used to perform time consuming tasks in a separate additional thread other than the main application thread.
When you, for example, have a time-consuming function, you may need to call this function as a response of a button click. Now, instead of freezing all your application waiting for this function to return / to finish, you can create a new thread and assign this function to. When you do this, your application interface will not be blocked and you can use it to perform other tasks. At the same time, your time-consuming task is being carried out in the background.
You can think of it as the two threads: the main one, and the newly created one. Both are running in parallel, and this improves the performance and responsiveness of your application.
Advantages and Disadvantages of Using MultithreadingDespite improving your application's performance, and avoiding unresponsive user interface, multithreading has the following disadvantages:
Notes:
Each time a thread is created, a certain amount of memory is consumed to hold this thread context information. Hence, the number of threads that can be created is limited by the amount of available memory.
More threads does not mean a faster responsive application, instead it can decrease the performance of your application.
public sealed class classSealed
{
// Class members here.
public string ID;
public double Price;
}
classSealed sc=new classSealed();
sc.ID=”C1”;
sc.Price=500.00;
double Samt=sc. CalculatePrice();
Response.Write(“Total Net Price of Product : “ +” “+sc.ID +” “+ “is”+” “+Samt);
Response.Write(“
”);
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. Sealing a class means one can not derive from it. Sealing a method means one can not override it. In C# structs are implicitly sealed; therefore, they cannot be inherited. If we try to inherit from a sealed class in another class we will get compile time error about Inconsistent accessibility (code is shown in following code listing).public class TestClass : classSealed
{
}
In C# a method can not be declared as sealed. However when we override a method in a derived class, we can declare the overridden method as sealed as shown below. By declaring it as sealed, we can avoid further overriding of this method.
public class testClass{
public int x;
public int y;
public virtual void testMethod(){
}
}
public class TestClass: testClass
{
public override sealed void testMethod()
{
}
}
Abstract classes can add more functionality without destroying the child classes that were using the old version. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. In an interface, creation of additional functions will have an effect on its child classes due to the necessary implementation of interface Methods in classes. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. Say there are two classes, bird and airplane, and both of them have methods called fly. It would be ridiculous for an airplane to inherit from the bird class just because it has the fly() method. Rather, the fly() method should be defined as an interface and both bird and airplane should implement that interface. If we want to provide common, implemented functionality among all implementations of component, we should use an abstract class. Abstract classes allow us to partially implement classes, whereas interfaces contain no implementation for any members. So the selection of interface or abstract classes depends on the needs and design of our project. We can make an abstract class, interface, or combination of both depending on our needs.
The most likely situation in which we make a class or method sealed will be if the class or method is internal to the operation of the library, class, or other classes that we are writing. Because any attempt to override some of its functionality will cause problems. We can mark a class or method as sealed for commercial reasons, in order to prevent a third party from extending our classes. For example, in the .NET base class library string is a sealed class.
We should not use the sealed key word on a method unless that method is itself an override of another method in some base class. If we are defining a new method and we don’t want anyone else to override it, we should not declare it as virtual in the first place. If however, we have overridden a base class method, the sealed keyword provides a way of ensuring that the override supplied to a method is a “final” override that means no one else can override it again.
As given in MSDNHere are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
public class Base: IDisposable
{
private bool isDisposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if (disposing)
{
// Code to dispose the managed resources
// held by the class
}
}
// Code to dispose the unmanaged resources
// held by the class
isDisposed = true;
base.Dispose(disposing);
}
~Base()
{
Dispose (false);
}
}
You should not reimplement IDisposable for a class that inherits from a base class in which IDispose has already been implemented. The following code snippet may help you understand this concept: public class Base: IDisposable
{
private bool isDisposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!isDisposed)
{
if (disposing)
{
// Code to dispose managed resources
// held by the class
}
}
// Code to dispose unmanaged resources
// held by the class
isDisposed = true;
base.Dispose(disposing);
}
~Base()
{
Dispose (false);
}
}
public class Derived: Base
{
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Code to cleanup managed resources held by the class.
}
// Code to cleanup unmanaged resources held by the class.
base.Dispose(disposing);
}
// Note that the derived class does not // re-implement IDisposable
}
In the preceding code, what if the Dispose method were to throw an exception? In that case, the Finalize method would exit prematurely, and the memory would never be reclaimed. Hence, in such situations, it is advisable to wrap the Dispose method in a try-catch block. This will prevent finalization exceptions from orphaning the object. public interface IDisposable
{
void Dispose();
}
The following code illustrates how to implement the Dispose method on a class that implements the IDisposable interface:class Test : IDisposable
{
private bool isDisposed = false;
~Test()
{
Dispose(false);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
}
// Code to dispose the un-managed resources of the class
isDisposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
In the preceding code, when the Boolean variable disposed equals true, the object can free both managed and unmanaged resources; but if the value equals false, the call has been initiated from within the finalizer (~Test) in which case the object should release only the unmanaged resources that the instance has reference to.
class Test
{
// Some Code
~Test
{
//Necessary cleanup code
}
}
In the preceding code, the ~Test syntax declares an explicit destructor in C#, letting you write explicit cleanup code that will run during the finalize operation.protected override void Finalize()
{
try
{
//Necessary cleanup code
}
finally
{
base.Finalize();
}
}
Note that the generated code above calls the base.Finalize method.
You should note the following points should when implementing finalizers:
| ASP.NET Web Services | .NET Remoting |
Protocol | Can be accessed only over HTTP | Can be accessed over any protocol (including TCP, HTTP, SMTP and so on) |
State Management | Web services work in a stateless environment | Provide support for both stateful and stateless environments through Singleton and SingleCall objects |
Type System | Web services support only the datatypes defined in the XSD type system, limiting the number of objects that can be serialized. | Using binary communication, .NET Remoting can provide support for rich type system |
Interoperability | Web services support interoperability across platforms, and are ideal for heterogeneous environments. | .NET remoting requires the client be built using .NET, enforcing homogenous environment. |
Reliability | Highly reliable due to the fact that Web services are always hosted in IIS | Can also take advantage of IIS for fault isolation. If IIS is not used, application needs to provide plumbing for ensuring the reliability of the application. |
Extensibility | Provides extensibility by allowing us to intercept the SOAP messages during the serialization and deserialization stages. | Very extensible by allowing us to customize the different components of the .NET remoting framework. |
Ease-of-Programming | Easy-to-create and deploy. | Complex to program. |