将WCF服务配置保存到文件

时间:2014-11-10 12:39:50

标签: c# wcf

我有WCF服务控制台应用程序。所有服务设置都是硬编码的。我想看看这个配置在web.config文件中的外观。其实我需要system.serviceModel部分。如何将我的服务设置保存到文件?

namespace WCF_con
{

    [ServiceContract]
    interface IStringService
    {
        [OperationContract]
        string Reverse(string s);
    }

    class MyStringService : IStringService
    {
        public string Reverse(string s)
        {
            return new string(s.Reverse().ToArray());
        }
    }

    internal class Program
    {


        private static void Main(string[] args)
        {

            WSHttpBinding binding = new WSHttpBinding();
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.Security.Mode = SecurityMode.None;

            Uri baseAddress = new Uri("http://localhost:8000/StringService");

            using (ServiceHost serviceHost =
                new ServiceHost(typeof (MyStringService), baseAddress))
            {
                // Check to see if it already has a ServiceMetadataBehavior
                ServiceMetadataBehavior smb =
                    serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                    smb = new ServiceMetadataBehavior();

                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
                serviceHost.Description.Behaviors.Add(smb);

                // Add MEX endpoint
                serviceHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    "mex"
                    );

                serviceHost.AddServiceEndpoint(typeof (IStringService), binding, baseAddress);



                serviceHost.Open();

                Console.WriteLine("The service is running. Press any key to stop.");
                Console.ReadKey();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)