【发布时间】: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