0

I am trying to track and use a session id between calls to a WCF service however after each call by the same WCF client instance I receive two different session ids.

Here is the service contract, specifying that sessions are required:

[ServiceContract(SessionMode=SessionMode.Required)]
public interface IMonkeyService
{
    [OperationContract(ProtectionLevel=System.Net.Security.ProtectionLevel.None, IsOneWay = true, IsInitiating = true, IsTerminating = false)]
    void Init();

    [OperationContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None, IsOneWay = false, IsInitiating = false, IsTerminating = false)]
    string WhereAreMyMonkies();

    [OperationContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None, IsOneWay = true, IsInitiating = false, IsTerminating = true)]
    void End();
}

Here is the service implementation:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults=true)]
public class BestMonkeyService : IMonkeyService
{
    public void Init() { ;}
    public void End() { ;}
    public string WhereAreMyMonkies()
    {
        return "Right here";
    }
}

Here is the service being opened:

_Service = new ServiceHost(typeof(BestMonkeyService));
var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.None;
binding.ReliableSession.Enabled = true;
string uriAddress = "http://localhost:8000/MONKEY_SERVICE";
var endpoint = _Service.AddServiceEndpoint(typeof(IMonkeyService), binding, uriAddress);
_Service.Open();

Here is the client configuration

<system.serviceModel>
 <client>
  <endpoint address="http://localhost:8000/MONKEY_SERVICE" bindingConfiguration="wsHttpBindingConfiguration"
            binding="wsHttpBinding" contract="IMonkeyService" />
 </client>
 <bindings>
  <wsHttpBinding>
   <binding name="wsHttpBindingConfiguration">
    <security mode="None" />
    <reliableSession enabled="true" />
   </binding>
  </wsHttpBinding>
 </bindings>
</system.serviceModel>

The call by the client which is kept open after the first call:

var client = new MonkeyClient();
client.Init();
client.WhereAreMyMonkies();
client.WhereAreMyMonkies();
client.End();

I am getting an ActionNotSupportedException

The message with Action 'http://tempuri.org/IMonkeyService/WhereAreMyMonkies' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

I have both service and client configured using the same binding and security. What am I missing?

fanuc_bob
  • 857
  • 1
  • 7
  • 20

2 Answers2

1

As explained here, because the default value of IsInitiating parameter is true each of your calls started a new session.

You need something like this:

[OperationContract(IsInitiating=false, IsTerminating=false)]
string WhereAreMyMonkies();

By default a session is initiated when channel is opened. You can also add methods that explicitly create and terminate a session (as explained in the documentation).

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • @fanuc_bob Glad I could help! If this was the correct answer consider marking it as accepted, for the benefit of future readers :-) – BartoszKP Nov 27 '20 at 09:18
0

WSHttpBinding doesn't support sessions without reliable messaging or security sessions.

See the answer to How to enable Session with SSL wsHttpBinding in WCF

Community
  • 1
  • 1
ErnieL
  • 5,773
  • 1
  • 23
  • 27