Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4335668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:42:05+00:00 2026-05-21T10:42:05+00:00

Still struggling with the server part of my wcf application. The problem is that

  • 0

Still struggling with the server part of my wcf application.

The problem is that the creating a “callback” object within the server class cased a “System.NullReferenceException was unhandled” error.

If I understand right, it happens when I create this server object – ServerClass myServer = new ServerClass();

So I guess I should somehow create a list for server objects and create & add this objects automatically whenever a client makes a connection. Please suggest, what would be the best way of doing that?

Here’s my code so far:

namespace server2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            myServer.eventHappened += new EventHandler(eventFunction);
        }

        ServerClass myServer = new ServerClass();
        // here I instance a new server object.
        // yet I believe that I need to make a list, that would store this objects, so that multiple clients would be able to connect,
        // and I would be able to pick, to whom I want to send a callback.

        void eventFunction(object sender, EventArgs e)
        {
            label1.Text = myServer.clientName;
        }

        private ServiceHost duplex;

        private void Form2_Load(object sender, EventArgs e)     /// once the form loads, create and open a new ServiceEndpoint.
        {
            duplex = new ServiceHost(typeof(ServerClass));
            duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
            duplex.Open();
            this.Text = "SERVER *on-line*";
        }

    }




    class ServerClass : IfaceClient2Server
    {

        public event EventHandler eventHappened;

        IfaceServer2Client callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
        // ERROR: System.NullReferenceException was unhandled

        public string clientName = "noname";

        public void StartConnection(string name)
        {
            clientName = name;
            MessageBox.Show(clientName + " has connected!");

            eventHappened(this, new EventArgs());
            // ERROR: System.NullReferenceException was unhandled :(

            callback.Message_Server2Client("Welcome, " + clientName);
        }

        public void Message_Cleint2Server(string msg)
        {
        }

        public void Message2Client(string msg)
        {
        }

    }




    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]


    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);

        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);
    }


    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();

        [OperationContract(IsOneWay = true)]
        void RejectConnection();

        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);
    }

}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:42:06+00:00Added an answer on May 21, 2026 at 10:42 am

    This can not be done that way. First of all, the Callback Channel is only available within an operation, not when the instance of the service class is created.

    Secondly, the instances of your ServerClass are created by the WCF service host depending on your WCF configuration. For example, there may be one instance per call (!). So creating an instance yourself and attaching an event handler does not affect the automatically created instances. That’s why you get an exception in StartConnection.

    What I’d do in that case is:

    1. Create a singleton class that publishes the desired event
    2. Attach a handler to the event from within your main code. This will be the code that listens to events
    3. Create a public method (like ConnectionStarted) which raises the event
    4. Call this method from your ServerClass

    If you don’t need to wait for the event handler to finish, you can also raise the event asynchronously in a separate thread. You then have to make sure that the event handler attached in step 2) handles thread contexts properly using for example this.Invoke (Forms) or this.Dispatcher.BeginInvoke (WPF).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a common problem but I'm struggling to find a solution that
I'm still struggling with changing my Spring Application to use Hibernate with JPA to
I'm still struggling to find an answer to my question. I want to download
I am trying learn the use of lambda expressions and hence still struggling to
I see there are several posts regarding Cannot set Property and I'm still struggling
I have been struggling with this yesterday whole day, and still couldnt figure it
Still having issues with this problem. Please help if you can. So I am
I am struggling understanding what a clustered index in SQL Server 2005 is. I
we've bean struggling here at work by somebody suggestion that we should decrease the
I have been struggling with the best way to make sure that the certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.