wcf消息模式(随记)
----------------------------------------------消息模式:
1.request\reply(默认)
2.one-way(单工)[Isoneway=true]
客户端有些情况希望保证消息能正确送达;(msmq(持久性的,机子重启后依然有效)和可靠性消息(短暂的)这两种技术保证)
应用:日志或发布,操作需要大量时间轮训场景,这样不需要等待;
3.Duplex(回调;双工)
操作必须声明为oneway;防止发生死锁;
回调接口不需要声明servicecontract
tcp和命名管道支持双工,http不支持;
--duplex案例:
--------------------------------------------双工:
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract =typeof(IReturnuser))]
public interface Iuser
{
[OperationContract(IsOneWay=true)]
void SetName(string name);
}
public interface IReturnuser
{
[OperationContract(IsOneWay = true)]
void ReturnName(string name);
}
-----------
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class ServicUser:Iuser
{
IReturnuser callback
{
get { return OperationContext.Current.GetCallbackChannel<IReturnuser>(); }
}
public void SetName(string name)
{
callback.ReturnName(name+"123");
}
}
-----------
public class CallbackHandler : myService.IService1Callback
{
public void Calculate(int result)
{
System.Windows.Forms.MessageBox.Show("结果为:" + result);
}
}
---------
InstanceContext ic = new InstanceContext(new CallbackHandler());
myService.Service1Client client = new wcfClient.myService.Service1Client(ic);
client.AddNumer(3, 4);
-----大型消息传输:
比如:上传和下载数据;
1.在wcf的配置文件制定消息大小的配额;从而支持较大消息的支持;
21.开销解决方案:
消息传输优化机制:MTOM;(删除过多无用的信息并且处理base64编码多带来的数据开销,改进消息传输的整体性能;)
通过配置文件配置Binding节点信息;messageEncoding="Mtom",
MTOM,会加载到内存中;为了降低内存使用,使用流传输;
配置:
<bindings>
<wsHttpBinding>
<binding name="sdf" maxReceivedMessageSize="5000000" messageEncoding="Mtom">
<readerQuotas maxArrayLength="5000000"/>
</binding>
</wsHttpBinding>
</bindings>
22流传输;特点:降低内存的使用量;但可靠性和安全性降低;
--标记流传输:
<bindings>
<basicHttpBinding>
<binding name="sdf" transferMode="Streamed">
</binding>
</basicHttpBinding>
</bindings>
--流传递支持三种绑定:netnamedpipebinding,nettcpbinding,basichttpbinding;
transferMode:值默认buffered(缓冲式),Streamed流传输;
23将消息切分成较小的切片;