Absolute Thoughts....

  Home  |   Contact  |   Syndication    |   Login
  5 Posts | 0 Stories | 2 Comments | 0 Trackbacks

News

Article Categories

Archives

Post Categories

Friday, May 18, 2012 #

Have you ever gotten errors when working with WCF services?

  1. Open up your web.config/app.config on the server side and add the following

    <system.diagnostics>
     <!-- This logging is great when WCF does not work. -->
     <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
       <listeners>
        <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "c:\traces.svclog" />
       </listeners>
      </source>
     </sources>
    </system.diagnostics>
  2. A file called traces.svclog will be stored on your harddrive. This will contain the the error message that you're looking for. All you now need is the right tool to open it up. It is called svctraceviewer.exe and usually resides in the folder C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin. If you don't have this folder or anything like it, you go download the Microsoft Windows SDK from here.
  3. Now you can open your log and look for the error that is thrown. There you will find a detailed stacktrace of what's wrong.



Sunday, October 16, 2011 #

ASP.NET view state, in a nutshell, is the technique used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks. View state has caused the most confusion among ASP.NET developers. When creating custom server controls or doing more advanced page techniques, not having a solid grasp of what view state is and how it works can come back to bite you. Web designers who are focused on creating low-bandwidth, streamlined pages oftentimes find themselves frustrated with view state, as well. The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE. This hidden form field can easily get very large, on the order of tens of kilobytes. Not only does the __VIEWSTATE form field cause slower downloads, but, whenever the user posts back the Web page, the contents of this hidden form field must be posted back in the HTTP request, thereby lengthening the request time, as well.

Here's an interesting, odd, but obvious idea. If you're not able to use HttpCompression like the Blowery HttpCompression module that we use with dasBlog due to the pile of bugs in older versions of IE around compression, you can "zip" up the ViewState on fat (usually DataGrid related bloat).

Using Zipped ViewState:

public class CompressedPage : PageViewStateZip

Just deriving from System.Web.UI.Page as usual:

public class RegularPage : System.Web.UI.Page

The "trick" is pretty simple. There are little-known virtuals in Page that you can override - specifically LoadPageStateFromPersistanceMedium (where that persistance medium is a hidden input box) and SavePageStateToPersistenceMedium.

When it's time to load view state, we pull it out of the form and un-base64 the string into a byte array, un-zip the bytes, then deserialize the ViewState. When it's time to save, reverse the process - Serialize, zip, store. There's some overhead, certainly. The amount of compression is usually about 50%, but your mileage may vary (YMMV).

Note that this sample uses SharpZipLib from ICSharpCode, but I assume you could use others, and I'd probably use System.IO.Compression if I was using .NET 2.0. The buffer sizes are hard-coded, but the only one that really matters it the one in Compress(). Again, salt to taste.


using System.IO;
using Zip = ICSharpCode.SharpZipLib.Zip.Compression;

/// <summary>
/// Summary description for vioZip
/// </summary>
public class vioZip
{
    public static byte[] Compress(byte[] bytes)
    {
        MemoryStream memory = new MemoryStream();
        Zip.Streams.DeflaterOutputStream stream = new Zip.Streams.DeflaterOutputStream(memory,new Zip.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072);
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
        return memory.ToArray();
    }

    public static byte[] Decompress(byte[] bytes)
    {
        Zip.Streams.InflaterInputStream stream = new Zip.Streams.InflaterInputStream(new MemoryStream(bytes));
        MemoryStream memory = new MemoryStream();
        byte[] writeData = new byte[4096];
        int size;
        while (true)
        {
            size = stream.Read(writeData, 0, writeData.Length);
            if (size > 0)
            {
                memory.Write(writeData, 0, size);
            }
            else
            {
                break;
            }
        }
        stream.Close();
        return memory.ToArray();
    }
}

Now create a base class for a page just as below

using System;
using System.Web;
using System.IO;
using Zip = ICSharpCode.SharpZipLib.Zip.Compression;
using System.Web.UI;

/// <summary>
/// Summary description for PageViewStateZip
/// </summary>
public class PageViewStateZip: System.Web.UI.Page
{
    protected override object LoadPageStateFromPersistenceMedium()
    {
        string vState = Request.Form["__VSTATE"];
        byte[] bytes = System.Convert.FromBase64String(vState);
        bytes = vioZip.Decompress(bytes);

        LosFormatter format = new LosFormatter();
        return format.Deserialize(System.Convert.ToBase64String(bytes));
    }

    protected override void SavePageStateToPersistenceMedium(object state)
    {
        LosFormatter format = new LosFormatter();
        StringWriter writer = new StringWriter();

        format.Serialize(writer, state);
        string viewStateStr = writer.ToString();

        byte[] bytes = System.Convert.FromBase64String(viewStateStr);

        bytes = vioZip.Compress(bytes);
        string vStateStr = System.Convert.ToBase64String(bytes);
        ClientScript.RegisterHiddenField("__VSTATE", vStateStr);
    }
}

Inherit this PageViewStateZip for all the webpages in your application.

public class CompressedPage : PageViewStateZip.

This article is C# version of the http://www.hanselman.com/blog/ZippingCompressingViewStateInASPNET.aspx.


Saturday, May 21, 2011 #

Below is how we can build a complex JSON in javascript and pass it to ASHX file.


$.toJSON(jsonData) looks like as below

[{"Name":"Pavan Kumar Pabothu","Age":27,"ID":361621},{"Name":"Reddaiah Raju Padhmaraju","Age":27,"ID":362541},{"Name":"Denish Raju Padhmaraju","Age":26,"ID":368941}]

Below is how you can read the object in ASHX page.


 


Thursday, March 17, 2011 #

public class Employees
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public decimal Salary { get; set; }
}

List<Employees> employeeList = new List<Employees>();
List<Employees> resultList = new List<Employees>();
decimal maxSalary;
List<string> employeeNames = new List<string>();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillEmployees();
    }

    // Getting a max salary
    maxSalary = employeeList.Max((emp) => emp.Salary);

    // Filtering a List
    resultList = employeeList.Where((emp) => emp.Salary > 50000).ToList();

    // Sorting a List
    // To get a descending order replace OrderBy with OrderByDescending
    resultList = employeeList.OrderBy<Employees, decimal>((emp) => emp.Salary).ToList();

    // Get the List of employee names only
    employeeNames = employeeList.Select<Employees, string>(emp => emp.Name).ToList();
  
    // Getting a customized object with a given list
    var employeeResultSet = employeeList.Select((emp) => new { Name = emp.Name, BigSalary = emp.Salary > 50000 }).ToList();
}

private void FillEmployees()
{
    employeeList.Add(new Employees { EmployeeId = 1, Name = "Shankar", Salary = 125000 });
    employeeList.Add(new Employees { EmployeeId = 2, Name = "Prasad", Salary = 90000 });
    employeeList.Add(new Employees { EmployeeId = 3, Name = "Mahesh", Salary = 36000 });
}


Wednesday, March 09, 2011 #

If you are using SQL Server management Studio much the we can observer that the list of server names in the log in of it. As you can imagin a period of time after 6 month or 1 year you will see a long list of server names in the login dialog.

How to clear this list...?

I doesn't provide a mechanism to clean nor clear the list, so you'll have to do a little browsing through your file system.

For SQl Server 2005 Management Studio, we should delete the below file
C:\Documents and Settings\<user>\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat.

For SQl Server 2008 Management Studio, we should delete the below file
C:\Documents and Settings\<user>\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\SQLStudio.bin.


After deletion we can re-login the Management studio and can see the empty list.