Self-host WCF Service. Error: Cannot obtain Metadata from http://localhost/TaskService If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.

Golomazdina Natalya 21 Reputation points
2022-01-08T17:47:37.86+00:00

Hi. I have created a WCF service. And I'm trying to self-host it in Console App like this:
var builder = new ContainerBuilder();
builder.RegisterType<Repository>().As<IRepository>();
builder.Register(c => new TaskService
(c.Resolve<IRepository>())).As<ITaskService>();
var container = builder.Build();

        string baseaddr = "http://localhost/TaskService/";
        Uri baseAddress = new Uri(baseaddr);
        using (ServiceHost host = new ServiceHost(typeof(TaskService), baseAddress))
        {

            ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
            serviceBehavior.HttpGetEnabled = true;
            serviceBehavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(serviceBehavior);

            host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
            host.AddServiceEndpoint(typeof(ITaskService), new WSHttpBinding(), "");

            host.AddDependencyInjectionBehavior<ITaskService>(container);

            host.Open();
        }

When ConsoleApp is run, I'm trying to test my wcf by using WcfTestClient. And I get an error:
Error: Cannot obtain Metadata from http://localhost/TaskService If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.

Any help would be really appriciated!

Internet Information Services
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,782 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
334 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,166 Reputation points Microsoft Vendor
    2022-01-10T06:27:56.837+00:00

    Hi @Golomazdina Natalya ,
    I think you can create a try block that will catch any exceptions thrown while the service is running.
    Make sure baseaddr is correct.
    For metadata about using the code publishing service, you can refer to the tutorial.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-publish-metadata-for-a-service-using-code
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. B. Nolan 1 Reputation point
    2022-02-06T02:53:28.523+00:00

    You have got to be kidding me. Whomever is responsible for writing this WCF abomination should be put in a dungeon with Bill Gates. That also goes for whomever is ultimately responsible for the God-awful documentation at MS. Literally everything is incomplete, utterly unexplained, and/or simply does not work.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.