我试图弄清楚为什么我的NetTcpBinding服务没有按预期保持状态。 (也许我做错了)
我有一个WCF服务,其结构如下(简化):
邮件合同
[MessageContract]
public class StreamMetaData
{
[MessageHeader]
public string Info {get; set;}
[MessageBodyMember]
public Stream Data { get; set; }
}
合同
ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IMyService
{
[OperationContract]
void Init(StreamMetaData metaData);
[OperationContract]
StreamMetaData GetMetaData();
}
实施
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService : IMyService
{
private StreamMetaData _streamMetaData;
private StreamMetaData StreamMetaData
{
get { return this._streamMetaData; }
set { this._streamMetaData= value; }
}
public void Init(StreamMetaData metaData)
{
if (metaData == null)
throw new ArgumentNullException("metaData");
try
{
this.StreamMetaData = metaData;
}
catch (Exception ex)
{
throw ex;
}
}
public StreamMetaData GetMetaData()
{
return this.StreamMetaData;
}
}
客户端
MyServiceNetTcp.MyServiceClient client= new MyServiceNetTcp.MyServiceClient ();
client.Open();
//metaData is an instance of StreamMetaData initialised somewhere in the code
client.Init(metaData);
var currentMetaData = client.GetMetaData(); //Returns null
我跟着MSDN documentation on handling sessions(特别是溪流)。
为什么 var currentMetaData = client.GetMetaData(); 返回null?尽管在第一次通话中初始化它?
更新