65.9K
CodeProject 正在变化。 阅读更多。
Home

在中等信任环境中在 C# Web 应用程序中使用 ZedGraph

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (3投票s)

2010年10月9日

CPOL

6分钟阅读

viewsIcon

62732

downloadIcon

3014

关于在中等信任级别的 Web 主机上使用 ZedGraph 的教程,从入门到部署。

引言

尽管您在 Google 上搜索“ZedGraph (部分或中等) 信任”时可能会发现一些不同的信息,但 Zedgraph 5.1.5 可以在中等信任环境下使用,但预编译的 DLL 不适用于此。我在使其工作方面遇到了很多困难,而且我找到的信息来自许多不同的来源。这些步骤从头到尾介绍了在中等信任环境下使用 ZedGraph 的“Hello World”Web 应用程序的部署过程。部署到使用中等信任的共享 Web 服务器对我来说尤其困难,主要是由于 Microsoft 安全系统 (CAS) 的问题。

我是一名科学家,而不是开发人员,因此描述在某些地方可能显得过于详细,而在其他地方则缺乏术语。也可能其中一些步骤是不必要的,或者可以更轻松地完成。

准备 ZedGraph DLL

  1. http://sourceforge.net/projects/zedgraph/files/ 下载并解压源代码。
  2. 在“source”子目录中打开项目 ZedGraph.csproj,允许 Visual Studio 将解决方案转换为更新版本的 Visual Studio(在我的情况下是 2008)。
  3. 在解决方案资源管理器中,展开“属性”并双击打开 AssemblyInfo.cs。添加
  4. using System.Security;
    //(This line may already be present.)
    
    [assembly: AllowPartiallyTrustedCallers ]
    //(Near bottom, I think I just had to un-comment it)
  5. 选择“生成”——“生成 ZedGraph”。然后关闭解决方案。(如果它询问您是否要以新格式保存,您可以选择“是”。)
  6. 现在对“web”子目录中的 ZedGraph.Web.csproj 重复步骤 (2)、(3) 和 (4)。

创建 Web 应用程序

  1. 在 Visual Studio 中,选择“新建项目”>“ASP.NET Web 应用程序”,然后选择一个名称。(我在这里使用 ZedGraphWebAp1。)
  2. 在中等信任级别运行测试可能也有帮助。在 Web.Config 文件中,在 System.Web 部分内,添加:<trust level="Medium">
  3. < System.Web>
       ...
       < trust level="Medium"/>
    </system.web>
  4. 对该项目重复步骤 (3)。此时,您可能需要按 Control-F5 确保项目正常工作——它应该生成一个空白网页,没有任何抱怨。
  5. 在项目目录中(在我的情况下是 Default.asp 所在的 \ZedGraphWebAp1\ZedGraphWebAp1 ),添加一个名为 ZedGraphImages 的子目录。现在将 ZedGraph.dll 从 Zedgraph 项目 bin 目录 ...\source\bin\Debug 复制到新项目 bin 目录 ...\bin(在我的情况下是 ...\ ZedGraphWebAp1\ ZedGraphWebAp1\bin)。还将 ZedGraph.Web.dll 从 ZedGraph ...\web\bin\Debug 复制到同一个新项目的 ...\bin 目录。
  6. 在 Visual Studio 中,点击“项目”---“添加引用”---“浏览”(选项卡),然后浏览到您的项目 /bin 目录中的 ZedGraph.dll。重复此操作以添加对 ZedGraph.Web.dll 的引用。
  7. 在 Visual Studio 主编辑器面板上,选择 Default.asx 选项卡,然后点击“视图”---“设计器”。(这将显示 Default.aspx 的设计器视图。)
  8. 现在我们将 ZedGraphWeb 控件添加到工具箱。选择工具箱选项卡(通常在右侧),一直滚动到最后一个选项卡“通用”。右键单击并选择“选择项”。然后点击“浏览”并浏览到您刚刚复制到项目 \bin 目录中的 ZedGraph.Web.dll。这应该会导致 ZedGraphWeb 控件出现在工具箱的“通用”选项卡下方的列表末尾。
  9. ZedGraphWeb 控件拖到 Default.aspx 上(它仍然处于设计器视图)。它应该创建一个控件,并且应该被突出显示。在“属性”(右侧)中,选择闪电图标以查看事件。双击 RenderGraph。这应该会将编辑面板带到“代码背后”的 Default.asx.cs,它刚刚添加了 RenderGraph 事件处理程序。这是我们添加代码以生成图表的地方。首先添加 using ZedGraph;using ZedGraph.Web;using System.Drawing;,然后我们将绘图指令添加到 RenderGraph 处理程序。这是我在 Default.aspx.cs 中用于绘制抛物线的代码
  10. 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. 现在按 Control-F5 进行构建和运行。应该会出现一个抛物线图。

部署

  1. 现在我们部署到在中等信任下运行的托管环境。创建一个子目录,例如 C:\compiledSite8。在 Visual Studio 中,选择“构建-发布”,当资源管理器类型的窗口出现时,浏览到这个新目录。接下来,会出现一个“设置”框,可以使用默认值,因此点击“发布”。如果一切顺利,文件将出现在您创建的“publish”目录中。
  2. 在这个“publish”目录中,如果您已经有其他网页,最好将 Default.aspx 的名称更改为其他名称。我使用了 zedGraphMediumTrust1.aspx 的名称。
  3. 此外,在“publish”目录中,用文本编辑器(WordPad 即可)打开 Web.Config。您可以删除 <trust level="Medium"> 这一行,最好在 <system.web> 部分中添加:<customerrors mode="Off">
  4. < System.Web>
       ..
       < customErrors mode="Off"/>
    </system.web>

    (请注意。默认情况下,有一个 customErrors 部分被注释掉。将其添加到注释部分的末尾。)

  5. 现在通过 FTP 连接到您的托管网站。您的主机应该提供 FTP 信息,其中包括主机名、用户名、密码和端口。我为此使用开源的 FileZilla。转到主网站页面。为了测试这是正确的位置,最好上传一个简单的 HTML 文件并浏览它。创建一个子目录 ZedGraphImages。您必须给这个目录公共写入权限。正常的 FTP chmod 函数在 Microsoft 主机上对我不起作用,所以我不得不让我的主机提供商来做这件事。然后将本地机器上“publish”目录中的所有其他文件通过 FTP 上传到主机的根目录。这包括 bin 子目录和 App_Data 子目录(尽管最后一个是空的,可能不是必需的),以及文件 changedNameWasDefault.aspx(在我的情况下是 zedGraphMediumTrust1.aspx)和 Web.Config
  6. 祈祷比尔·盖茨保佑,然后通过浏览到 YourDomain\changedNameWasDefault.aspx 来尝试。希望您能得到与上面相同的图表。
  7. (可选)此时,您可能希望使示例应用程序明显地具有动态性。以下是添加文本框的步骤,用户可以在其中输入二次系数并观察图表的变化
    1. 返回 Default.aspx(在设计器视图中)并拖动一个 TextBox 到其中。在“属性”中,选择闪电图标并添加 TextBoxChanged 事件。
    2. 添加代码将文本框文本转换为 double。然后绘制 y=ax^2。按照步骤 (14) 到 (19) 发布修改后的项目。(我想这次您只需要 FTP 新的 .aspxbin 子目录。)这是 Default.aspx.cs 中修改后的代码
    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();
            }
        }
    }

结论

Zedgraph 5.1.5 可以在中等信任环境下使用,但设置起来并不容易。其困难令人不安。如果它没有开发以适应新的 Microsoft 安全措施,那么很容易想象其他事情可能会出错。ZedGraph 并不完美。例如,如果控件的宽度发生变化,缩放系统似乎会崩溃,并且需要看似任意的技巧才能使其工作。它似乎根本不必要地需要一个具有公共写入权限的“ZedGraphImages”子目录,这甚至可能是一个安全风险。另一方面,ZedGraph 很棒,因为它是一个开源的并用 C# 编写。我希望它能继续改进。

其他 ZedGraph 文章

  1. 一个灵活的 .NET 图表库,作者 Jchampion,2007 年 6 月 6 日,https://codeproject.org.cn/KB/graphics/zedgraph.aspx
  2. 在 ASP.NET 中使用 ZedGraph,作者 Wahab Hussain,2009 年 5 月 6 日,ZedGraph.aspx

文件

下载的 zip 文件包含以下描述的项

历史

  • 修订版 0:2010 年 10 月。
© . All rights reserved.