Solved

Debugging a C# Web program

Posted on 2016-07-27
3
51 Views
1 Endorsement
Last Modified: 2016-08-01
I have to modify a program that was written for the web.  When I run the code using Visual Studio there isn't a problem.  When I 'Publish' the code and run it from my Chrome browser I start to have problems.

When I have written ASP classic programs I am able to set a bunch of response.write to see what variables are.  In C# it doesn't appear I can do the same thing.  At least I can't figure out how to do it.

I have this line of code but I don't know where to put it to view it.  I am assuming it will need to be on the same page that is published.  For example, Cm.cpt is my connection string.  This is set in a page that is not shown to the user.  When the Login.aspx page is shown, how do I show the connection string?

 HttpResponse Response;

            Response = System.Web.HttpContext.Current.Response;
            Response.Write("Cm.cpt= " + Cm.cpt + "<br>");

Open in new window


How do I display it on my page?
1
Comment
Question by:huerita37
  • 2
3 Comments
 
LVL 41

Expert Comment

by:pcelba
ID: 41733258
It should be possible to create the new web app in IIS, set the root folder of published application as the Working folder and then attach the VS debugger into IIS worker process. Then you may use breakpoints and look at variable values or evaluate expressions in Immediate window etc.
0
 

Author Comment

by:huerita37
ID: 41734750
pcelba - I may  not be understanding you completely because I am able to debug and step through the code while I run the program through VS.  My program runs fine when I run it through VS.  It's when it is live that I am having problems.  I want to see what certain variables are at particular times while the code is LIVE.

Can you confirm that your suggestion is for the LIVE program?
0
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 41734945
No, I cannot confirm it but how different is the application running under IIS on your local PC from the production one?

OK, if you really need values from the production then you may use Response.Write and you don't need to declare it as it is done in automatically generated metafiles.

Let suppose you are using C# language.

Simply create any ASPX page and to the code behind (.aspx.cs file) place the
Response.Write("Your text string");
into e.g. Page_Load method and that's it.

We are using this code to display Session keys:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class My_ShowSession : PageBase // System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        long total = 0;
        foreach (string key in Session.Keys)
        {
            object value = Session[key];
            long objectSize = ObjectSize(value);
            total += objectSize;
            Response.Write("Size:" + objectSize + " | " + key + "<BR/>");
            WriteDetails(value);
            Dictionary<string, object> s = Session[key] as Dictionary<string, object>;
            if (s != null)
            {
                if (s.ContainsKey("__timestamp"))
                {
                    Response.Write("&nbsp; &nbsp; TIMESTAMP is:" + ((DateTime)s["__timestamp"]).ToString()+"<br/>");
                }
            }
        }
        Response.Write("Total size:" + total);
    }

    private void WriteDetails(object value)
    {
       // format and write details here
    }


    private long ObjectSize(object obj)
    {
        try
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            b.Serialize(m, obj);
            return m.Length;
        }
        catch { }
        return 0;
    }

}

Open in new window

And the ASPX code is very simple:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SessionInfo.aspx.cs" Inherits="My_ShowSession" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Session Info</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question