我有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();
}
}
}
}