Connecting to Remote Object from aspx Webpage

J

Jim Bayers

Our network nazi doesn't want us running a webserver on the server that
has a connection to the campus database so we created a remote object
that runs as a windows service. The windows service runs on the server
with a connection to the campus database and we make calls to it. My
question is, how best to connect to this object via asp.net, in an aspx
page?. The example I had used a console ap to connect and that works,
but we want to connect to the remote object from an asp.net web page.
Here is the remote object

using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace RemoteObject
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Relay : MarshalByRefObject
{
private SqlConnection mySqlConn = new SqlConnection("Network
Library=DBMSSOCN;Data Source=server;Initial Catalog=proddb;User
ID=user;Password=password;");
private SqlCommand mySqlComm = new SqlCommand();

public Relay()
{
//
// TODO: Add constructor logic here
//
}
public String GetTestString(String str)
{
return str;
}

public DataSet GetDataSet(String strSqlQry)
{
DataSet myDS = new DataSet();
SqlDataAdapter myDA = new SqlDataAdapter(strSqlQry,
mySqlConn);
myDA.Fill(myDS);
myDA.Dispose();
mySqlConn.Close();

return myDS;
}

public SqlDataReader GetDataReader(String strSqlQry)
{
mySqlComm.CommandText = strSqlQry;
mySqlComm.Connection = mySqlConn;
mySqlConn.Open();
SqlDataReader myDR = mySqlComm.ExecuteReader
(CommandBehavior.CloseConnection);

return myDR;
}

}

}

And the config file for the windows service looks like

<configuration>
<system.runtime.remoting>
<application name="RemoteHostService">
<service>
<wellknown type="RemoteObject.Relay, RemoteObject"
objectUri="RemoteObject.Relay"
mode="Singleton" />
</service>
<channels>
<channel ref="http" port="8085">
<serverProviders>
<formatter ref="soap" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

And here is a working example of the console ap that can connect
remotely:

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using RemoteObject;

namespace RemotingClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class RemoteClient
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
HttpChannel chan = new HttpChannel();
ChannelServices.RegisterChannel(chan);

Relay r = (Relay)Activator.GetObject(
typeof(RemoteObject.Relay),
"http://server:8085/RemoteObject.Relay");
if (r == null)
System.Console.WriteLine("Could not locate
server");
else
{
System.Data.SqlClient.SqlDataReader _sqlReader;
_sqlReader = r.GetDataReader("select [Name] from
dbo.CMN_Terms WHERE CMN_TermsID=1");
_sqlReader.Read();
Console.WriteLine(_sqlReader["Name"].ToString
());

}

}
}
}

This works, but we've been having trouble connecting to the remote
object 'relay' from an aspx page which is what we need. How does one
connect from an aspx page to a remote object?
 
B

bruce barker

there should be no difference (unless the remote host is iis and is using
security). is the port blocked to the webserver? try to use the simpler tcp
transport.

also you should not pass a datareader reference, you should return a dataset
or datatable, or you'll end up with resource leaks. if the web page fails to
empty the reader, it will leave a dangling connection.

-- bruce (sqlwork.com)



| Our network nazi doesn't want us running a webserver on the server that
| has a connection to the campus database so we created a remote object
| that runs as a windows service. The windows service runs on the server
| with a connection to the campus database and we make calls to it. My
| question is, how best to connect to this object via asp.net, in an aspx
| page?. The example I had used a console ap to connect and that works,
| but we want to connect to the remote object from an asp.net web page.
| Here is the remote object
|
| using System;
| using System.Data;
| using System.Data.SqlClient;
| using System.Diagnostics;
|
| namespace RemoteObject
| {
| /// <summary>
| /// Summary description for Class1.
| /// </summary>
| public class Relay : MarshalByRefObject
| {
| private SqlConnection mySqlConn = new SqlConnection("Network
| Library=DBMSSOCN;Data Source=server;Initial Catalog=proddb;User
| ID=user;Password=password;");
| private SqlCommand mySqlComm = new SqlCommand();
|
| public Relay()
| {
| //
| // TODO: Add constructor logic here
| //
| }
| public String GetTestString(String str)
| {
| return str;
| }
|
| public DataSet GetDataSet(String strSqlQry)
| {
| DataSet myDS = new DataSet();
| SqlDataAdapter myDA = new SqlDataAdapter(strSqlQry,
| mySqlConn);
| myDA.Fill(myDS);
| myDA.Dispose();
| mySqlConn.Close();
|
| return myDS;
| }
|
| public SqlDataReader GetDataReader(String strSqlQry)
| {
| mySqlComm.CommandText = strSqlQry;
| mySqlComm.Connection = mySqlConn;
| mySqlConn.Open();
| SqlDataReader myDR = mySqlComm.ExecuteReader
| (CommandBehavior.CloseConnection);
|
| return myDR;
| }
|
| }
|
| }
|
| And the config file for the windows service looks like
|
| <configuration>
| <system.runtime.remoting>
| <application name="RemoteHostService">
| <service>
| <wellknown type="RemoteObject.Relay, RemoteObject"
| objectUri="RemoteObject.Relay"
| mode="Singleton" />
| </service>
| <channels>
| <channel ref="http" port="8085">
| <serverProviders>
| <formatter ref="soap" />
| </serverProviders>
| </channel>
| </channels>
| </application>
| </system.runtime.remoting>
| </configuration>
|
| And here is a working example of the console ap that can connect
| remotely:
|
| using System;
| using System.Runtime.Remoting.Channels;
| using System.Runtime.Remoting.Channels.Http;
| using RemoteObject;
|
| namespace RemotingClient
| {
| /// <summary>
| /// Summary description for Class1.
| /// </summary>
| class RemoteClient
| {
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
| [STAThread]
| static void Main(string[] args)
| {
| //
| // TODO: Add code to start application here
| //
| HttpChannel chan = new HttpChannel();
| ChannelServices.RegisterChannel(chan);
|
| Relay r = (Relay)Activator.GetObject(
| typeof(RemoteObject.Relay),
| "http://server:8085/RemoteObject.Relay");
| if (r == null)
| System.Console.WriteLine("Could not locate
| server");
| else
| {
| System.Data.SqlClient.SqlDataReader _sqlReader;
| _sqlReader = r.GetDataReader("select [Name] from
| dbo.CMN_Terms WHERE CMN_TermsID=1");
| _sqlReader.Read();
| Console.WriteLine(_sqlReader["Name"].ToString
| ());
|
| }
|
| }
| }
| }
|
| This works, but we've been having trouble connecting to the remote
| object 'relay' from an aspx page which is what we need. How does one
| connect from an aspx page to a remote object?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top