在我的控制台应用程序中找不到类型或名称空间“ ServiceHost”

问题描述 投票:0回答:1

我正在尝试在控制台应用程序中实现自托管WCF服务。第一步是我首先尝试实现一个helloworld应用。但是,似乎我无法在System.ServiceModel中引用关键字或类。谁能告诉我我做错了什么?下面是我的代码。找不到关键字“ ServiceHost”。

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

}

c# visual-studio console-application self-hosting
1个回答
0
投票

。Net Core不再支持此功能。查看:https://github.com/dotnet/wcf/issues/2559

如果可以,则可以降级到.Net框架,也可以将另一个第三方库用于servicehost。

© www.soinside.com 2019 - 2024. All rights reserved.