2

イントラネットでC#WCFを使用するASP.NET3.5アプリがあります。

このサービスは、3つのコマンドを順番に実行し、それぞれ2〜3分かかります。ラベルの更新など、実行中のコマンドでユーザーを最新の状態に保ちたいです。

私はこの問題の専門家ではないので、これを行うための最良の方法を知りたいです。

ありがとう、

追伸 サービスとクライアントは、IIS7.5を使用して同じサーバーでホストされます。

編集

さて、私は過去2日間これに取り組んできました..私は専門家ではありません:)

私はEricの提案、WSHttpDualBindingの使用、およびコールバック関数に従っています。

そのため、デュプレックスバインディングを使用してサービスを構築し、コールバック関数を定義することはできましたが、クライアント側でコールバック関数を定義することはできません。これに光を当ててください。

namespace WCF_DuplexContracts
{
    [DataContract]
    public class Command
    {
        [DataMember]
        public int Id;
        [DataMember]
        public string Comments;
    }



    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbacks))]
    public interface ICommandService
    {
        [OperationContract]
        string ExecuteAllCommands(Command command);
    }

    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void MyCallbackFunction(string callbackValue);
    }



    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CommandService : ICommandService
    {
        public string ExecuteAllCommands(Command command)
        {
            CmdOne();
            //How call my callback function here to update the client??
            CmdTwo();
            //How call my callback function here to update the client??
            CmdThree();
            //How call my callback function here to update the client??

            return "all commands have finished!";
        }

        private void CmdOne()
        {
            Thread.Sleep(1);
        }

        private void CmdTwo()
        {
            Thread.Sleep(2);
        }

        private void CmdThree()
        {
            Thread.Sleep(3);
        }
    }
}

編集2

これはクライアントの実装です。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.DuplexServiceReference;
using System.ServiceModel;

namespace Client
{
    class Program
    {
        public class Callback : ICommandServiceCallback
        {
            public void MyCallbackFunction(string callbackValue)
            {
                Console.WriteLine(callbackValue);
            }
        }

        static void Main(string[] args)
        {
            InstanceContext ins = new InstanceContext(new Callback());
            CommandServiceClient client = new CommandServiceClient(ins);

            Command command = new Command();
            command.Comments = "this a test";
            command.Id = 5;

            string Result = client.ExecuteAllCommands(command);
            Console.WriteLine(Result);

        }
    }
}

そして結果:

C:\>client
cmdOne is running
cmdTwo is running
cmdThree is running
all commands have finished!
4

2 に答える 2

3

二重バインディングを使用し、コールバックでステータスを更新します。

編集*コールバックチャネルへの参照を取得する必要があります

public string ExecuteAllCommands(Command command)
    {

        var callback = OperationContext.Current.GetCallbackChannel<ICallbacks>();                  
        CmdOne();
        //How call my callback function here to update the client??
        callback.MyCallbackFunctio("cmdOne done");
        CmdTwo();
        callback.MyCallbackFunctio("cmdTwo done");
        //How call my callback function here to update the client??
        CmdThree();
        callback.MyCallbackFunctio("cmdThree done");
        //How call my callback function here to update the client??

        return "all commands have finished!";
    }

タイムアウトしないように、サービスメソッドを無効にする必要があります。

于 2012-05-12T05:18:19.703 に答える
1

いくつかの操作を作成します。1つは長いコマンドを開始し、もう1つはステータスを取得します。コマンドの完了を待機する操作が1つある場合、タイムアウトの問題が発生し、進行状況を把握する方法がありません。

于 2012-05-12T04:34:25.480 に答える