0

我正在尝试在 Web 浏览器中运行 vector.asmx 并收到以下错误。问题是什么?

无法序列化 System.ComponentModel.MarshalByValueComponent.Site 类型的 System.ComponentModel.ISite 成员,因为它是一个接口。

[我的代码]

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

1 回答 1

2

您不能返回 Vector 类。那将是对服务实例的引用,这是没有意义的。


事实上,你的整个服务毫无意义。Web 服务是一个专门的类。像组合数据和行为这样的普通 OO 概念是行不通的。您应该有一个这样的类Vector包含所有公共数据,而另一个类VectorService是服务本身。它会接受 type 的参数Vector,并且也会返回这样的值。

于 2013-04-02T00:42:59.013 回答