Dear all:
Can any expert help me? urgently?
I build a server class, with "CallContext"
remoting method, I can run
my client.exe in windows successfully, but I cannot run it
in asp.net
from webBrowser IE6.0( where I also defined a class almost
same as
client.exe, except all the program code is not in main()
method, but in
a public method which is not static),
my code is as follows:
spService.cs(to build spService.dll)
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Channels;
using System.Security.Principal;
using System.Security.Permissions;
using System.Web;
using System.Web.Configuration;
namespace sp
{
namespace common
{
public class ContextBoundType : MarshalByRefObject
{
private DateTime starttime;
public ContextBoundType()
{
Console.WriteLine("A ContextBoundType
instance has been
created.");
starttime = DateTime.Now;
}
~ContextBoundType()
{
Console.WriteLine("ContextBoundType
being collected
after " + (new TimeSpan(DateTime.Now.Ticks -
starttime.Ticks)).ToString() + " seconds.");
}
public DateTime GetServerTime()
{
Console.WriteLine("Time requested by a
client.");
// This call overwrites the client's
// CallContextString.
//
CallContext.SetData("ServerThreadData", new
sendData("This is the server side
replacement."));
return DateTime.Now;
}
public bool closeServer(bool closeFlag)
{
if (closeFlag)
{
for (int i = 0; i < 1000; i++)
GC.Collect(i);
}
return (true);
}
//[PermissionSet(SecurityAction.LinkDemand)]
public System.Data.DataSet SetDataSource(string
ec,string
SqlTxt)
{
Console.WriteLine("DB requested by a
client.");
CallContext.SetData(ec, new
spDataTransfer(ec.Trim() +
"DS"));
spDataTransfer ds =
(spDataTransfer)(CallContext.GetData(ec));
//ds.SQL = SqlTxt;
Console.WriteLine("db after requested
by a client : " +
ds.spDataSource.DataSetName);
return (ds.spDataSource);
}
}
// One method of communicating between client and
server is
// to use the CallContext. Calling CallContext
essentially
puts the data
// in a Thread Local Store. This means that the
information is
available
// to that thread or that "logical"
thread (across application
domains) only.
[Serializable]
public class spDataTransfer :
ILogicalThreadAffinative
{
String _str = "";
string _PsId = "";
string _SQL = "";
public string PsId
{
get
{
return (_PsId);
}
set
{
_PsId = value;
}
}
public string SQL
{
get
{
return (_SQL);
}
set
{
_SQL = value;
}
}
//System.Data.DataSet DataSource;
public spDataTransfer(String str)
{
_str = str;
this.PsId = str;
//Console.WriteLine("CallContextString
created.");
}
public override String ToString()
{
string GlbDBstring =
WebConfigurationManager.AppSettings["GlobalDB"].
ToString();
return GlbDBstring;
}
public System.Data.DataSet spDataSource
{
get
{
return (getDataSource(PsId));
}
}
public System.Data.DataSet getDataSource(string
refId)
{
System.Data.DataSet newDs = new
System.Data.DataSet(refId);
System.Data.DataTable newDT = new
System.Data.DataTable();
newDT.Columns.Add(new
System.Data.DataColumn("T1",
System.Type.GetType("System.Int32")));
System.Data.DataRow dr = newDT.NewRow();
dr[0] = 32;
newDT.Rows.Add(dr);
newDs.Tables.Add(newDT);
return (newDs);
}
}
}
}
spServer.cs : (to build spserver.exe)
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace sp
{
namespace common
{
public class spServerProcess
{
public static void Main(string[] Args)
{
HttpChannel channel = new HttpChannel(8082);
ChannelServices.RegisterChannel(channel,
false);
WellKnownServiceTypeEntry WKSTE = new
WellKnownServiceTypeEntry(typeof(sp.common.ContextBoundType)
,
"HttpService", WellKnownObjectMode.Singleton);
//RemotingConfiguration.ApplicationName =
"HttpService";
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(
sp.common.ContextBoundType),
"HttpService",WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType(WKSTE);
Console.WriteLine("Press enter to stop
this process.");
Console.ReadLine();
}
}
}
}
Client.cs: ( to build client.exe)
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Security.Permissions;
using System.Data;
using sp.common;
namespace sp
{
namespace webAsp
{
public class spClientProcess
{
// [SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] Args)
{
string indId = "inited";
try
{
indId = "creatChanel";
HttpChannel channel = new
HttpChannel();
ChannelServices.RegisterChannel(channel,
false);
WellKnownClientTypeEntry remotetype
= new
WellKnownClientTypeEntry(typeof(sp.common.ContextBoundType),
"http://localhost:80
82/HttpService");
RemotingConfiguration.RegisterWellKnownClientType(remotetype
);
// This sets a CallContextDataSlot on
this thread.
// CallContextString will convert the
ThreadLocalDataStore into
// a property on the message, which will
then be
merged into the
// ThreadLocalDataStore on the server.
This is a
nice way to pass
// information from client to server
threads and
back again.
// NOTE: This data store has logical
thread scope.
If an
// application domain has more than one
thread
making calls,
// each thread will have its own logical
CallContext, and the
// server thread will keep each
separate.
/*
*
*
CallContext.SetData("ServerThreadData", new
CallContextString("This is the thread data inserted on
the client
thread."));
*
Console.WriteLine("CallContextString prior to
the call: " +
CallContext.GetData("ServerThreadData").ToString
());
* ContextBoundType service = new
ContextBoundType();
* Console.WriteLine("Server time
is: " +
service.GetServerTime().ToLongTimeString());
*
Console.WriteLine("CallContextString after the
call: " +
CallContext.GetData("ServerThreadData").ToString
());
* // create an instance on the
remote server
// and call a method remotely
Reverser rev =
(Reverser)Activator.GetObject(
typeof(Reverser),"http://localhost:
8000/Reverser.soap" );
*/
string cks = "Test";
if (cks == "")
cks = "Xa";
indId = "createds";
System.Data.DataSet ds = new DataSet();
spDataTransfer sd = new
spDataTransfer(cks);
indId =
"createcontextsetData";
CallContext.SetData(cks, sd);
indId =
"createdContxtGetData";
sd =
(spDataTransfer)(CallContext.GetData(cks));
indId = "createcontextType";
ContextBoundType service = new
ContextBoundType();
indId =
"createbyFuncCalling";
//string ias =
service.GetServerTime().Second.ToString();
ds =
service.SetDataSource(cks,"test");
indId =
"CallServiceSucceed";
ds = sd.spDataSource;
indId =
"runSPtransferSucced";
ds = sd.getDataSource(cks);
indId = "AllSucceed";
ds.DataSetName = indId;
Console.WriteLine("succeed!");
}
catch (Exception e)
{
Console.WriteLine(indId + e.Message);
}
//return (null);
}
}
}
}
spClient.cs ( to build spClient.dll, then I call it from
some other asp
page):
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Security.Permissions;
using System.Data;
using sp.common;
namespace sp
{
namespace webAsp
{
public class spClientProcess
{
// [SecurityPermission(SecurityAction.Demand)]
public System.Data.DataSet getDataSource(string
refId)
{
string indId = "inited";
try
{
indId = "creatChanel";
try
{
HttpChannel channel = new
HttpChannel();
ChannelServices.RegisterChannel(channel,
false);
WellKnownClientTypeEntry
remotetype = new
WellKnownClientTypeEntry(typeof(sp.common.ContextBoundType),
"http://localhost:80
82/HttpService");
RemotingConfiguration.RegisterWellKnownClientType(remotetype
);
}
catch
{
}
// This sets a CallContextDataSlot
on this
thread.
// CallContextString will convert the
ThreadLocalDataStore into
// a property on the message, which will
then be
merged into the
// ThreadLocalDataStore on the server.
This is a
nice way to pass
// information from client to server
threads and
back again.
// NOTE: This data store has logical
thread scope.
If an
// application domain has more than one
thread
making calls,
// each thread will have its own logical
CallContext, and the
// server thread will keep each
separate.
/*
*
*
CallContext.SetData("ServerThreadData", new
CallContextString("This is the thread data inserted on
the client
thread."));
*
Console.WriteLine("CallContextString prior to
the call: " +
CallContext.GetData("ServerThreadData").ToString
());
* ContextBoundType service = new
ContextBoundType();
* Console.WriteLine("Server time
is: " +
service.GetServerTime().ToLongTimeString());
*
Console.WriteLine("CallContextString after the
call: " +
CallContext.GetData("ServerThreadData").ToString
());
* // create an instance on the
remote server
// and call a method remotely
Reverser rev =
(Reverser)Activator.GetObject(
typeof(Reverser),"http://localhost:
8000/Reverser.soap" );
*/
string cks = refId;
if (cks == "")
cks = "Xa";
indId = "createds";
System.Data.DataSet ds = new DataSet();
spDataTransfer sd = new
spDataTransfer(cks);
indId =
"createcontextsetData";
CallContext.SetData(cks, sd);
indId =
"createdContxtGetData";
sd =
(spDataTransfer)(CallContext.GetData(cks));
indId = "createcontextType";
ContextBoundType service = new
ContextBoundType();
indId =
"createbyFuncCalling";
//string ias =
service.GetServerTime().Second.ToString();
ds =
service.SetDataSource(cks,"test");
indId =
"CallServiceSucceed";
ds = sd.spDataSource;
indId =
"runSPtransferSucced";
ds = sd.getDataSource(cks);
indId = "AllSucceed";
ds.DataSetName = indId;
return (ds);
}
catch (Exception e)
{
string ss = e.Message;
System.Data.DataSet newDs = new
System.Data.DataSet(refId);
/*
System.Data.DataTable newDT = new
System.Data.DataTable();
newDT.Columns.Add(new
System.Data.DataColumn("T1",
System.Type.GetType("System.String")));
System.Data.DataRow dr = newDT.NewRow();
dr[0] = "except happened";
newDT.Rows.Add(dr);
newDs.Tables.Add(newDT);
*/
newDs.DataSetName = indId +
"_err";
return (newDs);
}
//return (null);
}
}
}
}
the code I call spClient.cs ( spClient.dll):
spPageBase.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sp.common;
namespace sp
{
namespace webAsp
{
/// <summary>
/// Summary description for spPageBase
/// </summary>
public class spPageBase : Page
{
private DataSet ds;
public DataSet spDataSource
{
get
{
return (ds);
}
set
{
ds = value;
this.DataBind();
}
}
public spFuncLib funcLib = new spFuncLib();
string s_id = "";
public string PsId
{
get
{
return (s_id);
}
set
{
string oldId = s_id;
s_id = value;
if (oldId != s_id)
{
try
{
this.reinitialize();
}
catch //(Exception e)
{
this.Response.Write(this.Title +
" has
error while initialization!" +
"<br>" + "Please correct error and
try
again!");
s_id = "";
}
}
}
}
public spPageBase()
{
//
// TODO: Add constructor logic here
//
}
protected void reinitialize()
{
//funcLib.initialize(this);
this.PsId = "Test";
spClientProcess newspClient = new
spClientProcess();
this.Title = "Before run get
DS";
//spClientProcess.getDataSource(this.PsId);
this.spDataSource =
newspClient.getDataSource(this.PsId);
//if (this.spDataSource == null)
// {
this.Title = this.spDataSource.DataSetName ;
// }
}
}
}
}
and the asp PAge(a Blank page only with some text and the
title)
Service.aspx.CS:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sp.webAsp;
using sp.common;
public partial class Services : spPageBase
{
protected new void Page_Load(object sender, EventArgs e)
{
int i = 0;
this.reinitialize();
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C# Developers" group.
To post to this group, send email to CSDevelopers googlegroups.com
To unsubscribe from this group, send email to
CSDevelopers-unsubscribe googlegroups.com
For more options, visit this group at http://gr
oups.google.com/group/CSDevelopers
-~----------~----~----~----~------~----~------~--~---
|