To expose a WCF service, you need to provide a hosting environment in which to run the service. Just as the. NET CLR needs to create a hosting environment in managed code, the hosting environment for WCF is also running in the application domain of the process. You can create one or more ServiceHost instances in an application domain, as shown in Figure one:
Figure A managed ServiceHost
WCF does not recommend creating multiple ServiceHost instances in the application domain. If you are hosting multiple services, you can expose multiple WCF services through multiple endpoint in one host. Because application domains are isolated from security, it is necessary to create multiple ServiceHost instances if you need to provide a different security context.
The typical hosting of WCF includes the following four types:
1, "self-hosting" in a Managed application (self hosting)
2. Managed Windows Services (Windows Services Host)
3. Internet Information Services (IIS host)
4. Windows Process activation Service (was host)
The following is a specific example of how these hosts are managed and their related considerations. In such an instance, we define the following service contracts:
namespace BruceZhang.WCF.DocumentsExplorerServiceContract
{
[ServiceContract]
public interface IDocumentsExplorerService
{
[OperationContract]
[FaultContract(typeof(DirectoryNotFoundException))]
DocumentList FetchDocuments(string homeDir);
[OperationContract]
Stream TransferDocument(Document document);
}
}