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

时间:2018-12-31 10:22:59

标签: c# wcf wsdl

创建了一个简单的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}");

这行得通,如您在下面看到的:

Working Listing

但是打开?wsdl url没有任何作用。我之前已经做过了,完全相同的设置,并且可以正常工作。我不知道为什么不这样做。任何帮助表示赞赏。我已经与Fiddler进行了检查,以查看原始响应,但是无论是否包含WSDL,它只会返回相同的响应。

Opening ?wsdl does nothing

1 个答案:

答案 0 :(得分:1)

我不确定您的示例中是MainService还是AutoMappedConfig.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();
            }