tags:

views:

173

answers:

8

I'm prototyping a C#/.NET client/server environment in which I need to return an object instance (already constructed with data the client needs- avoiding the exposure of server-side (read: internal database details) that the calling code needs to perform "remote" method calls on said instance.

When the client code calls methods on this object, they should run in the context of the Host process.

Would using .NET remoting, WCF, or..? be appropriate for this task? And more importantly, can anybody provide an example of this or point me to any relevant articles regarding how this is done?

Interface Example (WCF decorations included):

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IGroupContract
{
    [OperationContract]
    GroupInfo BoundToGroup();

    [OperationContract]
    Group GetEntryInfo();

    [OperationContract]
    Boolean RebindGroup(GroupInfo NewGroup);
}

[ServiceContract]
public interface IClientDatabase {
{
    [OperationContract]
    System.String[] FetchClsidList();

    [OperationContract]
    IGroupContract Create(System.String Name, System.String Group);

    [OperationContract]
    System.Boolean Delete(IGroupContract Grp);
}
+1  A: 

Yes, .NET remoting is A way to do it.

Here is a good tutorial:
http://www.codeproject.com/KB/IP/remotingchatsample.aspx

Robert Harvey
Alright; so I can just drop WCF support for this entirely, and have return an object instance compatible with the declaration of IGroupContract?Can I pass my IGroupContract around, as if it were a local object and call Delete(IGroupContract ...) the instance if I don't need it anymore?
robert_
I've done it that way, using a prototype class instance rather than an interface, but I don't see why an interface wouldn't work just as well, if not better.
Robert Harvey
And yes, that's a fair description of how it works.
Robert Harvey
I strongly disagree; remoting is largely deprecated and is not recommended (by MS) for new builds. WCF is the recommendation, for a *lot* of reasons.
Marc Gravell
The classes that do remoting are not marked deprecated, even in the latest version of the framework. Even C# still uses elements from version 1.0 of the framework. What's the real objection? I recommended remoting because I used it in a recent, real-world application to accomplish this, and it worked well; that doesn't mean that WCF might not be a better fit, especially since Robert is already using WCF.
Robert Harvey
@Robert: all of the Remoting functionality has been replaced by WCF. Note that MSDN appear to be in the process of updating the documentation in this area. Only recently was ASMX noted as being "Legacy" (http://msdn.microsoft.com/en-us/library/bb552872.aspx). Don't start off in the past.
John Saunders
I already posted a WCF-specific answer to his question.
Robert Harvey
Remoting is still use by certain parts of .NET though - e.g. making calls across AppDomains and inter-process communication. For very simple and "boilerplate" things like this Remoting is actually better suited than WCF.
NathanE
Right, communicating among AppDomains within a process is one of the few remaining cases where .NET Remoting is appropriate, since it's being used as a native, lightweight intra/inter-process communication method. For the reasons others have mentioned, I would not recommend it for RPC.
Steven Sudit
+1  A: 

If the object returned contains just properties, then you don't really want to use .NET remoting if you can avoid it. Plain old SOAP or WCF would work better.

Steven Sudit
I can convert the properties into method calls, if the need arises.
robert_
In my case, I was using .NET remoting to pass a class instance to a windows service executing a long-running process. You might be doing something a little different. WCF seemed a little top-heavy for what I was doing; I just wanted to remote-control the service box.
Robert Harvey
If the class instance contains state but no behavior, then there's no reason to use .NET remoting. WCF has a lot of functionality, but if you stick to the basics then it's just two attributes, not anything complex or heavy.
Steven Sudit
+3  A: 

I would avoid .NET Remoting. It's an older technology (personally, I would call it legacy) and it is very difficult to deal with in both development and deployment. You should definitely stick with WCF. There are some good introductory videos and sample applications for WCF here.

Edit: Per some of the discussions. I think you need to start looking at the problem in more of a service oriented way. The server will expose services that the client can take advantage of. The client has no interest in the implementation of these services. It just knows that it calls a service and some unit of work gets done (this can be course or fine depending on your design).

Objects on the client can easily abstract away the service calls, so you don't have to worry about not having a clean object model on the client and you can receive messages and events back from the server as well.

One of the best ways to accomplish a more robust client-server architecture using WCF and SOA is a publish-subscribe message bus. Here's a working implementation of a [pub-sub framework][2]. This will allow your client to register for and receive notifications back from the service. That completes the loop and gives you the full feel of .NET Remoting, but without all the headaches (and there are many). The benefit of WCF in a design like this is that all the pluming is abstracted away, that is any binding, channel and set of endpoints can be used and the code on the client and code on the server don't have to change.

JP Alioto
+1 for "No interest in the implementation of these services."
Robert Harvey
A: 

Defiantly avoid remoting, before WCF there were cases that remoting made sence but now there is no excuse for using that "legacy" technology. WCF gives you a lot more flexibility and is also much easier to maintain and work with.

here is a sample calculator app done in WCF which is hosted within a windows application

http://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic3

Keivan
Problem: Nobody can definitively tell me how one would export an object using a WCF service and have the methods run of said object run on the server.
robert_
One more thing: That MSDN sample is missing a fair bit of code.
robert_
I think your choice of words are confusing people a bit, when you say exporting an object. it implies that you are trying to transfer a brand new object, an object that the server doesn't know anything about to the server and the server execute it. based on my understanding you want to remotely execute a method on the server.
Keivan
i updated a link to a more detailed sample article, hope this helps.
Keivan
Oh, when I say "export", I really mean, exporting the structure of the object, as would be seen by code using it on the service. Also, my terminology tends to suck; My apoologies for being unclear.
robert_
+1  A: 

Well, fine. Since everyone thinks that Remoting is so 2008, here's a WCF example. This sample demonstrates how to use WCF (Windows Communication Foundation) to develop a typical client server application using TCP/IP binding.

http://www.dotnetspider.com/resources/19314-Client-server-sample-using-WCF-net-tcp-binding.aspx

...and this article from Microsoft has details on both approaches, and why you would use one over the other It also explains how one would export an object using a WCF service and have the methods run of said object run on the server:

From .NET Remoting to the Windows Communication Foundation (WCF)
http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic6

Robert Harvey
Oh, I know how to self-host a WCF service; that isn't the issue.. It's, "I need an object instance which is exportable by my service that can have its methods run on the server."
robert_
That information is in Section 3 of the second link, titled: Step 3 - Use Sessions instead of Client-Activated Objects
Robert Harvey
Direct link here: http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic6
Robert Harvey
Just so I'm clear, you're not trying to pass a brand new bit of code to a server and then execute it, are you? I doubt that is permissible (or advisable) from a security perspective. I assume that the bit of code you want to execute is already installed on the server.
Robert Harvey
No.. That kind of remote code execution would be extremely retarded. All of this code lives on the server. I just need to be able to "up-call" a method call from client -> server.
robert_
After looking at the msdn link, it seems that this was written for an earlier version of wcf; my [ServiceContract] attribute doesn't have a Session= parameter..
robert_
I posted a new answer that contains a complete, but simple walkthrough of all of this; it doesn't require a Session parameter.
Robert Harvey
Actually, Remoting is more of a 2002 thing, not 2008.
John Saunders
+1  A: 

Migrating .NET Remoting to WCF (and even ASMX!) (from the Mattavis Weblog)

Summary:

  1. Following the .NET Remoting guidance enables quick and painless upgrade to WCF
  2. A single interface can be decorated to support .NET Remoting, WCF and ASMX
  3. The service implementation does not need to change at all to support all three platforms
  4. Wrapping the client connection code in a proxy allows developers to switch between .NET Remoting, WCF and ASMX on-the-fly

I found this article to have remarkable clarity and insight.

Read on for the details…
http://blogs.msdn.com/mattavis/archive/2005/10/10/479280.aspx

Robert Harvey
+1  A: 

I believe you're correct that WCF does not provide the same concept of distributed object identity that Remoting did. You cannot actually return an object with methods and properties from the server. You cannot call the methods of such an object and have them execute on the server.

So what? That was an implementation detail of one particular technology (Remoting). I know other technologies also have that capability (CORBA, RPC). Again, so what? That has nothing to do with the question of the best way for you to achieve your goals.

If your goal is to use the same .NET types on the client and server, then you can use the NetDataContractSerializer, which serializes the full CLR type name into the XML. See Serialization and Deserialization to learn the differences between this class and the DataContractSerializer.

If your goal is to enable high-performance interaction, then WCF does that: you can use binary over TCP/IP, and can use duplex contracts to allow the service to call back into the clients.

Don't allow the difference in implementation details to fool you into using a technology that has been replaced, and which has no future.

John Saunders
I'm not sure that I buy into this argument that, just because a technology is "old" that it is no longer useful. Unix is "old." Software doesn't wear out like an automobile engine does. It just gets superceded by the next flavor of the week.
Robert Harvey
Anyway, I already posted a WCF-specific answer to his question.
Robert Harvey
This software has been designated by the vendor as having been superseded. That's totally different from Unix, which has not been. I haven't heard any official "support" statements, but do you really expect the same level of bug fixes from Microsoft on Remoting as opposed to WCF? ASMX as opposed to WCF? WSE at all? Remoting had issues. For better or worse, the issues were addressed in WCF. You've got to ask yourself why fight against the tide?
John Saunders
A: 

WCF really doesn't seem to want to do what you're looking for. It wants to expose services, not objects. This is clear pretty well throughout the API - you have ServiceContracts that return things that are marked as DataContracts. At no point does the concept of an "object" enter into discussion.

In general, I find it's better to not force APIs into patterns that they don't want to support - use something that wants to do what you want to do in the first place. If you really want to remote objects, .Net Remoting is probably the answer.

Personally, I'd recommend thinking more in terms of messaging, as that usually seems to work better in distributed environments. Distributed objects usually don't seem to work well - see Martin Fowler's First Law of Distributed Objects.

kyoryu