简体   繁体   中英

How can I automatically configure WCF service on user's machine

I have an existing .NET application that controls external hardware. I am looking into extending some of the functionality that already exists on the PC to a smartphone app that will be used exclusively over a local network. This is not an enterprise system installed in a single location, it is a system sold to the public. WCF looks like a great solution, but if I'm going to have to walk users through manually setting up the service, configuring IIS, etc, that's a showstopper. How can I programatically deploy a WCF service so it is visible on a local network?

WCF can be hosted several different ways. Here is a great article that should get you going. You can jump to the section called "Exploring Your Hosting Options".

I've got it figured out. There are obviously multiple hosting methods, as Code Chops pointed out. For my requirements, I just need a self hosted solution that is running when the program I'm extending is running. I also used C# exclusively, with no xml configuration. This allows me to programmatically determine the local IP address (not shown). This all runs in a normal console app.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Web;


    namespace SelfHost
    {
        class Program
        {
            static void Main(string[] args)
            {     
                string localIP = "192.168.1.5";
                string port = "8001";
                Uri baseAddress = new Uri("http://" + localIP + ":" + port + "/hello");

                using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
                {                       
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                    host.Description.Behaviors.Add(smb);
                    host.AddServiceEndpoint(typeof(IHelloWorldService), new WebHttpBinding(), "");
                    host.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });                
                    host.Open();

                    Console.WriteLine("The service is ready at {0}", baseAddress);
                    Console.WriteLine("Press <Enter> to stop the service.");
                    Console.ReadLine();

                    // Close the ServiceHost.
                    host.Close();
                }
            }
        }

        [ServiceContract]
        public interface IHelloWorldService
        {
            [OperationContract]
            [WebGet(UriTemplate = "SayHello/{name}")]
            string SayHello(string name);

            [OperationContract]
            [WebGet(UriTemplate = "SayGoodbye/{name}")]
            string SayGoodbye(string name);
        }

        public class HelloWorldService : IHelloWorldService
        {
            public string SayHello(string name)
            {
                return string.Format("Hello, {0}", name);
            }

            public string SayGoodbye(string name)
            {
                return string.Format("Goodbye, {0}", name);
            }

        }


    }

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