C# Class Library in WCF Service Application
NickName:Michał Świątek Ask DateTime:2015-05-05T01:59:08

C# Class Library in WCF Service Application


I have a problem with C# class library and WCF Service Application. I built a class library with two classes Graph and Vertice. And they have few public methods inside. Then I created and published(IIS 7) a simple WCF Application, which uses this class library. 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. 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. 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

But I want it to return my overriden method ToString(which is implemented in class library). How can I make it works?'

My Graph class from Class Library(removed few methods:

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

    }

Copyright Notice:Content Author:「Michał Świątek」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30036752/c-sharp-class-library-in-wcf-service-application

More about “C# Class Library in WCF Service Application” related questions

Is there any difference between C c; and C c = C();?

#include&lt;iostream&gt; using namespace std; class C{ private: int value; public: C(){ value = 0; cout&lt;&lt;"default constructor"&lt;&lt;endl; }

Show Detail

Rebinding C-c to C-c

I'm using Viper, and I want to change its C-c and C-g to the original emacs functions. I can rebind C-g with (define-key viper-vi-global-user-map "C-g" 'keyboard-quit), but how can I rebind C-c, si...

Show Detail

Why value of variable c in (c=c==c) is 2 instead of 1,in C programming?

#include&lt;stdio.h&gt; int main() { int a=10,b=2,c; a=!++b&amp;&amp;(c=c==c); printf("b value is %d \n",b); printf("c value is %d \n",c);

Show Detail

The difference between "C c = new C()" and "A c = new C()" when C is a subclass of A in Java

Let's say we have class A as a parent class, and class C that extends it. class A { void m() { System.out.println("A.m"); } } class C extends A { @Override v

Show Detail

Mounting of /cygdrive/c resolves to C:\cygdrive\c

I am using Cygwin and try to run a bash script which uses the pwd command to construct a path and then generate a directory. The problem is that the directory is created under c:\cygdrive\c rather ...

Show Detail

Prefix key `C-S-c` is echoed as `C-c`

When I press C-S-c, the echo area shows only C-c. Things like C-S- selection do work, however. I'm in Ubuntu 14.10 Utopic Unicorn in case this helps. Here's the code for the key binding (for mul...

Show Detail

What is the difference between C, C99, ANSI C and GNU C?

I have started programming practice on codechef and have been confused by the difference between C and C99. What does C mean here? Is it C89? Check the languages at the bottom of this submit. It co...

Show Detail

Is C# a superset of C?

Is C# a superset of C in anyway, like Objective-C or C++? Is there a way to compile C online with constructs such compiler flags?

Show Detail

C - Scope of C Functions

I apologize if this is a beginner's question, but after working in C for a bit, I finally would like a bit of clarification on exactly what kind of files/functions are available to a function. I

Show Detail

Why a != b != c is not equal to a != b and a != c and b != c?

I want to check if 3 values a, b , c are not equal to each other. Given that a == b == c equals to a == b and b == c and a == c, why does python give a different answer for a != b != c ? Thanks! ...

Show Detail