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