【问题标题】:C# Class Library in WCF Service ApplicationWCF 服务应用程序中的 C# 类库
【发布时间】:2015-07-14 05:30:43
【问题描述】:


我对 C# 类库和 WCF 服务应用程序有疑问。 我构建了一个包含两个类 Graph 和 Vertice 的类库。而且它们内部几乎没有公共方法。然后我创建并发布(IIS 7)一个简单的 WCF 应用程序,它使用这个类库。 代码:

public class GraphsService : IGraphsService
{
    public Graph getGraph(int val)
    {
        return new Graph(val);
    }
}

当然发布过程很顺利,我可以访问这个 WCF。现在的问题是:

当我创建了一个客户端应用程序(Web)并向这个项目添加服务引用时,我可以访问方法 getGraph(int val) 但它返回的类没有我在类库中实现的任何方法。 客户端代码:

protected void Page_Load(object sender, EventArgs e)
    {
        GraphsServiceClient gc = new GraphsServiceClient();
        Graph g = gc.getGraph(5);
        lblGraph.Text = g.ToString();
    }

那条线返回我

GraphsClient.GraphService.Graph

但我希望它返回我的重写方法 ToString(在类库中实现)。我怎样才能让它工作?'

我的类库中的图表类(删除了几个方法:

public class Graph
    {
        protected List<Vertice> _vertices;

        public Graph()
        {
            this._vertices = new List<Vertice>();
        }
        public Graph(int startValue)
        {
            this._vertices = new List<Vertice>();
            this._vertices.Add(new Vertice(startValue));
        }

        public List<Vertice> Vertices
        {
            get
            {
                return this._vertices;
            }
        }

        public Vertice getFirstVertice()
        {
            if (this._vertices.Count != 0) return this._vertices.First();
            throw new GraphException("Graaph doesn't have any vertices in it!");
        }

        public Vertice findVertice(int value)
        {
            foreach (Vertice v in this._vertices)
            {
                if (v.value == value) return v;
            }
            return null;
        }

        public Graph addVertice(Vertice v, bool undirected = true)
        {
            if (this._vertices.Contains(v)) throw new GraphException("There is already this vertice in graph!");
            foreach (int val in v.Neighbours)
            {
                Vertice tmp = this.findVertice(val);
                if (tmp != null)
                {
                    if (undirected) tmp.addNeighbour(v.value);
                }
                else throw new GraphException("There isn't a vertice " + val + " in graph so it can't be in neighbours list!");
            }
            this._vertices.Add(v);
            return this;
        }

        public int[,] getAdjacencyMatrix()
        {
            int[,] adMatrix = new int[this._vertices.Count + 1, this._vertices.Count + 1];
            Array.Clear(adMatrix, 0, adMatrix.Length);
            int l = 1;
            foreach (Vertice v in this._vertices)
            {
                adMatrix[0, l] = v.value;
                adMatrix[l, 0] = v.value;
                l++;
            }
            for (int i = 1; i < this._vertices.Count + 1; i++)
            {
                for (int j = 1; j < this._vertices.Count + 1; j++)
                {
                    int val1 = adMatrix[i, 0];
                    int val2 = adMatrix[0, j];
                    Vertice v = this.findVertice(val1);
                    if (v.hasNeighbour(val2)) adMatrix[i, j] = v.countNeighbours(val2);
                }
            }
            return adMatrix;
        }

        public string getAdjacencyMatrixAsString()
        {
            string ret = "";
            int[,] adM = this.getAdjacencyMatrix();
            for (int i = 0; i < Math.Sqrt((double)adM.Length); i++)
            {
                for (int j = 0; j < Math.Sqrt((double)adM.Length); j++)
                {
                    if (i != 0 || j != 0) ret += adM[i, j] + "\t";
                    else ret += "\t";
                }
                ret += "\n";
            }
            return ret;
        }

        public override string ToString()
        {
            string ret = "";
            foreach (Vertice v in this._vertices)
            {
                ret += "Vertice: " + v.value + " neighbours: ";
                foreach (int val in v.Neighbours)
                {
                    ret += val + ", ";
                }
                ret = ret.Remove(ret.Length - 2);
                ret += "\n";
            }
            ret = ret.Remove(ret.Length - 1);
            return ret;
        }

    }

【问题讨论】:

  • 您正在从 WCF 服务返回一个 Graph 对象。请发布Graph 类,并告诉我们您希望显示该类的哪些属性。就像现在一样,它试图将整个Graph 类转换为一个字符串,除非你覆盖ToString,否则它只会返回类型名称。我们需要看看您是如何覆盖它的,因为 Graph 类中的某些内容不正确。
  • Method ToString 被覆盖,在客户端应用程序中我看不到任何在类库中实现的方法。
  • 你能检查一下Page_Load中Graph的命名空间吗?我害怕你使用 mex 生成的代码 而不是你的班级
  • 您在客户端使用“添加服务引用”时,是否表示要重用类型?否则,行为符合预期。
  • 感谢约翰,它有帮助。

标签: c# .net wcf class-library


【解决方案1】:

好的,据我所知,您的 Graph 是一个标记为 DataContract 的类,您正在尝试向其添加可由客户端访问的行为。问题是,DataContract 基本上是一个 DTO(数据传输对象)。您定义为 DataContract 类的一部分的任何行为都不会提供给消费客户端,因为唯一序列化的项目将是属性。不知道您对 Graph 对象上 ToString() 方法行为的确切期望是什么,您可以向 Graph 对象添加一个属性,该属性返回您希望访问的任何信息。这样你就可以做这样的事情了。

Graph g = gc.getGraph(5);
lblGraph.Text = g.Name;

查看您的 Graph 类的实现,我发现您正在向 DataContract 添加一大堆行为。我会将所有这些转移到您的 OperationContract 的实施中。客户端将无法访问您在 Graph 类中定义的任何方法。

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 2013-11-25
    • 2010-12-21
    • 2013-02-03
    • 2010-11-15
    • 2015-03-26
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多