Interfaces in a separate assembly...remoting

J

Junoti

Hello, I'm hoping someone might be able to help me out. I'm creating a
shared assembly with my interface definitions similar to the following.
When I try to compile the RemotingInterface, everything works. When I
go to compile the RemotingServer assembly, I receive the following
error message. I've included all my references, so I'm almost positive
that's not the issue. Shouldn't this be possible without having to
implement HelloMessage in the RemotingInterface assembly?

Error 2 'RemotingServer.SampleService' does not implement interface
member
'RemotingInterface.ISampleService.HelloWorld(RemotingInterface.IHelloMessage)'

namespace RemotingInterface
{
public interface IHelloMessage
{
string Message
{
get;
set;
}
}
public interface ISampleService
{
//If I have HelloMessage defined in the same assembly, I change
//IHelloMessage to HelloMessage and
everything works fine.
IHelloMessage HelloWorld(IHelloMessage
message);
}
}

Now, in my server assembly, I have the following:
namespace RemotingServer
{
public class SampleService : MarshalByRefObject, ISampleService
{
//This method declaration throws:
//'ISampleService.HelloWorld' in
explicit interface declaration is not a member of interface
//Shouldn't RemotingServer.HelloMessage
is RemotingInterface.IHelloMessage return true?
HelloMessage
ISampleService.HelloWorld(HelloMessage message)
{
HelloMessage response = new HelloMessage();

return response;
}
}

public class HelloMessage : IHelloMessage
{
public string Message
{
get
{
return "Hello";
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
}
}
 
M

Marc Gravell

HelloMessage ISampleService.HelloWorld(HelloMessage message) {
HelloMessage response = new HelloMessage();
return response;
}

Shouldn't that be IHelloMessage at the start? Otherwise it (as stated)
doesn't meet the interface definition. It doesn't matter that HelloMessage :
IHelloMessage; the signature must match.

Marc
 
J

Junoti

Yeah, IHelloMessage works. So if I have properties that return
interface types, I have to go through the exercise of casting each one
to its specific type, i.e.

public interface SomeInterface
{
IAnotherInterface SomeProperty
{
get;set;
}
}

And then in my implementation, I would do something like:

SpecificImplementation _var =
(SpecificImplementation)IAnotherInterface.SomeProperty


Is this correct? Thanks for the help, I appreciate it.
 
M

Marc Gravell

Well, only if you care about the bits in the specific implementation ;-p
Most of what you need may be (or perhaps /should be/) available in
IAnotherInterface. After all, if you are binding to a specific
implementation, why bother with an interface at all?

In /some/ circumstances, a generic interface may be appropriate... so you
could have

public interface ISomeInterface<T> where T : IAnotherInterface {
T SomeProperty {get; set;}
}

However, this is again blurring the abstraction that interfaces are meant to
provide, so treat with caution. However, if you know that something
implements IAnotherInterface<SomeImplementation>, then you can call
SomeImplementation result = i.SomeProperty;

Marc
 
J

Junoti

Marc,
Thanks for your help. I completely forgot that I could do

SomeImplementation result = i.SomeProperty;

The reason for defining the interfaces to begin with, was to be able to
use the shared assembly in remoting...which I understood was the method
to use.

Thanks again...
 
G

Guest

You don't "Have to" use Interfaces for Remoting, as long as both client and
server have a reference to the same assembly (so they know "what" to remote).
However, using an Interface does make things more extensible.
Peter
 

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

Top