Azure上的Microsoft.Glee.GraphViewerGdi.GViewer抛出异常

时间:2012-10-02 15:08:05

标签: c# asp.net-mvc azure microsoft-glee

在提出我想问的问题之前,让我向您介绍我的问题。

我的公司在我们正在ASP.NET MVC中开发的网站上工作。其中一个页面需要显示一个显示有向图的图像(该图是使用 Microsoft.Glee 图库创建的)。创建图表的方法如下:

private bool CreateGraph(XDocument document, string graphPath)
{
        try
        {
            if (document.Descendants("Nodes").Count() == 0)
                return false;

            Microsoft.Glee.Drawing.Graph graph = new Microsoft.Glee.Drawing.Graph("graph");

            string id, parent;
            foreach (var node in document.Descendants("Nodes"))
            {
                id = node.Attribute("Id").Value;
                parent = node.Attribute("Parent").Value;

                Microsoft.Glee.Drawing.Edge edge = graph.AddEdge(parent, id);

                edge.TargetNode.Attr.Shape = Microsoft.Glee.Drawing.Shape.Circle;
                edge.SourceNode.Attr.Shape = Microsoft.Glee.Drawing.Shape.Circle;
            }

            Microsoft.Glee.GraphViewerGdi.GraphRenderer renderer = new Microsoft.Glee.GraphViewerGdi.GraphRenderer(graph);
            renderer.CalculateLayout();
            Bitmap bitmap = new Bitmap((int)graph.Width, (int)graph.Height, PixelFormat.Format32bppPArgb);
            renderer.Render(bitmap);
            bitmap.Save(graphPath);

            return true;
        }
        catch
        {
            throw;
        }
}

我不是这段代码的原作者,即它是由我公司的另一位程序员编写的,所以我无法证明代码中的决策是正确的。

无论如何,我遇到的问题如下:当我在本地测试页面时,图像创建成功并显示在页面上。但是,当我部署网站时(我们使用Windows Azure进行托管),我得到以下异常:


    The type initializer for 'Microsoft.Glee.GraphViewerGdi.GViewer' threw an exception.
    System.TypeInitializationException: The type initializer for 'Microsoft.Glee.GraphViewerGdi.GViewer' threw an exception. --->
    System.ComponentModel.Win32Exception: Error creating window handle.
      at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
      at System.Windows.Forms.Control.CreateHandle()
      at System.Windows.Forms.Form.CreateHandle()
      at System.Windows.Forms.Control.get_Handle()
      at System.Windows.Forms.Control.CreateGraphicsInternal()
      at System.Windows.Forms.Control.CreateGraphics()
      at Microsoft.Glee.GraphViewerGdi.GViewer.GetDotsPerInch()
      at Microsoft.Glee.GraphViewerGdi.GViewer..cctor()
      --- End of inner exception stack trace ---
      at Microsoft.Glee.GraphViewerGdi.GViewer..ctor()
      at Microsoft.Glee.GraphViewerGdi.GraphRenderer.CalculateLayout()

我认为这是一些内存问题,但是你能告诉我你对它的看法以及可能是问题的原因是什么?搜索网络并没有真正帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

不,这不是内存问题。问题是Glee库似乎需要一个窗口句柄来完成它的工作,这在本地调试时工作正常,因为你登录到服务器并且有一个打开的Window Terminal来生成句柄。

但是,在服务器上运行时,很可能与用户(如网络服务)或未登录到窗口终端的特定用户相关联,因此没有关联的桌面可在其中创建窗口。

运行您的代码的服务器可能具有中等信任,并且不允许创建窗口句柄。我不太清楚Azure是否知道这种情况。

编辑:

您似乎需要在Azure上启用完全信任

http://blogs.msdn.com/b/windowsazure/archive/2009/03/18/hosting-roles-under-net-full-trust.aspx

在服务定义文件中,您需要添加:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyService" 
   xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
   <WebRole name="WebRole" enableNativeCodeExecution="true">
    <InputEndpoints>
      <InputEndpoint name="HttpIn" protocol="http" port="80" />
    </InputEndpoints>
  </WebRole>
</ServiceDefinition>