What steps do I need to take to use WCF Callbacks?

后端 未结 4 736
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 06:46

I am trying to learn WCF. I have a simple client and server application setup and upon pressing a button on the client, it gets an updated value from the server.

相关标签:
4条回答
  • 2020-12-02 07:06

    Grab a copy of "Programming WCF Services, 2nd Edition" by Juval Lowy. There are large sections of the book devoted to Callback operations. In Chapter 5, start on page 214. In the chapter on Concurrency Management (ch. 8) there's even more information.

    "Programming WCF Services" is more or less the WCF "bible."

    0 讨论(0)
  • 2020-12-02 07:16

    If I'm reading your question right, you want to have a two-way conversation between the client and the server (the server can communicate back to the client). The WSDualHttpBinding gives you this functionality.

    The unfortunate reality with WCF is that there is no such thing as a simple example. It requires you to define contracts, configure the services, and use a host, and create client code. Take a look at this article for a somewhat simple example.

    0 讨论(0)
  • 2020-12-02 07:27

    I know, old question... I came across this question from a google search earlier today and the answer provided by Ray Vernagus is the easiest to understand example of WCF that I have read to date. So much so that I was able to rewrite it in VB.NET without using any online converters. I thought I'd add the VB.NET variant of the example that Ray Vernagus provided. Just create a new VB.NET Windows Console application, add a reference to System.ServiceModel, and copy/paste the entire code below into the default Module1 class file.

    Imports System.ServiceModel
    Imports System.ServiceModel.Channels
    
    
    
    Public Interface IMyContractCallback
        <OperationContract()> _
        Sub OnCallBack()
    End Interface
    
    <ServiceContract(CallBackContract:=GetType(IMyContractCallback))> _
    Public Interface IMyContract
        <OperationContract()> _
        Sub DoSomething()
    End Interface
    
    <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Reentrant)> _
    Public Class Myservice
        Implements IMyContract
    
        Public Sub DoSomething() Implements IMyContract.DoSomething
            Console.WriteLine("Hi from server!")
            Dim callback As IMyContractCallback = OperationContext.Current.GetCallbackChannel(Of IMyContractCallback)()
            callback.OnCallBack()
        End Sub
    End Class
    
    Public Class MyContractClient
        Inherits DuplexClientBase(Of IMyContract)
    
        Public Sub New(ByVal callbackinstance As Object, ByVal binding As Binding, ByVal remoteAddress As EndpointAddress)
            MyBase.New(callbackinstance, binding, remoteAddress)
        End Sub
    End Class
    
    Public Class MyCallbackClient
        Implements IMyContractCallback
    
        Public Sub OnCallBack() Implements IMyContractCallback.OnCallBack
            Console.WriteLine("Hi from client!")
        End Sub
    End Class
    
    
    Module Module1
    
        Sub Main()
            Dim uri As New Uri("net.tcp://localhost")
            Dim binding As New NetTcpBinding()
            Dim host As New ServiceHost(GetType(Myservice), uri)
            host.AddServiceEndpoint(GetType(IMyContract), binding, "")
            host.Open()
    
            Dim callback As New MyCallbackClient()
            Dim client As New MyContractClient(callback, binding, New EndpointAddress(uri))
            Dim proxy As IMyContract = client.ChannelFactory.CreateChannel()
    
            proxy.DoSomething()
            ' Printed in console:
            '  Hi from server!
            '  Hi from client!
    
            Console.ReadLine()
    
            client.Close()
            host.Close()
        End Sub
    
    End Module
    
    0 讨论(0)
  • 2020-12-02 07:29

    Here is about the simplest complete example that I can come up with:

    public interface IMyContractCallback
    {
        [OperationContract]
        void OnCallback();
    }
    
    [ServiceContract(CallbackContract = typeof(IMyContractCallback))]
    public interface IMyContract
    {
        [OperationContract]
        void DoSomething();
    }
    
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class MyService : IMyContract
    {
        public void DoSomething()
        {
            Console.WriteLine("Hi from server!");
            var callback = OperationContext.Current.GetCallbackChannel<IMyContractCallback>();
            callback.OnCallback();
        }
    }
    
    public class MyContractClient : DuplexClientBase<IMyContract>
    {
        public MyContractClient(object callbackInstance, Binding binding, EndpointAddress remoteAddress)
            : base(callbackInstance, binding, remoteAddress) { }
    }
    
    public class MyCallbackClient : IMyContractCallback
    {
        public void OnCallback()
        {
            Console.WriteLine("Hi from client!");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var uri = new Uri("net.tcp://localhost");
            var binding = new NetTcpBinding();
            var host = new ServiceHost(typeof(MyService), uri);
            host.AddServiceEndpoint(typeof(IMyContract), binding, "");
            host.Open();
    
            var callback = new MyCallbackClient();
            var client = new MyContractClient(callback, binding, new EndpointAddress(uri));
            var proxy = client.ChannelFactory.CreateChannel();
            proxy.DoSomething();
            // Printed in console:
            //  Hi from server!
            //  Hi from client!
    
            client.Close();
            host.Close();
        }
    }
    

    A few namespaces will need to be included in order to run the example:

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    0 讨论(0)
提交回复
热议问题