By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,779 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,779 IT Pros & Developers. It's quick & easy.

Connecting to Remote Object from aspx Webpage

P: n/a
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?
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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)

"Jim Bayers" <sp**@spamity.spam> wrote in message
news:Xn*****************************@207.46.248.16 ...
| 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?
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.