我怎么能初始化启动时的服务器? [英] How can i initialise a server on startup?

查看:199
本文介绍了我怎么能初始化启动时的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在服务器启动一些连接。我使用WCF技术,此客户端 - 服务器应用程序。问题是,服务器的构造是不是随时调用,因此就目前而言,我初始化连接时,第一个客户端进行连接。但是这会产生在进一步的部分的问题。

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.

这是我的服务器设置:

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.

先谢谢了。

推荐答案

根据需要WCF将实例你MonitoringSystemService类。直到第一个客户端进行连接,不会实例化它,如果你得到了很多客户端连接的一下子,它将实例几个MonitoringSystemServices处理负载。

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.

您可以禁用此行为,而不是仅仅使用你的程序启动时被创建MonitoringSystemService的一个实例。不是告诉WCF哪种类型应该自动初始化的,你只是你自己初始化它,并将它传递的:

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);

您获得的控制权,当MonitoringSystemService构造器运行时,在可扩展性为代价的。

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

或者(如果你需要的可扩展性),你可以初始化连接你的主要方法,但是要知道,WCF可以实例,将需要共享这些连接多个MonitoringSystemServices。

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.

这篇关于我怎么能初始化启动时的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆