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

时间:2015-04-21 08:53:31

标签: c# asp.net

我使用的是visual basic 2012,我使用以下代码创建了一个Web服务。我有两个方法GetConnection()和Insert()。我得到连接我已经写了代码来获取数据库字符串。在Inert方法中我 我使用以下代码将值插入EMP表:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService1 : System.Web.Services.WebService 
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader sdr;

    public string name
    {
        set
        {
            name = value;
        }
        get
        {
            return name;
        }
    }
    public string dept
    {
        set
        {
            dept = value;
        }
        get
        {
            return dept;
        }
    }
    public string adress
    {
        set
        {
            adress = value;
        }
        get
        {
            return adress;
        }
    }
        public int salary
    {
        set
        {
            salary = value;
        }
        get
        {
            return salary;
        }

    }
        public string email
        {
            set
            {
                email = value;
            }
            get
            {
                return email;
            }

        }
    public WebService1()
    {
        //
        // TODO: Add any constructor code required
        //
    }

    // WEB SERVICE EXAMPLE
    // The HelloWorld() example service returns the string Hello World.

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public void GetConnection()
    {
        string c = "Data Source=navse-win81_1;Initial Catalog=TestDatabase;Integrated Security=True;Pooling=False";
        con = new SqlConnection(c);
    }
    [WebMethod]
    public int Insert(Test obj)
    {


        GetConnection();
        string query = "insert into Emp(Name,Dept,Salary,Addres,Email)values(@na,@Dt,@sa,@ad,@em)";
        cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@na", obj.name);
        cmd.Parameters.AddWithValue("@dt", obj.dept);
        cmd.Parameters.AddWithValue("@sa", obj.salary);
        cmd.Parameters.AddWithValue("@ad", obj.adress);
        cmd.Parameters.AddWithValue("@em", obj.email);

        int a = cmd.ExecuteNonQuery();

        return a;
    }
}

public class Test:System.Web.Services.WebService
{

    public string name
    {
        set
        {
            name = value;
        }
        get
        {
            return name;
        }
    }
    public string dept
    {
        set
        {
            dept = value;
        }
        get
        {
            return dept;
        }
    }
    public string adress
    {
        set
        {
            adress = value;
        }
        get
        {
            return adress;
        }
    }
    public int salary
    {
        set
        {
            salary = value;
        }
        get
        {
            return salary;
        }

    }
    public string email
    {
        set
        {
            email = value;
        }
        get
        {
            return email;
        }

    }
}

当我尝试在网络浏览器中访问此网络服务时出现以下错误:

Server Error in '/Projects' Application.

Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[NotSupportedException: Cannot serialize member System.ComponentModel.MarshalByValueComponent.Site of type System.ComponentModel.ISite because it is an interface.]

[InvalidOperationException: Cannot serialize member 'System.ComponentModel.MarshalByValueComponent.Site' of type 'System.ComponentModel.ISite', see inner exception for more details.]
   System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +5640106
   System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +122
   System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo) +89
   System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +247
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +378
   System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +5641243
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +378
   System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +5641243
   System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) +378
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1734

[InvalidOperationException: There was an error reflecting type 'Test'.]
   System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1852
   System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +5648048
   System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +869
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +286

[InvalidOperationException: There was an error reflecting 'obj'.]
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +899
   System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access) +133
   System.Web.Services.Protocols.SoapReflector.ImportMembersMapping(XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, Boolean serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault, XmlReflectionMember[] members, Boolean validate, Boolean openModel, String key, Boolean writeAccess) +233
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +1965

[InvalidOperationException: Method WebService1.Insert can not be reflected.]
   System.Web.Services.Protocols.SoapReflector.ReflectMethod(LogicalMethodInfo methodInfo, Boolean client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, String defaultNs) +6262
   System.Web.Services.Description.SoapProtocolReflector.ReflectMethod() +137
   System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding) +1577
   System.Web.Services.Description.ProtocolReflector.Reflect() +641
   System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[] reflectors) +559
   System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url) +109
   System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri, Boolean excludeSchemeHostPortFromCachingKey) +230
   System.Web.Services.Protocols.DocumentationServerProtocol.Initialize() +472
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +122

[InvalidOperationException: Unable to handle request.]
   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +325
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +171

[InvalidOperationException: Failed to handle request.]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +346
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +209
   System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +226
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +145

       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248  

如果我在这里错过了什么,请建议我。

1 个答案:

答案 0 :(得分:1)

您的public int Insert(Test obj)被装饰为网络服务方式。 稍后您可以看到Test继承自System.Web.Services.WebService。所有Web服务方法/对象(在本例中为输入参数Test)都应该是xml可序列化的;最佳实践是使用简单的POCO类,它们只公开您应该公开的字段;绝对不是像WebService中所有继承的东西那样的复杂对象。所以我的建议 - 放弃TestSystem.Web.Services.WebService之间的继承。

像:

public class Test {
 ...
}