Wednesday, December 06, 2006

should this blog still be called .NET Webservices?

for some program's performance reason, i will recode dataset retuning webservices to .NET Remoting.


-----------------------------

the proxy(stub) code for middletier



using System;
using System.Data;

namespace SyerDll
{
public abstract class RemoteObjectDef : MarshalByRefObject
{
public abstract string Test();

public abstract DataSet ReturnHeader();
}
}

----------------------------------


-----------------------------

sample Client Code:

using System;
using System.Data;

using SyerDll;


class Mate
{


public static void Main()
{

Console.WriteLine("Creating Remote Object.");

string url = "tcp://192.168.10.254:8076/RemoteObjectXXX";
/*RemoteObjectDef ro = (SyerDll.RemoteObjectDef)Activator.GetObject(
typeof(SyerDll.RemoteObjectDef), url );*/

RemoteObjectDef ro = (SyerDll.RemoteObjectDef)Activator.GetObject(
typeof(SyerDll.RemoteObjectDef), url );


DataSet rs = ro.ReturnHeader();


for(int i = 0; i < rs.Tables[0].Rows.Count; i++)
Console.WriteLine(rs.Tables[0].Rows[i]["Lastname"].ToString());

Console.WriteLine("Remote Object says " + ro.Test() );
}

}

-----------------------------------------

sample Server Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using SyerDll;
using System.Data;


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;


namespace TheServer
{
public class RemoteObject : SyerDll.RemoteObjectDef
{
public override string Dancer()
{
return "UMD";
// throw new Exception("The method or operation is not implemented.");
}

public override DataSet ReturnHeader()
{
DataSet ds = new DataSet();
DataTable t = new DataTable();

t.Columns.Add("Lastname", typeof(string));

t.Rows.Add("Lennon");
t.Rows.Add("McCartney");
t.Rows.Add("Harrison");
t.Rows.Add("Starr");

// ds.Tables[0].Rows[0][0]
ds.Tables.Add(t);



return ds;

//throw new Exception("The method or operation is not implemented.");
}

public override string Test()
{
return "Hello mike" + DateTime.Now.ToString();
// throw new Exception("The method or operation is not implemented.");


}

static void Main(string[] args)
{


// Set up the configuration parameters through a dictionary
IDictionary properties = new Hashtable();
properties.Add("port", 8076);
properties.Add("secure", false);
// properties.Add("impersonate", false);

// Create an instance of a channel
TcpServerChannel serverChannel =
new TcpServerChannel(properties, null);
ChannelServices.RegisterChannel(serverChannel,false);
// ChannelServices.RegisterChannel(new TcpChannel(8076));
Type RemoteType = Type.GetType("TheServer.RemoteObject");

// RemotingConfiguration.RegisterWellKnownServiceType(RemoteType, "RemoteObjectXXX", WellKnownObjectMode.SingleCall);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(TheServer.RemoteObject), "RemoteObjectXXX", WellKnownObjectMode.SingleCall);

//while(true)
Console.WriteLine("Michael Buen");
Console.ReadLine();
}
}
}

No comments: