使用 WCF 回调需要采取哪些步骤? [英] What steps do I need to take to use WCF Callbacks?

查看:22
本文介绍了使用 WCF 回调需要采取哪些步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 WCF.我有一个简单的客户端和服务器应用程序设置,按下客户端上的按钮后,它会从服务器获取更新的值.

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#?

我一直在网上浏览示例,但我不明白这需要什么?当然,我可以逐行复制示例,但这对我没有好处,因为如果我想在自己的代码中执行此操作,我仍然不知道要实现什么.

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.

有人可以用一个非常简单的例子来帮助我说明我需要采取哪些步骤以及我需要在服务器代码和客户端代码中做什么来实现这一点吗?

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;

这篇关于使用 WCF 回调需要采取哪些步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆