Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WCF problems

Status
Not open for further replies.

Gorkem

Programmer
Jun 18, 2002
259
CA
Hi all,

I am writing an application that uses WCF to enable secure calls from one computer to another.. However, I need to pass a reference from the "server" class to the WCF class so that the calling "client" can utilize and access properites from the "server".

I can't seem to do this.

On the server, when creating the WCF listener, I create the reference as a property in my WCF class. However, when my client connects, it creates a "new" instance of the WCF and my reference is now NULL.

Any help or work arounds?

I was thinking maybe creating a some sort of global class which doesn't need to be instanciated (no constructor) and have my IClass access references from that. Not sure though the exact way to go about this.

Cheers,

G.


Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Ok.. so I figured it out.. This was my solution:

I create a singleton class, this way it is always static and can be used globally throughout other classes.

I passed the reference of the Main class into the a MainClass property in the singleton class, and then referenced that in my WCF class.

Works like a charm.

Here is a quick and dirty code demo of the solution:

Singleton Class
Code:
    public class Singleton
    {
        private static Singleton instance;
        public Form1 frm;

        public Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }                      
        
    }

Calling Class
Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Singleton s = Singleton.Instance;

            s.frm = this;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChangeForm c = new ChangeForm();
            c.ChangeTitle("Testing 1 2 3 4");
        }
    }

    public class ChangeForm
    {
        public ChangeForm()
        {
        }

        public void ChangeTitle(string Title)
        {
            Singleton s = Singleton.Instance;

            s.frm.Text = Title;
        }

    }

I hope this helps with anyone else having the same problem.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top