tags:

views:

321

answers:

3

Say I have the following:

namespace SomeProject
{
    public interface ISomething
    {
        string SomeMethod();
    }
}

namespace SomeService
{
    [DataContract]
    public class CompositeType
    {
        [DataMember]
        public ISomething Something { get; set; }
    }
}

Now, in my calling code I want to be able to do this:

SomeService service = new SomeService();
service.Something.SomeMethod();

SomeMethod() is not available unless I return the DataMember as the implementation rather than the interface. Is there a way to do this?

+3  A: 

Your WCF "DataContract" is exactly that - a data contract. On your client side you have a proxy object that implements all of the data members, but none of the methods. You'd have to have the original class/interface definitions and reconstruct the full object on the client side in order to do something like this.

GalacticCowboy
Exactly... WCF is a *messaging* technology. Focus on sending messages back and forth, not calling methods. The method calling metaphor is just an abstraction. It's an unfortunate one because it leads to misunderstandings like this.
Anderson Imes
+2  A: 

This is not how you want to use your WCF service. WCF is about transferring data, not implementation. You are confusing your client and your service layers by doing this.

However, if you really want to do this, you can tell the proxy generator in the client to re-use any existing types... this means that you can

  1. reference the "SomeProject" dll in your client
  2. add the service reference
  3. choose "Advanced"
  4. Select "Reuse types in referenced assemblies"
  5. Choose where you want to get the types from

Again, I do not recommend doing it this way.

Brian Genisio
I haven't used services much but it seems to me to defeat the purpose of a service if you have to reference the class directly just to call a method? I thought part of the reason for services was to allow you to reference the service and not be coupled to the assemblies. Seems like a bit of duplication to have to call a service which then calls class code where you have to pass an instance of the class to the service. Rather convoluted.
suedeuno
suedeuno: I completely agree. The functionality should be in the service, and the client should just consume data. The client shouldn't be then re-using the exact same class and calling methods on it. It completely pokes holes in the service oriented architecture to do this. As I put in bold: I do not recommend doing it this way.The correct way to do this is to add the service reference, and allow the proxy to be created with just the data. If you need more functionality, call back into the service.
Brian Genisio
I understand what you're saying but I'm not sure you understood my point. As it stands, if I want to retrieve a value I have to call the service which in turn has a reference to the class, but I have to pass an instance of that class to the service just so I can get some value. In a layered achitecture I would simply just call myclassinstance.IsValueAvailable() for instance. With a service, I have to call myservice.IsValueAvailable(myclassinstance) which then goes off to call the class for the value. You save an assembly reference from the calling code but it becomes less clean in doing so imo
suedeuno
You save the need for updating references in multiple projects but you end up writing more code (if you're the auther of both the client and the service). I was hoping it wouldn't be a big deal to call a class method directly through the service which would mean you don't have to reference the assembly directly yet you can more directly utilize that class code.
suedeuno
@suedeuno: WCF stands for Windows COMMUNICATION Framework. It is all about communication, not implementation. WCF cannot transmit functionality to the client... only data. If you want some utilities to exist in both the client and the server, then all the power to you... but it is not the design of WCF. Let me ask you this: If you wrote a client that connected to a web service like Ebay.com, would you expect the client to have direct access to the functionality of the objects hosted in the service?
Brian Genisio
OK, I think I understand your confusion... on the client, SomeService is a proxy that handles the behind-the-scenes communication. You are confusing your metaphors a bit as services are decorated with [ServiceContract] and the methods are decorated with [OperationContract]. This defines the actual functional service. Composite data that get returned implement [DataContract] and [DataMember]. In short, your class SomeService is not a service at all, but plain old data. There is no proxy inside of it... it is just data.
Brian Genisio
A: 

I strongly suggest you go through some basic WCF tutorials so that you can get a hang of what WCF actually does.

Check out the O'Reilly books, "Learning WCF" by Michele Bustamante and more importantly "Programming WCF Services 2nd Edition" by Juval Lowy.

WCF is a complicated beast and I suggest that you really need to get a firm foundation before you try writing code.

Terry Donaghe