我正在尝试在控制台应用程序中实现自托管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();
}
}
}
}
。Net Core不再支持此功能。查看:https://github.com/dotnet/wcf/issues/2559
如果可以,则可以降级到.Net框架,也可以将另一个第三方库用于servicehost。