views:

106

answers:

2

Hey all,

I need to make some connections on startup of a server. I'm using the wcf technology for this client-server application. The problem is that the constructor of the server isn't called at any time, so for the moment, i initialize the connections when the first client makes a connection. But this generates problems in a further part.

This is my server setup:

private static ServiceHost _svc;

    static void Main(string[] args)
    {
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
        Uri address = new Uri("net.tcp://localhost:8000");
        _svc = new ServiceHost(typeof(MonitoringSystemService), address);
        publishMetaData(_svc, "http://localhost:8001");
        _svc.AddServiceEndpoint(typeof(IMonitoringSystemService), binding, "Monitoring Server");
        _svc.Open();

        Console.WriteLine("Listener service gestart op net.tcp://localhost:8000/Monitoring");
        Console.ReadLine();
    }

    private static void publishMetaData(ServiceHost svc, string sEndpointAddress)
    {
        ServiceMetadataBehavior smb = svc.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (smb != null)
        {
            smb.HttpGetEnabled = true;
            smb.HttpGetUrl = new Uri(sEndpointAddress);
        }
        else
        {
            smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.HttpGetUrl = new Uri(sEndpointAddress);
            svc.Description.Behaviors.Add(smb);
        }
    }

How can i start the server without waiting for a client to logon so i can initialize it.

Thanks in advance.

A: 

There are two ways I can immediately think of.

One - you can implement your solution as windows service

and Second - let a dummy client program call your server at start-up.

this. __curious_geek
in developping stage, i'm using it as console app, but i intend to convert it to a service as soon as it's running flawlessly.
djerry
In that case I suggest you write a dummy client to call your server upon startup.
this. __curious_geek
A: 

WCF will instantiate your MonitoringSystemService class as needed. It won't instantiate it until the first client makes a connection, and if you get a lot of client connections all at once, it will instantiate a few MonitoringSystemServices to deal with the load.

You can disable this behaviour, and instead just use one instance of MonitoringSystemService that gets created when your program starts. Instead of telling WCF which type it should be automatically instantiating, you just instantiate it yourself and pass it in:

_svc = new ServiceHost(new MonitoringSystemService()), address);

You gain control of when the MonitoringSystemService contructor runs, at the expense of scalability.

Alternatively (if you need the scalability), you could "initialize the connections" in your Main method, but be aware that WCF could instantiate multiple MonitoringSystemServices that would need to share those connections.

Joe Daley
Great, what was i looking for.But what do you mean by scalability?And if i choose the option where i instantiate it myself, will there be multiple instances of MonitoringSystemService as well?
djerry
If you choose to instantiate MonitoringSystemService yourself, your program will have just the one instance that you create. ServiceHost will not create any more. If multiple clients call your service at the same time, their requests will be queued by WCF and processed one-by-one by your single instance of MonitoringSystemService. This could be slow if you have a lot of clients connecting at the same time (in other words, it is not scalable). But if you will only have a small number of clients connecting at a time, it's not a problem - go for the instantiate-it-yourself option.
Joe Daley
jups, i'm doing it right now and it solves the issue i had.There will be no case where alot of clients will connect, so not a problem for me :)Thank you.
djerry