Remoting and circular dependencies

  • Thread starter Thread starter nyhetsgrupper
  • Start date Start date
N

nyhetsgrupper

I have written a windows service and want to expose a web based user
interface for this service. I then wrote a class library containing a
..net remoting server. The class library have a method named
StartRemotingServer(). To be able to call this method from the windows
service I need to reference the remoting class library, but for the
class library to be able to access the internal structures of the
windows service the class library needs to reference the windows
service. Since circular dependecies is not allowed this does not work.
Some of you may tell me there is something wrong with my design,
because circular dependencies should not be necessary, but please give
me an advice on how to change my design. I have head that one method to
resolve circular dependencies is to use reflection. Can anyone show me
some example code to resolve circular dependecies using reflection?

Thank you!
 
I think you've hit the nail on the head, it certainly sounds like a
design issue. I need a little more detail though. What in the service
does the remoting library require? Could you perhaps resolve the
dependency by creating a new class in the remoting library that the
host can populate with session data and that can raise events, that
sort of thing?
 
Hi,

You could place the "internal structures" in another library project. Each
of the two assemblies can reference it.

Normally, the library is the third project since it doesn't provide a
graphical interface. Then, the interface is provided by a WinForms or
ASP.NET application that can reference the library. Four assemblies isn't
required to alleviate the circular dependency, so I'd be curious to your
design as well if you think you'll need another project, but I wouldn't
count that out just yet.
 
The service is controlling some robots. They are moving very fast and
the windows service keeps track of where they are, and avoids
collisions. I want to create a web-page where you can see all the robot
positions. To make this possible i make a remoting service whit one
method GetRobotPosition(int robotNo). This method is calling a similar
method in the windows service to get the position of the robot. The web
page, which is a seperate project, is connecting to the remoting server
and calls GetRobotPosition. The remoting server needs to know about the
windows service to get the position of the robots, and the windows
service need to know about the remoting server to be able to start/stop
it. Can someone give me a concrete example on how to solve this. I want
my code to be easy to read. Maybe the simplest solution is just to
merge the windows service and the remoting server into one assembly,
but I don't want to do that as it shouldn't be necessary. If anyone can
show me how to solve this using reflection i would be very happy.

Thank you!

DeveloperX skrev:
 
Hi,

You don't need to use Reflection.

If you don't want the Remoting server in the Windows Service Project
(simplest approach), then you can create another project that contains the
actual Windows Service implementation:

Windows Service Project
References: RobotsService Project (new)
References: Remoting Project

class Service : ServiceBase
{
private RobotsService robotsService;
private RemoteServer remotingServer;

protected override void OnStart(string[] args)
{
robotsService = new RobotsService();
robotsService.Start();

// TODO: remoting config
remotingServer = new RemoteServer();
remotingServer.Start(robotsService);
}
}


Remoting Project
References: RobotsService Project (new)

public class RemoteServer : MarshalByRefObject
{
private RobotsService service;

public void Start(RobotsService service)
{
this.service = service;
}

public Point GetRobotPosition(int robotNo)
{
return service.GetRobotPosition(robotNo);
}
}


RobotsService Project (new project)
References: None

public class RobotsService
{
public void Start()
{
// actual service implementation
}

public Point GetRobotPosition(int robotNo)
{
return Point.Empty;
}
}
 
The service is controlling some robots. They are moving very fast and
the windows service keeps track of where they are, and avoids
collisions. I want to create a web-page where you can see all the robot
positions.

Just a brief pointer in case you didn't know... the Microsoft Robotics
SDK is out (as a Community Tech Preview). It has a .net API and lots
of c# examples. And it uses web-pages to monitor your robots. Might be
fun.

http://msdn.microsoft.com/robotics/
 
Has anyone built a Terminator or Robocop yet?

- just wondering how far along we are ;)
 
Thank you Dave! Very well explained. I'll try this aproach.


Dave said:
Hi,

You don't need to use Reflection.

If you don't want the Remoting server in the Windows Service Project
(simplest approach), then you can create another project that contains the
actual Windows Service implementation:

Windows Service Project
References: RobotsService Project (new)
References: Remoting Project

class Service : ServiceBase
{
private RobotsService robotsService;
private RemoteServer remotingServer;

protected override void OnStart(string[] args)
{
robotsService = new RobotsService();
robotsService.Start();

// TODO: remoting config
remotingServer = new RemoteServer();
remotingServer.Start(robotsService);
}
}


Remoting Project
References: RobotsService Project (new)

public class RemoteServer : MarshalByRefObject
{
private RobotsService service;

public void Start(RobotsService service)
{
this.service = service;
}

public Point GetRobotPosition(int robotNo)
{
return service.GetRobotPosition(robotNo);
}
}


RobotsService Project (new project)
References: None

public class RobotsService
{
public void Start()
{
// actual service implementation
}

public Point GetRobotPosition(int robotNo)
{
return Point.Empty;
}
}

--
Dave Sexton

The service is controlling some robots. They are moving very fast and
the windows service keeps track of where they are, and avoids
collisions. I want to create a web-page where you can see all the robot
positions. To make this possible i make a remoting service whit one
method GetRobotPosition(int robotNo). This method is calling a similar
method in the windows service to get the position of the robot. The web
page, which is a seperate project, is connecting to the remoting server
and calls GetRobotPosition. The remoting server needs to know about the
windows service to get the position of the robots, and the windows
service need to know about the remoting server to be able to start/stop
it. Can someone give me a concrete example on how to solve this. I want
my code to be easy to read. Maybe the simplest solution is just to
merge the windows service and the remoting server into one assembly,
but I don't want to do that as it shouldn't be necessary. If anyone can
show me how to solve this using reflection i would be very happy.

Thank you!

DeveloperX skrev:
 
I have written a windows service and want to expose a web based user
interface for this service. I then wrote a class library containing a
.net remoting server. The class library have a method named
StartRemotingServer(). To be able to call this method from the windows
service I need to reference the remoting class library, but for the
class library to be able to access the internal structures of the
windows service the class library needs to reference the windows
service. Since circular dependecies is not allowed this does not work.
Some of you may tell me there is something wrong with my design,
because circular dependencies should not be necessary, but please give
me an advice on how to change my design. I have head that one method to
resolve circular dependencies is to use reflection. Can anyone show me
some example code to resolve circular dependecies using reflection?

Thank you!

Interesting approach you have there ... Question: is it possible to
abstract the logic causing the circular reference to another assembly?
 

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

Back
Top