Pages

Monday, October 1, 2018

How to read XML in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            // Start with XmlReader object

            //here, we try to setup Stream between the XML file nad xmlReader

            using (XmlReader reader = XmlReader.Create(@"D:\xmlFile\demo.xml"))

            {

                while (reader.Read())

                {

                    if (reader.IsStartElement())

                    {

                        //return only when you have START tag



                        switch (reader.Name.ToString())

                        {

                            case "Name":

                            Response.Write("Name of the Element is : " + reader.ReadString());

                                break;



                            case "Location":

                            Response.Write("Your Location is : " + reader.ReadString());

                                break;

                        }

                    }

               

                }

            }

         

        }

    }
---------------------------------XML file--------------------------
<?xml version="1.0" encoding="utf-8"?>

<Students>

  <Student>

    <Name>suneel</Name>

    <Location>Delhi</Location>

  </Student>

  <Student>

    <Name>Sadhna</Name>

    <Location>location4</Location>

  </Student>

  <Student>

    <Name>Name2</Name>

    <Location>location2</Location>

  </Student>

  <Student>

    <Name>Name3</Name>

    <Location>location3</Location>

  </Student>

  <Student>

    <Name>Name4</Name>

    <Location>location4</Location>

  </Student>

</Students>

https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part
https://www.codeproject.com/Articles/487571/XML-Serialization-and-Deserialization-Part

Saturday, November 11, 2017

End to End execution process from IIS6.0 to ASP.NET Application

https://abhijitjana.net/2010/03/14/beginners-guide-how-iis-process-asp-net-request/


https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/29fab1cf-8983-4f62-bff5-356057630f17.mspx?mfr=true

Friday, September 26, 2014

IIS Server Performance


 Application-Based IIS Server Performance Tips:

IIS is the Microsoft implementation of an HTTP web server. As a component of the Windows operating system, IIS web server is tightly integrated into the OS itself and designed to deliver the best possible performance.

IIS Server includes not only the HTTP web server but also the FTP and SMTP services.

Microsoft IIS Server also serves as the foundation for other important business applications such as Exchange and SharePoint.

There are several versions of IIS Server, each tied to the corresponding version of the operating system. Three of the Microsoft IIS servers are IIS 6 in Windows 2003, IIS7 in Windows 2008 and IIS 7.5 in Windows 2008 R2.

Microsoft IIS server is the most used web server after the Apache HTTP Server.

Performance Tips for IIS

Server performance is linked with the number of requests that a server can handle at a time and the time required for processing these requests. Many factors, including hardware, other applications running on the same computer, network bandwidth and other elements, impact IIS 7.0 performance. These performance tips are broadly classified into:

    Application Based
    Software Based
    Feature Based

In this article, we will cover application based performance tips for IIS servers.
Application Based Performance Tips

The following tuning tips, based on the applications running on IIS, can help administrators with IIS management and optimize the performance of IIS.


1. Remove competing applications and services

In order to provide the user with the best possible performance, IIS must have the necessary hardware resources: CPU, disk and memory. Basic IIS monitoring will indicate whether the resources necessary are available.

Other applications and services on the same computer may consume those resources and negatively impact IIS web server performance.

banner-seo-3



To free up hardware resources, use IIS server monitoring and check to see if the other applications or services are left idle and unused. If so, uninstall the unused applications and services.

However, if the other applications and services are actively used, move them to another computer to free up resources for IIS server.
2. Optimize content usage

Content published on a server directly affects its response. One of the benefits of Microsoft IIS server is that it can handle both static and dynamic content.

The advantage of static content over dynamic contact is that static contact is directly served upon the client request, whereas dynamic content needs to be processed before passing it to the client. The processing required for dynamic content results in a resource burden on the IIS server.

Whenever possible, avoid using dynamic content and utilize static content to provide better performance.
3. Limit the queue length for application pools

When running an IIS server in worker process isolation mode, limit the queue length for application pools to prevent large numbers of requests from queuing and overloading the IIS web server.
Adding a new request to the queue can result in exceeding the maximum queue length. In turn, the IIS server may reject the requests and send a 503 error to the client. Keep the queue length shorter to boost IIS performance.
4. IIS HTTP compression


Enable HTTP compression on the Microsoft IIS server to use bandwidth more effectively. This results in faster response times between the IIS web server and compression-enabled browsers.


IIS can provide three compression options, including static files only, dynamic application responses, and both static and dynamic application responses.

Enable dynamic compression for the server when bandwidth is an issue as indicated by IIS server monitoring, but remember that compression consumes CPU time and memory resources significantly.
5. Grow a Web Garden on IIS

The configuration of two or more worker processes as a part of an application pool forms a web garden. A web garden allows for more scalability in an application because multiple processes are used to process requests on the application’s behalf.

A web garden increases performance of the IIS server, especially when there are memory constraints or leaky applications. Creating a web garden on a multiprocessor system boosts application response time on the server. However, when using any session variables, make sure to run out-of-process session management.

Out of process session management is necessary because all worker processes inside the web garden do not share memory space. Having an in-process session management will result in losing session details if the request was previously handled by other worker process.
6. Recycling application pools

This tip applies specifically to Microsoft IIS server 6. Application pools are used to isolate web applications for better security, reliability, and availability and performance and to keep the web applications running without them impacting each other. It separates sets of IIS worker processes that share the same configuration and application boundaries.

Recycling application pools means recycling the worker processes and the memory used for the web application.

You can configure the Microsoft IIS server to periodically restart worker processes under certain conditions, which is required to avoid memory problems.

After recycling an application pool, memory resources on IIS are released until the next request for an application in that pool is received. As a result, the WWW service shuts down all running worker processes that are serving the application pool and starts new worker processes.
7. Logging frequency

Logging too much information affects IIS server performance. Set IIS to log as little information as possible for normal, day-to-day utilization. If there are issues with a site or web server, turn up the logging level to include failed request tracing. In this case, simple IIS management can create better results.

In order to enabled the failed request tracing feature in IIS Manager, follow these steps:

    Select the website to be enabled for failed request tracing
    In the Actions pane, click Failed Request Tracing to bring up the Edit Web Site Failed Request Tracing Settings dialog box, shown below.


    Click the Enable box and select the directory to create the log files and the maximum number of trace files desired. For best results, move the log file creation to a separate volume; otherwise, logging files will hog space on the system drive.

To configure other day-to-day logging, use the Logging applet to set the following items:

    Format
    Directory
    Encoding
    Log file rollover

8. Use the Expires header in IIS7

Configure the HTTP Expires header to improve IIS server performance. Generally, continuous HTTP requests sent from new website visitors to display pages creates a massive load on the IIS server.


Configuring the Expires header allows caching pages (and page elements such as images, scripts, css, etc.) on the client side and helps avoid unnecessary HTTP requests.


In IIS 7, Expires headers can be enabled in the system.webServer section of the web.config file. Using the Expires header, you can determine how long files can be cached on the client side. This may be a certain number of hours or days in the future or a fixed date.

Use these application based performance tips to improve IIS server performance. In our next article, we will cover software based tips to make IIS management easier.


for more:
http://fullsocrates.wordpress.com/2012/07/25/iisreset-vs-recycling-application-pools/

Saturday, October 5, 2013

Convert Html page to Excell of Word file

Public Sub ExportData(ByVal DataToExport As String, ByVal FileName As String, ByVal mode As String)


HttpContext.Current.Response.ClearContent()
If mode.ToUpper() = "EXCEL" Then

Dim attachment As String = "attachment; filename=" & FileName & ".xls"

HttpContext.Current.Response.AddHeader("content-disposition", attachment)

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"

ElseIf mode.ToUpper() = "WORD" Then

Dim attachment As String = "attachment; filename=" & FileName & ".doc"

HttpContext.Current.Response.AddHeader("content-disposition", attachment)

HttpContext.Current.Response.ContentType = "application/ms-word"

End If


HttpContext.Current.Response.Write(DataToExport.ToString())

HttpContext.Current.Response.End()
End Sub

Wednesday, October 2, 2013

FaultException in WCF(Error:The creator of this fault did not specify a Reason.)

 <ServiceContract()> _
Public Interface IService
    <OperationContract()> _
    <FaultContract(GetType(MyExceptionContainer))> _
   Function div(ByVal num1 As Integer, ByVal num2 As Integer) As Integer

End Interface
<DataContract()> _
Public Class MyExceptionContainer
    Dim msg, dsc As String
    <DataMember()> _
    Public Property message() As String
        Get
            Return msg
        End Get
        Set(ByVal value As String)
            Msg = value
        End Set
    End Property
End Class

...............cs...........................
Public Class Service
    Implements IService

    Public Function div(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Implements IService.div
        Try
            Return (num1 \ num2)
        Catch ex As DivideByZeroException
            Dim ec As New MyExceptionContainer
            ec.message = "Error msg"
            Throw New FaultException(Of MyExceptionContainer)(ec, New FaultReason(ec.message))
        End Try
    End Function

End Class

------------------.client Side ---------------------

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim sr As New ServiceReference1.ServiceClient
            Dim div As Integer = sr.div(10, 0)
        Catch ex As FaultException(Of ServiceReference1.MyExceptionContainer)
            Response.Write(ex.Detail.message)

        End Try
        
    End Sub
----------------------
Managing a correct error handling process in an application is a nightmare if you want to do it properly and I would say that it is not always the best exciting part, but it is absolutly needed to prevent as much as possible unhandle situation. In a normal application you simply place the code inside try-catch block with the exception type, and there are handle as normal .net exception object that you can bubble up.
With WCF service I have discover around different reading that it is a bit different, simply because WCF need to guaranty interoperability with any client application in order that they are able to catch error returned.
For that WCF need to convert the “normal .NEt exception” into a SOAP messageexception that will be understandable from client application.
To achive this you need to specify a FaultContract attrribute in your service either declaratively or imperatively way. For my case I have done it with declarative.
You specify the declarative FaultContrat as follow for your service method:

[OperationContract]
[
FaultContract (typeof(MyError))]Boolean myMethod();
MyError is here a custom type which define an error message
After having configured the service FaultContractAttribute, next you need to raise the exception from your server side service method code as follow :
Server side
catch (Exception exc){
  MyError ErrLog = new Maillefer.Nomos.Types.Common.MyError (“This is an error”,”Critical”);  FaultException<MyError> fe = new FaultException<MyError >(ErrLog, new FaultReason(ErrLog.Message));  throw fe;}
So far so good. Then from the client application side you simply need to catch the FaultException error type as you normally do and retrive the message return by the Servcie SOAP message as follow:
Client side
catch (FaultException <MyError> MyError){  string msg = Error.Detail.Message;   MessageBox.Show (msg);
  wcfclient.Close();
}
And that’s it. You then receive the error message send from your server, inside your client application.. Hmmm this is what I was expecting but it was not behaving as expected. I spend days to cross check my code and verify impementation to get the exception correctly thrown but when runnig my application my service was stoping at the time it was throwing the exception (throw fe). The error return from that execution was something strange like :
System.ServiceModel.FaultException`1 was unhandled by user code
After a lot of research I find out the solution on a post mentionning that it was due to some setting ofdebugging scenario of my VS environement, which make my code execution stop at each exception with not really logic message wich was giving a lot of confusion.
To remove this behaviour it was advise to uncheck the 2 following options from the Tools->Option menu of IDE:
 Capture
Please give the comment if the above solution save your time.