Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am trying to figure out why my NetTcpService is not maintaining state, as expected. (Perhaps I did it wrong)

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

Contract
:

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

    [OperationContract]
    StreamMetaData GetMetaData();

}


Implementation
C#
[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
C#
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[^]MSDN documentation on handling sessions (streams specifically - last paragraph).

Why is it that var currentMetaData = client.GetMetaData(); returns null? Despite initializing it in the first call?
Posted
Comments
[no name] 10-Mar-15 9:30am    
I'm not a WCF-Expert.. but the first thing I'd try is to see if changing
ServiceContract(SessionMode = SessionMode.Allowed)]
to
ServiceContract(SessionMode = SessionMode.Required)]
would help.
tinoneticm 10-Mar-15 9:44am    
That's just guessing :) SessionMode.Required is not supported with a Streaming TransferMode
[no name] 10-Mar-15 9:47am    
Alright, I hope someone can help you :)
Rajat_RJT 11-Mar-15 2:49am    
can you update you web.config also in question??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900