How to add wcf service at runtime

前端 未结 3 1605
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 16:27

How can i add wcf service at runtime in my winform UI. I created a wcf service which return running processes of hosted machine. I want to add the hosted machine service in

3条回答
  •  青春惊慌失措
    2021-01-16 17:15

    If I understand you question properly you need some code that will add service in run-time without using any configuration in *.config file and *.svc files.

    See that sample:

        Uri baseAddress = new Uri("http://localhost:8080/hello");
        // Create the ServiceHost.
        using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
            // Enable metadata publishing.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        host.Description.Behaviors.Add(smb);
    
        // Open the ServiceHost to start listening for messages. Since
        // no endpoints are explicitly configured, the runtime will create
        // one endpoint per base address for each service contract implemented
        // by the service.
        host.Open();
    
        Console.WriteLine("The service is ready at {0}", baseAddress);
        Console.WriteLine("Press  to stop the service.");
        Console.ReadLine();
    
        // Close the ServiceHost.
        host.Close();
    }
    

    It creates self-hosted service in console app.

    http://msdn.microsoft.com/en-us/library/ms731758.aspx

    Is that what you asked?

提交回复
热议问题