none
Can I call a method directly from an object in WCF RRS feed

  • Question

  • Hi,

    I'm trying to find out if it is possible to call methods directly on objects within a WCF service without using the service client. Much the same way that you would do by applying the webmethod attribute. I can create an instance of a class from my service reference, for example:

    ServiceReference.MyClass class = new ServiceReference.MyClass();

    MyClass is attributed with [ServiceContract] and the method I want to consume is attributed with [OperationContract]

    When I do this I can see any properties on the object but I can't see any of the methods. I've spent a day looking for an answer on this but so far no joy. Must I always surface these methods through the service client interface or is what I'm trying to do in any way possible?

    Many Thanks


    Wednesday, March 27, 2013 10:08 AM

All replies

  • Hi,

    >>I'm trying to find out if it is possible to call methods directly on objects within a WCF service without using the service client.

    Can you provide some code snippets here to better explain your issue?

    You can utilize WcfTestClient.exe to test the service without creating a client application.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 6:06 AM
    Moderator
  • Hi,

    Thanks for the prompt reply.

    I already have a WCF service up and running and a client that is consuming it and its all working fine. I'm consuming a couple of methods surfaced through the interface  OK. What I would like to know is this. With a standard web service you can create an instance of a class on the client side code by creating an instance of the type by using the web service reference. So:

    WebServiceReference.MyClass myClassInstance = new WebServiceReference.MyClass();

    WebServiceReference.MyClass has a method on it called DoSomething() which is attributed with the [WebMethod] attribute.

    So now I can implement myClassInstance.DoSomething() on the client side without having to expose it through the interface.

    What I would like to know is if this is possible with a WCF service, and if so how? Can I create a

    WCFServiceReference.MyClass myWCFClassInstance = new WCFServiceReference.MyClass()

    and the call myWCFClassInstance.DoSomething()

    Best Regards

    Thursday, March 28, 2013 8:08 AM
  • I believe you have to get the singleton instance of the service in order to call its operationcontract directly without creating a proxy to it.  This of course is from within the application hosting the service.

    To explain a little further, the ServiceHost has a SingletonInstance property.  It is from there you can call your methods directly.  This in turn means that your WCF service must be attributed with the Singleton attribute, like this:

        [ServiceBehavior(
            ConcurrencyMode = ConcurrencyMode.Multiple,
            InstanceContextMode = InstanceContextMode.Single
        )]

    Thursday, March 28, 2013 7:55 PM

  • Yeah that's right. In this situation, you can only see the public properties of the object, and all public methods/behavior are dropped from the object. Really, when WCF sends or receives a XML serialized object,  all there is are the public properties that are being passed. A serialized object has its methods/behavior dropped, until it is cast back to an instance of an object type.

    If you were to XML serialize the object you talk about and wrote the xml to a file, all you are going to see are the public properties and the data that is contained within the properties. WCF is doing XML serialization of objects when the object passes through WCF.

    You can't address the object, unless it's an instance of the object that is in memory, even if you did that "New", becuase where you got it from was on the other side of the WCF service, so all you are going to get are the properties when you did a "new".

    It doesn't matter if it's .NET, Java or any other OO programming language, because OOPs is OOPs.
     The "New" you are doing is never going to work in picking up the object's behavior on the other side of the WCF service.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Thursday, March 28, 2013 11:22 PM

  • Oh, one other thing here. If you want a new version of the object on the client-side and the class/object only exist on the service-side, then you have to create the object "new" on the service-side. And then send the object back, and it will have its properties and behavior/methods. I believe that's how I did it a long time ago, so don't hold me to it.

    Thursday, March 28, 2013 11:57 PM
  • Thanks for replying. Unfortunately this doesn't seem to be working for me. I'm still getting the same issue where no methods are visible on the client side.

    Tuesday, April 2, 2013 9:10 AM
  • Hi,

    Many thanks for the detailed response. I'm fairly au-fait (I hope) with OOP principles and everything that you say makes perfect sense regarding serialisation. Not sure I expressed the problem very clearly. The point I still fail to understand is that this behaviour was and still is possible on a standard web service. I'm just trying to see if I can do the same in WCF. I think that as I have the service up and running and can consume methods through the interface that I'll stick with that. At least it works.

    Thanks again.


    Tuesday, April 2, 2013 9:18 AM
  • If the service is not a singleton, what you want to do will work so long as you do not require any data persistance. 

    On a percall session, everytime a call is made to a method of your service, a new instance of the service is created.  You can confirm this by placing a breakpoint on your service constructor, and then calling a method of your service contract via a client.

    Conversely if you decorate your service as a singleton, then the only time it is instantiated is when the service is first created.  All subsequent calls use the same instance.  Therefore you can persist data.

    Tuesday, April 2, 2013 6:07 PM
  • Hi,

    Thanks for elaborating. I'm still not getting the result. Do you mind if I take my understanding of your explanation form the beginning just to see where I'm failing to get it?

    So I have my class: public class ServiceClass: IServiceClass.

    this is adorned with the attribute you provided earlier. I publish the updated service and update the service reference on the client side.

    Normally I would create the service reference client and access the methods through the interface and all that works fine.

    But if I type ServiceReferenceName all of my business objects server side are visible through intellisense on the client. I should then be able to create an instance of ServiceReferenceName.SpecificBusinessClass on the client side and see the public methods via intellisense? Then go on to create the instance and call the methods? Is that correct. If so, one question that does come to mind if you don't mind my asking is how should the specific server side business object be adorned whose method I want to access. At present it's [DataContract] with [OperationContract] on the method in question. Maybe that's where I'm getting it wrong?

    Thanks.

     

    Wednesday, April 3, 2013 3:16 PM