[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

Ask for help on returning a DataSet from a remoting component

Lin Qi

6/15/2004 7:33:00 AM

Dear All

Iâ??m just getting into remoting and developed a client/server model to access the database. This model included the server component (*.dll), server application (the host, *.exe) and client application (*.exe). The model can run well on the development environment (namely in one computer), but an unhandled exception of type â??System.InvalidCastExceptionâ?? occurred in mscorlib.dll in the client/server environment when the client called the method that return a DataSet. Additional information is 'Return argument has an invalid type'. If calling the method that return a string type, it is OK. The above scenario occurs with .NET Framework 1.1.

Can anyone help me solve this problem? Thank you very much.

Best Regards
Lin Qi

I list the code as follows:

//MyServer.cs

using System;
using System.Data;
using System.Data.OleDb;

namespace MyServer
{
public interface IRemoteServer
{
string GetTest(string request);
DataSet GetDataSet();
}

public class RemoteServer : MarshalByRefObject, IRemoteServer
{
public string GetTest(string request)
{
return "Reply to " + request;
}

public DataSet GetDataSet()
{
DataSet ds = null;
string strConnect =
"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=yanjun;Initial Catalog=Northwind;Data Source=LINQI";
OleDbConnection conn = new OleDbConnection(strConnect);

try
{
conn.Open();
string strSQL = "SELECT CustomerID,ContactName FROM Customers";
OleDbDataAdapter sda = new OleDbDataAdapter(strSQL,conn);
ds = new DataSet();
sda.Fill(ds,"Customers");
}
finally
{
conn.Close();
}
return ds;
}
}
}



//TestApp.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using System.Reflection;
using MyServer;

namespace TestApp
{
class TestApp
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(8100);
ChannelServices.RegisterChannel(tcpChannel);
Type serverType = typeof(RemoteServer);
RemotingConfiguration.RegisterActivatedServiceType(serverType);
RemotingConfiguration.RegisterWellKnownServiceType(serverType,"RemoteServer",WellKnownObjectMode.Singleton);

// Keep the Server alive to respond to requests
Console.WriteLine("Server running and waiting for requests ...");
Console.WriteLine("Press [ENTER] to shut down Server");
Console.ReadLine();
}
}
}

//MyClient.cs
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;
using System.IO;
using System.Reflection;
using MyServer;

namespace MyClient
{
class ShowDataSet
{
// Remoting client
// You must add a reference to MyServer, and to System.Runtime.Remoting

[STAThread]
static void Main(string[] args)
{
IChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel);

try
{
// Get a proxy to the server object
//server runs on the different computer from the client
string url = "tcp://ccserver:8100/RemoteServer";
//server runs on the same computer as the client
//string url = "tcp://localhost:8100/RemoteServer";

Type serverType = typeof(RemoteServer);
RemoteServer dss = (RemoteServer)Activator.GetObject(serverType,url);

// Call the method on the proxy
string test = dss.GetTest("test");
DataSet ds = dss.GetDataSet();

// Write out the results
PrintDataTable(ds.Tables[0]);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("\r\nPress enter to exit");
Console.ReadLine();
}

// Writes out the passed DataTable
public static void PrintDataTable(DataTable dt)
{
foreach(DataRow dr in dt.Rows)
{
// Step through each column in the row and write it out
foreach(DataColumn dc in dt.Columns)
{
if (dc.Ordinal > 0)
Console.Write(", ");
if (dr.RowState == DataRowState.Deleted)
{

// For Deleted rows, need to access the original value
Console.Write(dr[dc,DataRowVersion.Original].ToString().Trim());
}
else
{
// Otherwise, just write out the regular version
Console.Write(dr[dc].ToString().Trim());
}
}
Console.WriteLine("");
}
}
}
}