有关 WCF 服务和 Windows 应用程序客户端的帮助

发布于 2024-12-06 11:17:02 字数 489 浏览 0 评论 0原文

我已经在互联网上浏览了一段时间,但找不到任何适合我的确切问题的东西。如果有人能简洁地解释这一点,我将不胜感激。

基本上我想从我的客户端(Windows 应用程序)调用 WCF Web 服务,该服务将执行更新。不过,我希望该服务能够“回调”客户端的进度,以便用户可以通过可视进度条看到正在发生的情况。这可以做到吗?

我已经研究过全双工 WCF 服务的想法,其中有回调,并尝试编写一些代码,但实际上并没有最大的运气让这些回调触发,我大致了解 wsDualHttpBinding 和 netTcpBinding 之间的磨难,但可以都没有真正开始工作。

目前,我的测试正在同一个盒子上运行,即 Windows 应用程序和 WCF 服务(运行 http://localhost:58781/ )。我知道一旦这些转移到生产环境,我可能会遇到更多问题,所以我希望现在考虑这些问题。

任何对此的帮助将不胜感激。

I have been looking around the internets for some time but can't find anything that fits my exact problem. If someone could explain this succinctly I would be grateful.

Basically I would like to call a WCF web service from my client (Windows App), this service will perform updates. However I would like the service to "callback" to the client with progress so the user can see what's going on via a visual progress bar. Can this be done?

I have looked at the idea of Full Duplex WCF services which have callbacks in them and have tried to write some code, but not having the greatest of luck actually getting these callbacks to fire, I roughly know about the tribulations between wsDualHttpBinding and netTcpBinding but can't really get either to work.

Currently my testing is running off the same box, i.e both the windows application and the WCF service (running off http://localhost:58781/). I know once these move to a production environment I may get more issues so I wish to take these into consideration now.

Any help with this would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-12-13 11:17:02

这是一个具有自托管服务和客户端的准系统示例。

合同

[ServiceContract(CallbackContract = typeof(IService1Callback), SessionMode=SessionMode.Required)]
public interface IService1
{
    [OperationContract]
    void Process(string what);
}

public interface IService1Callback
{
    [OperationContract]
    void Progress(string what, decimal percentDone);
}

服务器

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
    public void Process(string what)
    {
        Console.WriteLine("I'm processing {0}", what);
        for (int i = 0; i < 10; i++)
        {
            OperationContext.Current.GetCallbackChannel<IService1Callback>().Progress(what, (i+1)*0.1M);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {

        using (ServiceHost host = new ServiceHost( typeof(Service1), new Uri[] { new Uri("net.tcp://localhost:6789") }))
        {
            host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(SecurityMode.None), "Service1");
            host.Open();
            Console.ReadLine();
            host.Close();
        }
    }
}

客户端

public class CallbackHandler : IService1Callback
{
    public void Progress(string what, decimal percentDone)
    {
        Console.WriteLine("Have done {0:0%} of {1}", percentDone, what);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Setup the client
        var callbacks = new CallbackHandler();
        var endpoint = new EndpointAddress(new Uri("net.tcp://localhost:6789/Service1"));
        using (var factory = new DuplexChannelFactory<IService1>(callbacks, new NetTcpBinding(SecurityMode.None), endpoint))
        {
            var client = factory.CreateChannel();
            client.Process("JOB1");
            Console.ReadLine();
            factory.Close();
        }
    }
}

这使用 net.tcp 上的双工通道,并由服务器触发通信来通知客户端进度更新。

客户端会显示:

Have done 10% of JOB1
Have done 20% of JOB1
Have done 30% of JOB1
Have done 40% of JOB1
Have done 50% of JOB1
Have done 60% of JOB1
Have done 70% of JOB1
Have done 80% of JOB1
Have done 90% of JOB1
Have done 100% of JOB1

This is a barebone example with a self hosted service and client.

Contracts

[ServiceContract(CallbackContract = typeof(IService1Callback), SessionMode=SessionMode.Required)]
public interface IService1
{
    [OperationContract]
    void Process(string what);
}

public interface IService1Callback
{
    [OperationContract]
    void Progress(string what, decimal percentDone);
}

Server

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
    public void Process(string what)
    {
        Console.WriteLine("I'm processing {0}", what);
        for (int i = 0; i < 10; i++)
        {
            OperationContext.Current.GetCallbackChannel<IService1Callback>().Progress(what, (i+1)*0.1M);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {

        using (ServiceHost host = new ServiceHost( typeof(Service1), new Uri[] { new Uri("net.tcp://localhost:6789") }))
        {
            host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(SecurityMode.None), "Service1");
            host.Open();
            Console.ReadLine();
            host.Close();
        }
    }
}

Client

public class CallbackHandler : IService1Callback
{
    public void Progress(string what, decimal percentDone)
    {
        Console.WriteLine("Have done {0:0%} of {1}", percentDone, what);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Setup the client
        var callbacks = new CallbackHandler();
        var endpoint = new EndpointAddress(new Uri("net.tcp://localhost:6789/Service1"));
        using (var factory = new DuplexChannelFactory<IService1>(callbacks, new NetTcpBinding(SecurityMode.None), endpoint))
        {
            var client = factory.CreateChannel();
            client.Process("JOB1");
            Console.ReadLine();
            factory.Close();
        }
    }
}

This uses a duplex channel over net.tcp with communications being triggered by the server to inform the client of progress updates.

The client will display:

Have done 10% of JOB1
Have done 20% of JOB1
Have done 30% of JOB1
Have done 40% of JOB1
Have done 50% of JOB1
Have done 60% of JOB1
Have done 70% of JOB1
Have done 80% of JOB1
Have done 90% of JOB1
Have done 100% of JOB1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文