Click here to Skip to main content
15,867,686 members
Articles / Security

ZedGraph in a C# web application in a Medium trust environment

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
9 Oct 2010CPOL6 min read 61.2K   3K   21   4
Tutorial on using ZedGraph on a Medium trust web host, beginning to deployment.

Introduction

Despite some of the things that you find by Googling "ZedGraph (Partial or Medium) trust", Zedgraph 5.1.5 can be used in a Medium trust environment, but the pre-compiled DLLs do not work for this. I had a lot of difficulty in making it work, and the information that I found came from many separate sources. These steps go from start all the way through deployment of a "hello world" web application using ZedGraph in Medium trust. Deployment to a shared web server using Medium trust was particularly difficult for me, mostly due to problems with the Microsoft security system (CAS).

I am a scientist and not a developer, so the description might seem overly detailed in some places and lacking in terminology in others. It may be that some of these steps are unnecessary or can be done more easily.

Preparing the ZedGraph DLLs

  1. Download and unzip the source code from http://sourceforge.net/projects/zedgraph/files/.
  2. Open the project ZedGraph.csproj in the "source" subdirectory, allowing Visual Studio to convert the solution to the more recent version of Visual Studio (2008 in my case).
  3. In Solution Explorer, expand Properties and double-click to open AssemblyInfo.cs. Add:
  4. C#
    using System.Security;
    //(This line may already be present.)
    
    [assembly: AllowPartiallyTrustedCallers ]
    //(Near bottom, I think I just had to un-comment it)
  5. Select Build-- Build ZedGraph. Then close the solution. (You can say Yes if it asked if you want to save it in the newer format.)
  6. Now repeat steps (2), (3), and (4) with ZedGraph.Web.csproj in the "web" subdirectory.

Creating the Web Application

  1. In Visual Studio, select New Project>ASP.NET Web Application, then choose a name. (I use ZedGraphWebAp1 here.)
  2. It might also be helpful to run your tests in Medium trust as well. In the Web.Config file, inside the System.Web section, add: <trust level="Medium">.
  3. XML
    < System.Web>
       ...
       < trust level="Medium"/>
    </system.web>
  4. Repeat step (3) for this project. At this point, you might want to hit Control-F5 to make sure that the project works - it should produce a blank web page without complaints.
  5. Add a subdirectory in the project directory (where Default.asp is located, \ZedGraphWebAp1\ZedGraphWebAp1 in my case) called ZedGraphImages. Now copy ZedGraph.dll from the Zedgraph project bin directory ...\source\bin\Debug into the new project bin directory ...\bin (in my case ...\ ZedGraphWebAp1\ ZedGraphWebAp1\bin). Also copy ZedGraph.Web.dll from ZedGraph ...\web\bin\Debug to the same new project ...\bin directory.
  6. In Visual Studio, hit Project--- Add Reference--- Browse (tab), and browse to ZedGraph.dll in your project /bin directory. Repeat to also add a reference to ZedGraph.Web.dll.
  7. On the main Visual Studio editor panel, select the Default.asx tab, then hit View--- Designer. (This brings up the designer view of Default.aspx.)
  8. Now we add the ZedGraphWeb control to the Toolbox. Select the Toolbox tab (usually on the right) and scroll all the way to the last tab "General". Right-click and select "Choose item". Then hit "Browse" and browse to the ZedGraph.Web.dll that you just copied into your project \bin directory. This should result in a ZedGraphWeb control appearing in the Toolbox under the General tab at the bottom of the list.
  9. Drag the ZedGraphWeb control onto Default.aspx (which is still in Designer view). It should make a control and it should be highlighted. In Properties (to the right), select the lightening-bolt icon to see the events. Double-click RenderGraph. This should bring the edit panel to the "code-behind" Default.asx.cs, where it just added the RenderGraph event handler. This is where we add the code to produce the graph. Start by adding using ZedGraph;, using ZedGraph.Web;, and using System.Drawing;, and then we add the plotting instructions to the RenderGraph handler. Here is the code in Default.aspx.cs that I used to graph a parabola:
  10. C#
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    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;
    using System.Xml.Linq;
    
    using ZedGraph;
    using ZedGraph.Web;
    using System.Drawing;
    
    namespace ZedGraphWebAp1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, 
                      System.Drawing.Graphics g, ZedGraph.MasterPane pane)
            {
                // The code in this event handler is added to make the graph.
    
                GraphPane myPane = pane[0];
    
                myPane.Title.Text = "Title";
                myPane.XAxis.Title.Text = "X axis label";
                myPane.YAxis.Title.Text = "Y axis label";
    
                PointPairList list1 = new PointPairList();
    
                for (int i = 0; i < 10; i++)
                {
                    double x = Convert.ToDouble(i);
                    double y = x * x;
                    list1.Add(x, y);
                }
    
                string textForLegend = "x-squared";
                myPane.CurveList.Clear();
                LineItem myCurve = myPane.AddCurve(textForLegend,
                    list1, Color.Red, SymbolType.Diamond);
                myCurve.Symbol.IsVisible = true;
            }
        }
    }
  11. Now hit Control-F5 to build and run. A graph of a parabola should appear.
  12. Image 1

Deployment

  1. Now we deploy to a hosting environment that operates in Medium trust. Make a subdirectory, e.g., C:\compiledSite8. In Visual Studio, select Build-Publish, and when the Explorer-type window comes up, browse to this new directory. Next, a Settings box appears where the defaults can be used, so hit "Publish". If all goes well, the files will appear in the "publish" directory that you made.
  2. In this "publish" directory, it is a good idea to change the name Default.aspx to another name if you have other web pages already located there. I used the name zedGraphMediumTrust1.aspx.
  3. Also, in the "publish" directory, open Web.Config in a text editor (WordPad is OK). You can remove the line <trust level="Medium">, and it is a good idea to add in the <system.web> section: <customerrors mode="Off">:
  4. XML
    < System.Web>
       ..
       < customErrors mode="Off"/>
    </system.web>

    (Be careful. By default, there is a customErrors section that is commented off. Add this after the end of the commented section.)

  5. Now FTP to your hosting web site. Your host should provide the FTP information, which includes the host name, user name, password, and port. I use the Open-Source FileZilla for this. Go to the main website page. In order to test that this is the right place, it is a good idea to upload a simple HTML file and browse to it. Create a subdirectory ZedGraphImages. You have to give this directory public write permission. The normal FTP chmod functions did not work for me on the Microsoft host, so I had to have my host provider do this. Then FTP all of the other files from the "publish" directory on your local machine to the host main web page. This includes the bin subdirectory and the App_Data subdirectory (although the last is empty and might not be necessary), and the files changedNameWasDefault.aspx (in my case zedGraphMediumTrust1.aspx) and Web.Config.
  6. Pray to Bill Gates, then try it by browsing to YourDomain\changedNameWasDefault.aspx. Hopefully you will get the same graph as shown above.
  7. (Optional) At this point, you might want to make the sample application clearly dynamic. Here are the steps to add a textbox, where the user can type in a quadratic coefficient and watch the graph change:
    1. Go back to Default.aspx (in Designer view) and drag a TextBox to it. In Properties, select the lightening bolt and add the TextBoxChanged event.
    2. Add the code to convert the text box text to a double. Then plot y=ax^2 instead. Follow steps (14) to (19) to publish the revised project. (I think you only need to FTP the new .aspx and bin subdirectory this time.) Here is the revised code in Default.aspx.cs:
    C#
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    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;
    using System.Xml.Linq;
    
    //Added for Zedgraph example:
    using ZedGraph;
    using ZedGraph.Web;
    using System.Drawing;
    
    namespace ZedGraphWebAp1
    {
        public partial class _Default : System.Web.UI.Page
        {
            // This is the coefficient of the parabola,
            // which user will select.
            double a=1.0;
            double aDefault=1.0;
    
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, 
                      System.Drawing.Graphics g, ZedGraph.MasterPane pane)
            {
                // The code in this event handler is added to make the graph.
    
                GraphPane myPane = pane[0];
    
                myPane.Title.Text = "Title";
                myPane.XAxis.Title.Text = "X axis label";
                myPane.YAxis.Title.Text = "Y axis label";
    
                PointPairList list1 = new PointPairList();
    
                for (int i = 0; i < 10; i++)
                {
                    double x = Convert.ToDouble(i);
                    double y = a * x * x;
                    list1.Add(x, y);
                }
    
                string textForLegend = "x-squared";
                myPane.CurveList.Clear();
                LineItem myCurve = myPane.AddCurve(textForLegend,
                    list1, Color.Red, SymbolType.Diamond);
                myCurve.Symbol.IsVisible = true;
    
            }
    
            protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                try
                {
                    a = Convert.ToDouble(TextBox1.Text);
                }
                catch
                {
                    a = aDefault;
                }
    
                TextBox1.Text = a.ToString();
            }
        }
    }

Conclusions

Zedgraph 5.1.5 can be used in a Medium trust environment, but setting it up is not easy. The difficulty is disconcerting. If it is not developed to fit in with the newer Microsoft security measures, then it is easy to imagine that other things might go wrong. ZedGraph is not perfect. For example, the scaling system seems to break down if the width of the control is changed, and seemingly arbitrary tricks are needed to make it work. It seems fundamentally unnecessary that it should need a "ZedGraphImages" subdirectory with public write permission, and this could even be a security risk. On the other hand, ZedGraph is wonderful because it is Open-Source and written in C#. I hope that it continues to improve.

Other Articles on ZedGraph

  1. A flexible charting library for .NET, by Jchampion, 6 Jun 2007, http://www.codeproject.com/KB/graphics/zedgraph.aspx
  2. Using ZedGraph with ASP.NET, by Wahab Hussain, 6 May 2009, ZedGraph.aspx

Files

The download zip file contains the items described below:

History

  • Rev 0: October 2010.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionZedGraph & VS2013: "There are not Component in zedgraph.dll that can be placed on toolbox" Pin
Member 1151861212-Mar-15 4:44
Member 1151861212-Mar-15 4:44 
QuestionAlmost solved but i still missed something others may have too Pin
waynebekker24-Jan-13 9:22
waynebekker24-Jan-13 9:22 
AnswerRe: Almost solved but i still missed something others may have too Pin
AS012347-Feb-13 9:45
AS012347-Feb-13 9:45 
GeneralMy Problem solved.. Pin
asedfred2-Nov-10 0:54
asedfred2-Nov-10 0:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.