Why is my sessionful,NetTcpBinding, Streaming service instance not retaining session state?

断了今生、忘了曾经 提交于 2020-01-15 18:49:25

问题


I am trying to figure out why my NetTcpBinding Service is not maintaining state, as expected. (Perhaps I did it wrong)

I have a WCF service with the following structure(simplified):

Message Contract

    [MessageContract]
    public class StreamMetaData
    {
        [MessageHeader] 
        public string Info {get; set;}
        [MessageBodyMember]
        public Stream Data { get; set; }

    }

Contract

    ServiceContract(SessionMode = SessionMode.Allowed)]
    public interface IMyService
    {
        [OperationContract]
        void Init(StreamMetaData metaData);

        [OperationContract]
        StreamMetaData GetMetaData();

    }

Implementation

    [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;

        }
    }

Client Side

        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

I followed the MSDN documentation on handling sessions (streams specifically).

Why is it that var currentMetaData = client.GetMetaData(); returns null? Despite initializing it in the first call?

Update:

  • Investigating possibility of having two different bindings: the one for streaming and another for session creation.

来源:https://stackoverflow.com/questions/28959925/why-is-my-sessionful-nettcpbinding-streaming-service-instance-not-retaining-ses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!