1

I am still struggling with the server part of my wcf application. The following code shows an example WCF Service. Method Getnews creates an instance of class TOInews and changes some of its values. The following code works without errors.

namespace WCF_NewsService
{
    public class News_Service : INews_Service
    {
        public TOInews Getnews()
        {
            TOInews objtoinews = new TOInews();

            objtoinews.ID = 1;
            objtoinews.Header = "Mumbai News";
            objtoinews.Body = "2013 Code contest quiz orgnize by TOI";

            return objtoinews;
        }
    }
}

The following code, in contrast, does not work. And I am wondering why. Now, I want to store the object objtoinews within my service. I do not want to create a new object each time I access Getnews(). Therefore, I create a method named Initnews(), which is only called once (at the beginning) by the client (consumer).

namespace WCF_NewsService
{
    public class News_Service : INews_Service
    {
        TOInews objtoinews;

        public TOInews Initnews()
        {
            objtoinews = new TOInews();
            return objtoinews;
        }

        public TOInews Getnews()
        {          
            objtoinews.ID = 1;
            objtoinews.Header = "Mumbai News";
            objtoinews.Body = "2013 Code contest quiz orgnize by TOI";

            return objtoinews;
        }
    }
}

When I run this code, I get a System.NullReferenceException, because, for some reason, objtoinews equals null. Can anyone tell me why?

EDIT: Here's how I call it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCF_NewsConsumer.Proxy_TOInews;

namespace WCF_NewsConsumer
{
    class Program
    {
        static void Main(string[] args)
        {
            Proxy_TOInews.News_ServiceClient proxy = new News_ServiceClient("BasicHttpBinding_INews_Service");
            TOInews Tnews = new TOInews();

            Tnews = proxy.Initnews();
            Tnews = proxy.Getnews();

            Console.WriteLine(" News from:" + Tnews.ID + "\r\r \n " + Tnews.Header + "\r\r \n " + Tnews.Body + "");
            Console.ReadLine();
        }
    }
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
Boozzz
  • 245
  • 1
  • 6
  • 19

1 Answers1

1

Just to clarify my comment - you'll need to use SessionMode=Required on your contract in order for the server to retain the state of the constructed object across multiple calls:

[ServiceContract(SessionMode=SessionMode.Required)]
public interface INews_Service
{
    // Initiating - indicates this must be called prior to non initiating calls
    [OperationContract(IsInitiating=true, IsTerminating=false)]
    TOInews Initnews();

    // You can choose whether this terminates the session (e.g. if more calls)
    [OperationContract(IsInitiating=false, IsTerminating=true)]
    TOInews Getnews();
}

You'll need to regenerate the client as well to update the bindings and proxy.

That said, WCF Sessions should be used sparingly, as they limit the scalability of your solution (since the server consumes threads and resources per session), and it also requires sticky session routing, as the session must be routed back to the same server in a multi-server scenario.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Thank you! That looks exactly like what I was looking for! However, when I add those statements, I get a `System.InvalidOperationException`. I use a selfhosting application, and I get the Exception when I call `host.Open();`. – Boozzz Jun 17 '14 at 09:03
  • You'll need to regenerate the client proxy and check both configs since you've changed the contract. The error may indicate that you are trying to use an [incompatable binding like basicHttpBinding](http://stackoverflow.com/q/4406972/314291). Suggest you compare the configs with the [sample here](https://github.com/nonnb/WcfSessionSample) – StuartLC Jun 17 '14 at 09:15