什么样的步骤,我需要使用WCF回调?回调、步骤、WCF

2023-09-02 01:43:49 作者:离别

我努力学习WCF。我有一个简单的客户端和服务器应用程序的安装,并在pressing在客户端上的一个按钮,它就会从服务器的更新值。

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.

我的下一个步骤,我试图做的从服务器回调到客户端,以更新它的值。我已经经历了很多例子倒,他们只是显得过大和混乱。是否有任何人可以给它在C#实现我的只是最简单的例子?

My next step is I am trying to do a callback from the server to the client to update its value. I have poured through many examples, and they just seem too big and confusing. Is there anyone that can give my just the simplest example of its implementation in C#?

我一直通过实例看在线,我只是不明白,需要采取哪些措施?当然,我可以复制的例子逐行但确实我没有好,因为我还没有怎么实现,如果我想这样做我自己的code。

I keep looking through examples online and I just do not understand what it takes? Of course I could copy the example line by line but that does me no good because I still don't what to implement if I wanted to do this in my own code.

可能有人请帮我一个很简单的例子什么措施,我需要采取什么我需要做的服务器code,然后在客户端code来实现这一目标?

Could someone please help me with a very simple example on what steps I would need to take and what I would need to do in the server code and then in the client code to make this happen?

感谢您

推荐答案

下面是关于简单的完整的例子,我可以想出:

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;