簡體   English   中英

WCF服務應用程序中的C#類庫

[英]C# Class Library in WCF Service Application


我對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(在類庫中實現)。 我如何使它起作用?

我的Graph類來自類庫(刪除了一些方法:

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;
        }

    }

好的,當我得到它時,Graph是一個標記為DataContract的類,並且您正在嘗試向其添加可由客戶端訪問的行為。 問題是,DataContract基本上是DTO(數據傳輸對象)。 您定義為DataContract類的一部分的行為都不會對使用方客戶端可用,因為序列化的唯一項將是屬性。 不知道您對Graph對象的ToString()方法的行為的確切期望是什么,您可以向Graph對象添加一個屬性,該屬性返回您希望訪問的任何信息。 這樣,您可以執行以下操作。

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

查看您的Graph類的實現,我發現您正在向DataContract添加一系列行為,這是可疑的。 我將所有這些移至您的OperationContract的實施中。 客戶端無法訪問在Graph類中定義的所有方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM