Results 1 to 11 of 11

Thread: WCF Accessing the class created at the Endpoint?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    WCF Accessing the class created at the Endpoint?

    Hi, Please be gentle, I'm new to WCF..

    I have a class that I create as an EndPoint for a Callback Interface. I create the object (called svh below) and it does what it needs to do. I have a method within that class called Subscribe and all clients register with this and I save-off the references into an ArrayList.

    However, my issue comes when I want to use the svh object to call another method on this class (topically, NotifySubscribers). How do I get to the serviceendpoint started class form outside ???

    Thanks

    Chubby:

    Code:

    Code:
    svh = new ServiceHost(typeof(EventCallbackPipe));
    svh.AddServiceEndpoint(typeof(IEventCallbackPipe), new NetTcpBinding(), "net.tcp://" + ChannelServer + ":" + ChannelPort);
                           
    svh.Open();

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: WCF Accessing the class created at the Endpoint?

    I'm not sure what you mean - how are you calling Subscribe()? What do you mean by "get to the serviceendpoint started class form outside"?

    Are you trying to preserve some sort of state between two calls or is one of them a callback event?

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF Accessing the class created at the Endpoint?

    If i understand you correctly - you dont. If you have got the callback reference then just cast it to your callback interface type and then use the methods you defined in your callback interface straight from there. As long as the connection between the two machines has not timed out or faulted etc then it will just work
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: WCF Accessing the class created at the Endpoint?

    Hi,

    Thanks for your replies. I think I need a little more explanation.

    These two pieces of code create a callback Serviceendpoint:

    svh = new ServiceHost(typeof(EventCallbackPipe));

    svh.AddServiceEndpoint(typeof(IEventCallbackPipe), new NetTcpBinding(), "net.tcp://" + ChannelServer + ":" + ChannelPort);


    the type of object is ServiceHost but has factored an interface class of type IEventCallbackPipe.

    On the Interface is a Subscribe method. When the clients call subscribe I take the CallbackChannel and store it in a arraylist.

    Code:
    public bool Subscribe(enForEvents ForEvents)
           {
               ICallbackInterface Subscriber = OperationContext.Current.GetCallbackChannel<ICallbackInterface>();
               
               CallbackSubscribers.Add(Subscriber);
               return true;
               
           }
    now, say i want the service class that created the end point to access the endpoint and call another method that iterates the list of subscribers and sends them something, how do I do this ?

    Thanks again for your patience.

    Chubby.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF Accessing the class created at the Endpoint?

    OK so lets say you have your ArrayList (you would probably be better off using a strongly typed List(Of Subscriber) by the way) that contains the CallBackChannel objects and its named ClientList. Your callback interface that each of the clients implements is called ICallbackInterface and we will pretend you have a method defined in there called ReceiveData. So you simply need to do something like this:

    vb Code:
    1. For Each Client As Subscriber In ClientList
    2.      DirectCast(Client, ICallbackInterface).ReceiveData("some data to be sent")
    3. Next
    That code might not be 100&#37; perfect as its just from memory but give it a go and see if you can get it to work

    EDIT: Actually I just realised Subscriber is not a class in your code is it, its just the name of a local variable. So try this instead:
    vb Code:
    1. For Each Client As Object In ClientList
    2.      DirectCast(Client, ICallbackInterface).ReceiveData("some data to be sent")
    3. Next
    Note that if you used a strongly typed List(Of T) then you could probably just do this:
    vb Code:
    1. For Each Client As ICallbackInterface In ClientList
    2.      Client.ReceiveData("some data to be sent")
    3. Next
    Last edited by chris128; Sep 16th, 2009 at 03:43 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: WCF Accessing the class created at the Endpoint?

    Quote Originally Posted by chris128 View Post
    OK so lets say you have your ArrayList (you would probably be better off using a strongly typed List(Of Subscriber) by the way) that contains the CallBackChannel objects and its named ClientList. Your callback interface that each of the clients implements is called ICallbackInterface and we will pretend you have a method defined in there called ReceiveData. So you simply need to do something like this:

    vb Code:
    1. For Each Client As Subscriber In ClientList
    2.      DirectCast(Client, ICallbackInterface).ReceiveData("some data to be sent")
    3. Next
    That code might not be 100% perfect as its just from memory but give it a go and see if you can get it to work

    EDIT: Actually I just realised Subscriber is not a class in your code is it, its just the name of a local variable. So try this instead:
    vb Code:
    1. For Each Client As Object In ClientList
    2.      DirectCast(Client, ICallbackInterface).ReceiveData("some data to be sent")
    3. Next
    Note that if you used a strongly typed List(Of T) then you could probably just do this:
    vb Code:
    1. For Each Client As ICallbackInterface In ClientList
    2.      Client.ReceiveData("some data to be sent")
    3. Next
    Hi Chris128

    Thankyou very much for posting, I really appreciate your help. My issue isn't with the actual clients receiving the data, it is that I want to call the function that iterates the list from outside the endpoint class ? I have a few endpoints that I create from a service. One of these is the callback endpoint. I wish the service itself (that created the endpoint) to call a method in the endpoint class that iterates the subscribers in the list and sends them something? So, I wish to get to the class created by the AddServiceEndpoint method in order to call "NotifyClients" which in turn iterates the list ?

    Sorry if I'm being too vague here.

    Thanks again for your help

    Chubby.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF Accessing the class created at the Endpoint?

    Ohhh I see (I think)
    You want to be able to call methods on the instance of the service that is running? The way I have handled this in the past is to use public shared methods but that meant that my client list also had to be public shared so it wasnt ideal... but it worked in my situation. In your situation though it may not work, it depends how you are using your service etc
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: WCF Accessing the class created at the Endpoint?

    Quote Originally Posted by chris128 View Post
    Ohhh I see (I think)
    You want to be able to call methods on the instance of the service that is running? The way I have handled this in the past is to use public shared methods but that meant that my client list also had to be public shared so it wasnt ideal... but it worked in my situation. In your situation though it may not work, it depends how you are using your service etc


    The reason for this architecture is that the application is designed to function across a wide area network and have different WCF interfaces (some heavy on network & CPU), I wanted a top-level Callback interface that can be used by all the other WCF interfaces to notify clients of changes of data. The reason for more than one WFC channel is that some stuff needs to work in it's own thread so as to not unduly affect the running of the client applications. But as there is no coercion (sp?) that I can find on the ServiceChannel and iterating the BaseClasses yields nothing I'm stuck with a bit of a dilemma. do I:

    A) Create a local client proxy and call those methods using that?
    B) Create each WCF interface as a Callback....

    Thanks again for your replies. Appreciate the help.

    Chubby.

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WCF Accessing the class created at the Endpoint?

    I think the problem with solution A would be that you would still have to have a shared client list so you may as well just have a public shared (static in C#) method that iterates through it as well. I mean if you created a local client proxy and called a method on the WCF service then you would be working with your own instance of the service and therefore would not have access to the client list that contains all of the other clients unless it was shared/static (perhaps it already is?). If that's the case then why not just have a public static method that you can call from anywhere?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    Re: WCF Accessing the class created at the Endpoint?

    Hi Chris128,

    Thanks. Regardless, I need an "IN" to the object created by the addserviceendpoint.. so would need to configure a Client Channel on the server to do this whether I use Static method or not as I still have the issue of accessing the object from the service..

    Decided to implement Callback interfaces for every WFC I need and propagate from a base class, so, DATA WCF will have a usable Callback method, as will the MetaData WCF, as will the CHAT WCF (for obvious reasons) etc.. and ne'r the twain shall meet.. annoying, but a workaround nonetheless..

    Many thanks for taking the time to reply. I really appreciate the help.

    Chub.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: WCF Accessing the class created at the Endpoint?

    That's just like a heartbeat method. Here's what I'd do: When the item is created in the WCF service, return some sort of a unique ID or GUID which you associate with each work task. Have the long running tasks in the service update a database or central 'repository' (yes, maybe even a static variable, shudder), and have your heartbeath method query the static variable or database for the status of the task.

    I'd rather have an extra method that looks at the task running than the callback, and I know you've already moved on with this, but just so you know...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width