我正在尝试在Web浏览器中运行vector.asmx并收到以下错误。有什么问题?
无法序列化System.ComponentModel.ISite类型的成员System.ComponentModel.MarshalByValueComponent.Site,因为它是一个接口。
[我的代码]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication2
{
/// <summary>
/// Summary description for Vector
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Vector : System.Web.Services.WebService
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
[WebMethod]
public double MagnitudeSquared()
{
return X * X + Y * Y + Z * Z;
}
[WebMethod]
public double Magnitude()
{
return Math.Sqrt(MagnitudeSquared());
}
[WebMethod]
public static Vector operator -(Vector v1, Vector v2)
{
Vector result = new Vector();
result.X = v1.X - v2.X;
result.Y = v1.Y - v2.Y;
result.Z = v1.Z - v2.Z;
return result;
}
[WebMethod]
public static Vector operator +(Vector v1, Vector v2)
{
Vector result = new Vector();
result.X = v1.X + v2.X;
result.Y = v1.Y + v2.Y;
result.Z = v1.Z + v2.Z;
return result;
}
[WebMethod]
public static Vector operator *(Vector v1, double factor)
{
Vector result = new Vector();
result.X = v1.X * factor;
result.Y = v1.Y * factor;
result.Z = v1.Z * factor;
return result;
}
[WebMethod]
public Vector Clone()
{
Vector result = new Vector();
result.X = this.X;
result.Y = this.Y;
result.Z = this.Z;
return result;
}
}
}
答案 0 :(得分:2)
您无法返回Vector类。这将是对服务实例的引用,这没有任何意义。
事实上,您的整个服务毫无意义。 Web服务是一个专门的类。正常的OO概念(如组合数据和行为)不起作用。您应该有一个类Vector
包含所有公共数据,另一个类VectorService
是服务本身。它会接受Vector
类型的参数,并且也会返回这些值。