使用Vs2005中:ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxsamples/html/31db1897-b551-42e6-bd53-0852a68adc08.htm
例子时,我直接运行此例子,一切正常,得到了正确的结果。
我在ISharedInterface项目中增加了一个BusinessEntity类,此类从DataSet继承。只是简单的定义了一个DataSet代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;namespace Microsoft.Samples.SharedInterface
{
[Serializable]
public class BusinessEntity : DataSet
{
public BusinessEntity()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("CustomerName", typeof(string)));
dt.Columns.Add(new DataColumn("CustomerBirthday", typeof(DateTime)));
DataRow dr = dt.NewRow();
dr[0] = "bvw";
dr[1] = DateTime.Now;
dt.Rows.Add(dr);
this.Tables.Clear();
this.Tables.Add(dt);
} public string CustomerName
{
get { return this.Tables[0].Rows[0][0].ToString(); }
} public DateTime CustomerBirthday
{
get { return (DateTime)this.Tables[0].Rows[0][1]; }
}
}
}在ISharedIterface接口和ImplementionClass类中增加了下个GetEntity()的方法:
public BusinessEntity GetEntity()
{
return new BusinessEntity();
}
我在Client类加增加了下面的代码:
BusinessEntity entity = remoteObject.GetEntity();
Console.WriteLine("Customer's Name is: {0}, birthday is: {1}", entity.CustomerName, entity.CustomerBirthday.ToLongDateString());
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
但是报出了这样的错误:
The constructor to deserialize an object of type 'Microsoft.Samples.SharedInterface.BusinessEntity' was not found.但如果我在Client类里增加一个引用ImplementionClass的引用:using Microsoft.Samples.Implementation;
然后在增加如下代码:
ImplementationClass obj = new ImplementationClass();
BusinessEntity directEntity = obj.GetEntity();
Console.WriteLine("Customer's Name is: {0}, birthday is: {1}", directEntity.CustomerName, directEntity.CustomerBirthday.ToLongDateString());
Console.WriteLine("Press ENTER to exit...");
则能返回下确结果。请高帮忙!

解决方案 »

  1.   

    更正一下,在“我在Client类加增加了下面的代码:”增加一个RemoteObject的初始化代码:“RemotingConfiguration.Configure("Client.exe.config");
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem");”
    我在Client类加增加了下面的代码:
                RemotingConfiguration.Configure("Client.exe.config");
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem"); BusinessEntity entity = remoteObject.GetEntity();
    Console.WriteLine("Customer's Name is: {0}, birthday is: {1}", entity.CustomerName, entity.CustomerBirthday.ToLongDateString());
    Console.WriteLine("Press ENTER to exit...");
    Console.ReadLine();
      

  2.   

    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem");
    //这样有问题吧
    改成
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ImplementionClass), "tcp://localhost:8080/server.rem");
      

  3.   

    hdt,正确,实例化时,不能实例接口,要实例化ImplementionClass
      

  4.   

    不对,不是应该是这个原因,因为在用,下面的代码运行是正确的:
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem");
    Console.WriteLine("Server responds: " + remoteObject.HelloWorld("Hi Server"));其中的HelloWorld在ImplementionClass中是这样定义的:
    public string HelloWorld(string input)
    {
             string identity = Thread.CurrentPrincipal.Identity.Name;
    return "Hello to you too, " + identity;
    }而且我在ISharedInterface接口和Implemention类中增加了一个返回double类型的方法:
    public double Add(double a, double b)
    {
    return a + b;
    }
    在Client类中:
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem");
    Console.WriteLine("{0} add {1} is: {2}", 10.1, 9.3, remoteObject.Add(10.1, 9.3));
    这样引用,也是正确的,
    现在的问题是,当返回类型是string类型时是正确的,但DataSet类型就找不到反序列化方法。
    为什么DataSet不能反序列化?
      

  5.   

    我发现问题是在DataSet的继承上,如果我在ImplementionClass中创建一个DataSet,然后返回,运行正确,如下代码:
    ISharedInterface,增加:
    DataSet GetDataSet();ImplementionClass增加:
    public DataSet GetDataSet()
    {
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("CustomerName", typeof(string)));
    dt.Columns.Add(new DataColumn("CustomerBirthday", typeof(DateTime)));
    DataRow dr = dt.NewRow();
    dr[0] = "bvw";
    dr[1] = DateTime.Now;
    dt.Rows.Add(dr);
    ds.Tables.Add(dt);
    return ds;
    }
    Client中还是用Interface来引用:
    ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(typeof(ISharedInterface), "tcp://localhost:8080/server.rem");
    DataSet ds = remoteObject.GetDataSet();
    Console.WriteLine("Customer's Name is: {0}, birthday is: {1}", ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString());这样的话运行就是正确的,
    现在的问题就是,从DataSet继承的类是不是还得自己重写Serialize方法?