none
Windows Services in C# RRS feed

  • Question

  • I've created a service using this link http://arcanecode.com/2007/05/22/windows-services-in-c-getting-started-part-2/[^]

    I configure my config for https, but cannot start my service. I get this error "Service cannot be started. System.InvalidOperationException: Contract requires Session, but Binding 'WSHttpBinding' doesn't support it or isn't configured properly to support it."

    <service name="Service.MyService" behaviorConfiguration="MyServiceBehavior">         <host>           <baseAddresses>             <add baseAddress="https://localhost:443/MyService"/>           </baseAddresses>         </host>         <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyServiceEndpoint" contract="IMyService"/>         <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />       </service>

    <wsHttpBinding>     <binding name="MyServiceEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">           <readerQuotas maxDepth="2147483647" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />           <security mode="Transport">             <transport clientCredentialType="None" />           </security>     </binding> </wsHttpBinding>

    <serviceBehaviors>         <behavior name="MyServiceBehavior">           <serviceMetadata httpsGetEnabled="true"/>           <serviceDebug includeExceptionDetailInFaults="False"/>         </behavior>       </serviceBehaviors>

    Can anybody help me? What else I should do for starting it?   Thanks all

    Monday, October 14, 2013 11:48 AM

Answers

  • If you want to use session enabled service then you must take care of there things for contract.

    There are 3 things to remember for WCF session:

    1. Sessionful binding
    2. SessionMode service contract
    3. InstanceContextMode service behavior

    You have to set each attribute for the session enabled WCF service. See this article for more details:

    http://www.codeproject.com/Articles/188749/WCF-Sessions-Brief-Introduction

    Monday, October 14, 2013 2:13 PM
  • Hi,

    We can have WCF hold session information in memory, I'll explain it with a brand new project:

    1. Create a new WCF Service Library project. This project will already contain a service with a WSHttpBiding binding preconfigured.
    2. Go to the service contract (IService1.cs) and change the ServiceContract attribute to the following:

      [ServiceContract(SessionMode = SessionMode.Required)]
    3. Go to the service implimentation (Service1.cs) and add the following ServiceBehavior attribute to the service class (Service1):

      [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
    4. Add session data as members of the service class (Service1):

      public class Service1 : IService1
      {
          ...
      
          private string Name { get; set; }
      
          ...
      }
    5. Use the members to present session specific data (remember to also add them to the service contract, IService1):

      public class Service1 : IService1
      {
          ...
          
          public string HelloWorld(string id)
          {
              Name=id;
      return string.Format("HelloWorld, {0}!", Name); } ... }

    SessionMode.Required ensures that your clients are session-tracked.
    InstanceContextMode.PerSession ensures that an instance of your service class (Service1) is created for every session, so that you can retain session data in it and it will exist in memory across multiple calls in the same session.
    ConcurrencyMode.Single ensures that only one thread can enter each service class instance (Service1), and prevents possible concurrency issues if you only access data from the service class (and external thread-safe locations).

    EDIT: By default, WSHttpBinding only allows security sessions. But it also support reliable sessions, which allow establishing sessions without security enabled. The following binding configuration disables security and enables reliable sessions:

    <wsHttpBinding>
        <binding name="wsHttpBindingConfiguration">
            <security mode="None" />
            <reliableSession enabled="true" />
        </binding>
    </wsHttpBinding>
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, October 15, 2013 5:34 AM
    Moderator

All replies

  • <copied>

    Contract requires Session, but Binding 'WSHttpBinding' doesn't support it or isn't configured properly to support it."

    <end>

    It's telling you what is wrong. It's telling you that the WCF service contract requires "Session" and 'WSHttpBinding' doesn't support "Sessions" or you don't have it configured to use "Sessions" with 'WSHttpBinding'.

    That's what you have to figure out. What are you suppose to be using for the transport protocol and how to configure it. 

    Monday, October 14, 2013 1:27 PM
  • I use [ServiceContract(SessionMode = SessionMode.Allowed)] above my Interface and get that error. 

    I even used <reliableSession enabled="true"/> , it that case I get another error 

    • Proposed as answer by vendettamit Monday, October 14, 2013 2:10 PM
    Monday, October 14, 2013 1:54 PM
  • If you want to use session enabled service then you must take care of there things for contract.

    There are 3 things to remember for WCF session:

    1. Sessionful binding
    2. SessionMode service contract
    3. InstanceContextMode service behavior

    You have to set each attribute for the session enabled WCF service. See this article for more details:

    http://www.codeproject.com/Articles/188749/WCF-Sessions-Brief-Introduction

    Monday, October 14, 2013 2:13 PM
  • Hi,

    We can have WCF hold session information in memory, I'll explain it with a brand new project:

    1. Create a new WCF Service Library project. This project will already contain a service with a WSHttpBiding binding preconfigured.
    2. Go to the service contract (IService1.cs) and change the ServiceContract attribute to the following:

      [ServiceContract(SessionMode = SessionMode.Required)]
    3. Go to the service implimentation (Service1.cs) and add the following ServiceBehavior attribute to the service class (Service1):

      [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
    4. Add session data as members of the service class (Service1):

      public class Service1 : IService1
      {
          ...
      
          private string Name { get; set; }
      
          ...
      }
    5. Use the members to present session specific data (remember to also add them to the service contract, IService1):

      public class Service1 : IService1
      {
          ...
          
          public string HelloWorld(string id)
          {
              Name=id;
      return string.Format("HelloWorld, {0}!", Name); } ... }

    SessionMode.Required ensures that your clients are session-tracked.
    InstanceContextMode.PerSession ensures that an instance of your service class (Service1) is created for every session, so that you can retain session data in it and it will exist in memory across multiple calls in the same session.
    ConcurrencyMode.Single ensures that only one thread can enter each service class instance (Service1), and prevents possible concurrency issues if you only access data from the service class (and external thread-safe locations).

    EDIT: By default, WSHttpBinding only allows security sessions. But it also support reliable sessions, which allow establishing sessions without security enabled. The following binding configuration disables security and enables reliable sessions:

    <wsHttpBinding>
        <binding name="wsHttpBindingConfiguration">
            <security mode="None" />
            <reliableSession enabled="true" />
        </binding>
    </wsHttpBinding>
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, October 15, 2013 5:34 AM
    Moderator