ServiceHost和WebServiceHost之间的区别是什么?

Ada*_*Lee 5 .net asp.net-mvc wcf c#-4.0

对于.Net4,以下是否有任何区别

Uri baseAddress = new Uri("http://localhost:8080/test");
ServiceHost host = new ServiceHost(typeof(TestService), baseAddress);
host.Open();
Run Code Online (Sandbox Code Playgroud)

Uri baseAddress = new Uri("http://localhost:8080/test");
WebServiceHost host = new WebServiceHost(typeof(TestService), baseAddress);
host.Open();
Run Code Online (Sandbox Code Playgroud)

所有的书都推荐使用webServiceHost,但为什么我看不出差异?

小智 6

WebServiceHost课程以班级为基础ServiceHost.

它配备了WebHttpBindingWebHttpBehavior默认.(好的是你不需要配置文件,只是为了简单使用.)

来自MSDN:

当您使用WebServiceHost而不是ServiceHost时,它将使用基本HTTP地址为您自动创建Web端点,并使用WebHttpBehavior配置注入的端点


Jai*_*dra 1

当您拥有服务类型时,可以使用WebServiceHost(Object, Uri[])构造函数,并且可以在需要时创建它的新实例,即使您需要单例实例也是如此。ServiceHost(Object, Uri[])仅当您希望服务主机使用服务的特定单例实例时,才使用构造函数。