0

I'm trying to implement a self-host WCF service in a console app. The first step is that I tried to implement a helloworld app first. However, It seems I couldn't reference the key word or class in System.ServiceModel. Can anyone tell me what I did wrong? Below is my code. Keyword "ServiceHost" cannot be found now.

using System;
using System.ServiceModel;

namespace selfHost
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

public class HelloWorldService : IHelloWorldService
{
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:8080/hello");

        // Create the ServiceHost.
        using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
            // Enable metadata publishing.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            // Open the ServiceHost to start listening for messages. Since
            // no endpoints are explicitly configured, the runtime will create
            // one endpoint per base address for each service contract implemented
            // by the service.
            host.Open();

            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();

            // Close the ServiceHost.
            host.Close();
        }
    }
}

}

tk5566
  • 9
  • 4
  • This is a unexpected behavior. ServiceHost seems to be a really old thing, going all the way back to the Framework 1.1 and all across Core. So we have to look for the harder to debug cases: Maybe the parsing trips up so bad, it can not even tell you where there error is anymore? Are there any other Compiler Errors? | Did you add the proper .dll to the project reference. I doubt most consoel applications add System.ServiceModel.dll by default. However then it should complain at the using directive already. | Maybe it is a bug in Visual Studio. It is a programm too, after all. – Christopher Aug 01 '19 at 23:24
  • Did you tried using the fully qualified name of `System.ServiceModel.ServiceHost`? – Christopher Aug 01 '19 at 23:26
  • Hi @Christopher I tried fully qualified name too but it still has the same compiled error. – tk5566 Aug 01 '19 at 23:33
  • That leaves a general error in source code - wich I can not see here - wich trips up the Interpretation so badly it can not tell you where it tripped. Or some bug in the IDE. There should be a way to make a clean rebuild of the project, just to get rid of any temporary data that might be corrupted and cause this issue. – Christopher Aug 01 '19 at 23:44

2 Answers2

0

This is no longer supported in .Net Core. Looking at: https://github.com/dotnet/wcf/issues/2559

You can downgrade to .Net framework if you can or use another 3rd party library for servicehost.

Abbas Soloki
  • 117
  • 10
0

Based on your code, I did a demo using the .NET framework, I didn't find any error, but if you're using the .NET Core, it doesn't work, have you tried re-referencing the System.ServiceModel ? There is a link about System.ServiceModel References ,hope it use for you. System.ServiceModel References

Ning ke
  • 1
  • 1