WCF callback

1 创建WCF服务
2 更改IService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
 

    [ServiceContract(SessionMode= SessionMode.Required,CallbackContract =typeof(IServiceCall))]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }


    public interface IServiceCall
    {
        [OperationContract(IsOneWay = true)]
        void send(string value);
    }
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

3 修改Service.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode= InstanceContextMode.PerSession)]
    public class Service1 : IService
    {

        public IServiceCall callback{
            get {
                return OperationContext.Current.GetCallbackChannel<IServiceCall>();
            }
        }

        public string GetData(int value)
        {
            callback.send("jjjj");
            return string.Format("You entered: {0}", value);
            
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

4 更改config文件,添加如下配置

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="Service" name="WcfService.Service">
        <endpoint address="" binding="wsDualHttpBinding" contract="WcfService.IService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

5 发布并部署到IIS, 需要注意的是发布的文件夹需要添加一个everyone all control
在这里插入图片描述
6 开发客户端
添加callback接口并实现

    public interface IServiceCall
    {
        [OperationContract(IsOneWay = true)]
        void send(string value);
    }
 public class Class1:IServiceCall
    {
        public void send(string value)
        {
            Console.WriteLine(value);
        }
    }

7 调用

Uri uri = new Uri("http://127.0.0.1:8888/Service.svc");
            EndpointAddress eaddr = new EndpointAddress(uri);
            WSDualHttpBinding wSDualHttpBinding = new WSDualHttpBinding();
            //BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

            DuplexChannelFactory<IService> channelFactory = new DuplexChannelFactory<IService>(new InstanceContext(new Class1()), wSDualHttpBinding);
            IService channel = channelFactory.CreateChannel(eaddr, uri);

            string ss = channel.GetData(12345);
            Console.WriteLine(ss); ;

source code
https://github.com/xdqt/wcf_callback.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值