Search code examples
c#wcfwsdl

?wsdl does not work, even with HttpGetEnabled


Created a simple WCF Service

Interface:

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

Service:

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);
        }
    }
}

Console app to host it:

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}");

And this works, as you can tell below:

Working Listing

But opening the ?wsdl url does nothing. I have done this before, exact same setup, and it just works. I have no clue why this doesn't. Any help is appreciated. I've checked with Fiddler to see the raw response, but it just returns the same response with and without the WSDL.

Opening ?wsdl does nothing


Solution

  • I am not sure what MainServiceis in your example or AutoMappedConfig.WcfHostAddress, But I know you need the MetaExchange piece for the wsdl to be accessible.

    Try it like this:

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