简体   繁体   中英

WCF Service application without gui that will stay alive

[WCF newbie]
I have a basic client-server WCF project.
My Service is "gui"less application, meaning that I created winform application, removed the Form1.cs and the lines that starts the gui.
The service is running ok, I am using servicehost.open..
My problem is that it is "serial" (sync), so after a second the application exists.
How can i keep the application alive and listening to the host ?
I need to halt the process and then to host.close when I want to end it. Thanks

This is code of service:

class Program
{
    public static Uri BaseAddress;
    [STAThread]
    static void Main(string[] args)
    {

        string baseAddressStr = "http://localhost:7000/someservice";
        BaseAddress = new Uri(baseAddressStr);
        using (ServiceHost host = new ServiceHost(typeof(MyClass)BaseAddress))
        {
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            host.Open();
            host.Close();
        }
    }
}

I don't think you want to host your WCF service in a windowless WinForms app. If you want it to stay open indefinitely, the way to go is to host the WCF service in a Windows Service. Here you have a basic sample.

The benefits should be obvious:

  1. Can run even if no user is logged in.
  2. Does not require a workaround for keeping the application open.

Additionally, you should consider externalizing your WCF configuration (like the base address, service behavior) to the application configuration file. You don't want to rebuild and redeploy your service each time something (anything) changes in the configuration, which may vary from development, test, acceptance and production environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM