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

时间:2012-10-14 10:09:46

标签: .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();

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

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

2 个答案:

答案 0 :(得分:6)

WebServiceHost类基于ServiceHost类。

默认情况下,它附带WebHttpBindingWebHttpBehavior。 (好的是你不需要配置文件,只是为了简单使用。)

来自MSDN

  

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

答案 1 :(得分:1)

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