1
votes

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

1
If you are not getting an exception then it indicates the service is running. Do you have another service running on the machine? You may be calling a different service that isn't responding with the console write.jdweng
Nope, it's the specific service (see the named response). Had to add a urlacl, etc. Also, this machie is just a few days old, so no legacy stuff :)NKCSS
Did you install all the latest update to machine?jdweng
there may be a mistake in your code snippets.please ensure that MainService is the alias of Usagelogger and AutoMappedConfig.WcfHostAddress is uri format string.Abraham Qian

1 Answers

1
votes

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