简体   繁体   English

WCF服务应用程序中的C#类库

[英]C# Class Library in WCF Service Application


I have a problem with C# class library and WCF Service Application. 我对C#类库和WCF服务应用程序有疑问。 I built a class library with two classes Graph and Vertice. 我用两个类Graph和Vertice构建了一个类库。 And they have few public methods inside. 而且它们内部几乎没有公共方法。 Then I created and published(IIS 7) a simple WCF Application, which uses this class library. 然后,我创建并发布(IIS 7)一个简单的WCF应用程序,该应用程序使用此类库。 Code: 码:

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

Of course publish process went fine, and I have an access to this WCF. 当然,发布过程进行得很好,而且我可以访问此WCF。 And now the problem: 现在的问题是:

When I created an Client Application(Web) and added service reference to this project, I can access the method getGraph(int val) but it returns class which doesn't have any of the methods I implemented in class library. 当我创建一个客户端应用程序(Web)并向该项目添加服务引用时,我可以访问方法getGraph(int val),但它返回的类没有在类库中实现的任何方法。 Code for client: 客户代码:

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

That line returns me 那条线还给我

GraphsClient.GraphService.Graph GraphsClient.GraphService.Graph

But I want it to return my overriden method ToString(which is implemented in class library). 但是我希望它返回我的重写方法ToString(在类库中实现)。 How can I make it works?' 我如何使它起作用?

My Graph class from Class Library(removed few methods: 我的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;
        }

    }

Ok, as I get it you Graph is a class marked as a DataContract and you are trying to add behavior to it that can be accessed by the client. 好的,当我得到它时,Graph是一个标记为DataContract的类,并且您正在尝试向其添加可由客户端访问的行为。 The problem is, a DataContract is basically a DTO (Data Transfer Object). 问题是,DataContract基本上是DTO(数据传输对象)。 None of the behavior you define as part of a DataContract class will be made available to the consuming client as the only items serialized will be the properties. 您定义为DataContract类的一部分的行为都不会对使用方客户端可用,因为序列化的唯一项将是属性。 Not knowing exactly what your expectation is on the behavior of the ToString() method on the Graph object, you could add a property to the Graph Object that returns whatever piece of information you hope to get access to. 不知道您对Graph对象的ToString()方法的行为的确切期望是什么,您可以向Graph对象添加一个属性,该属性返回您希望访问的任何信息。 That way you could do something like this. 这样,您可以执行以下操作。

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

Looking at the implementation of your Graph class, I see that you are adding a whole bunch of behavior to the DataContract as suspected. 查看您的Graph类的实现,我发现您正在向DataContract添加一系列行为,这是可疑的。 I'd move all of those to the implementation of your OperationContract. 我将所有这些移至您的OperationContract的实施中。 None of the methods you have defined in the Graph class will be accessible to the client. 客户端无法访问在Graph类中定义的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM