?wsdl 不起作用,即使使用 HttpGetEnabled

?wsdl does not work, even with HttpGetEnabled

创建了一个简单的 WCF 服务

接口:

using System.ServiceModel;
namespace AsyncCollectorAndWorker
{
    [ServiceContract]
    public interface IUsageLogger
    {
        [OperationContract]
        void LogSearch(string term);
        [OperationContract]
        void LogSearchSuggestion(System.Guid id);
    }
}

服务:

using System;
namespace AsyncCollectorAndWorker
{
    public class UsageLogger : IUsageLogger
    {
        public void LogSearch(string term)
        {
            Console.WriteLine("{0} Search Term: '{1}'", DateTime.Now, term);
        }
        public void LogSearchSuggestion(Guid id)
        {
            Console.WriteLine("{0} Search Suggestion: '{1}'", DateTime.Now, id);
        }
    }
}

托管它的控制台应用程序:

host = new ServiceHost(typeof(MainService), new Uri(AutoMappedConfig.WcfHostAddress));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
Console.WriteLine($"Listeing on {AutoMappedConfig.WcfHostAddress}");

这是有效的,如下所示:

但是打开 ?wsdl url 没有任何作用。我以前做过这个,完全相同的设置,而且它很管用。我不知道为什么不这样。任何帮助表示赞赏。我已经检查过 Fiddler 以查看原始响应,但它只是 returns 使用和不使用 WSDL 的相同响应。

我不确定你的示例中的 MainServiceAutoMappedConfig.WcfHostAddress 是什么, 但我知道您需要 MetaExchange 部分才能访问 wsdl。

试试这样:

ServiceHost svcHost = new ServiceHost(typeof(UsageLogger), new Uri("http://localhost:15616/UsageLogger"));
            try
            {
                ServiceMetadataBehavior smb = svcHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

                if (smb == null)
                    smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                svcHost.Description.Behaviors.Add(smb);                
                svcHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");                
                svcHost.AddServiceEndpoint(typeof(IUsageLogger), new BasicHttpBinding(), "");                
                svcHost.Open();               
                Console.WriteLine("The service is ready.");                
                Console.ReadLine();                
                svcHost.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }