Exposing Multiple WCF Services from the Same AppDomain

Although you have no need to do so here, if you did wish to expose multiple WCF services from the same hosting
agent, you would need to do the following:
   •        Define multiple (uniquely named) <service> elements in your *.config file.
   •        Create multiple ServiceHost objects, sending (unique) type information into their constructors.
   •        Match the constructor name and the <service> name.

Assume you have a new WCF service named MyAdvancedCalc contained in a new WCF service library (named
WcfNewMathService). The executable’s Main() method could expose MyCalc and MyAdvancedCalc as follows:


// C#
static void Main(string[] args)
{
// Expose two WCF services from this app domain.
using (ServiceHost myServiceHost = new ServiceHost(typeof(WcfMathService.
MyCalc)),
  
 myOtherServiceHost = new ServiceHost(typeof(WcfNewMathService.
MyAdvancedCalc)))
{
  myServiceHost.Open();
  myOtherServiceHost.Open();

  Console.WriteLine("Hit Enter to shut down host");
  Console.ReadLine();
}
}




' VB
Sub Main()
' Expose two WCF services from this app domain.
Using myServiceHost As New _
  ServiceHost(GetType(WcfMathService.MyCalc)),
       myOtherServiceHost As New _
  ServiceHost(typeof(WcfNewMathService.MyAdvancedCalc))

  myServiceHost.Open()
  myOtherServiceHost.Open()

  Console.WriteLine("Hit Enter to shut down host")
  Console.ReadLine()
End Using
End Sub



The corresponding configuration file would look like this:


<?xml version = "1.0" encoding = "utf-8" ?>
<configuration>
<system.serviceModel>
<services>
 <service name = "WcfMathService.MyCalc">
   <endpoint address = "http://localhost:8080/MyCalc"
             binding = "wsHttpBinding"
             contract = "WcfMathService.IBasicMath"/>
 </service>

 
<service name = "WcfNewMathService.MyAdvancedCalc">
   <endpoint address = "http://localhost:8081/MyAdvancedCalc"
             binding = "wsHttpBinding"
             contract = "WcfNewMathService.IAdvacnedMath"/>
 </service>

</services>
</system.serviceModel>
</configuration>
Exposing Multiple WCF Services from the Same AppDomain
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services