WCF 回调及重入

服务端类代码

服务契约:

    [ServiceContract(CallbackContract=typeof(IserviceCallBack))]

    public interface IService1

    {

        [OperationContract]

        string GetData(int value);



    }



    public interface IserviceCallBack

    {

        [OperationContract]

        string GetClientData();

    }

 

服务实例:

   [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]

    public class Service1 : IService1

    {

        public string GetData(int value)

        {

            IserviceCallBack callback = OperationContext.Current.GetCallbackChannel<IserviceCallBack>();

            string value1= callback.GetClientData();

            return string.Format("You entered: {0} and CallValue is {1}", value,value1);

        }

    }


由于在方法中调用客户端的结果,所以使用重入

客户端代码

    class CallBack : IService1Callback

    {

        public string GetClientData()

        {

            return "test callback value";

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Service1Client client = new Service1Client(new InstanceContext(new CallBack()));

            Console.Write(client.GetData(1));

            Console.Read();

        } 

    }


结果是:You entered: 1 and CallValue is test callback value

 

你可能感兴趣的:(WCF)