WCF-复杂配置

  两种模式,一个契约两个实现,两个契约一个实现。

服务类库

宿主

static void Main(string[] args)
        {
            ServiceHost sh1 = new ServiceHost(typeof(WCFComplexLib.OneImpTowInt.People));
            sh1.Open();
            Console.WriteLine("一个实现两个接口服务开启");
            ServiceHost sh2 = new ServiceHost(typeof(WCFComplexLib.OneIntTowImp.Student));
            sh2.Open();
            Console.WriteLine("一个接口两个实现  学生服务开启");
            ServiceHost sh3 = new ServiceHost(typeof(WCFComplexLib.OneIntTowImp.Teacher));
            sh3.Open();
            Console.WriteLine("一个接口两个实现  老师服务开启");
            Console.ReadKey();
        }

服务端配置文件

<system.serviceModel>
    <services>
      <!--两个契约一个实现服务-->
      <service behaviorConfiguration="customBehavior" name="WCFComplexLib.OneImpTowInt.People">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6001/"/>
          </baseAddresses>
        </host>
        <endpoint address="OneImpTowIntPeopleStudent" binding="basicHttpBinding" contract="WCFComplexLib.OneImpTowInt.IStudent"></endpoint>
        <endpoint address="OneImpTowIntPeopleTeacher" binding="basicHttpBinding" contract="WCFComplexLib.OneImpTowInt.ITeacher"></endpoint>
      </service>
      
      <!--两个实现一个契约  学生服务-->
      <service behaviorConfiguration="customBehavior" name="WCFComplexLib.OneIntTowImp.Student">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6002/" />
          </baseAddresses>
        </host>
        <endpoint address="OneIntTowImpStudent" binding="basicHttpBinding" contract="WCFComplexLib.OneIntTowImp.IPeople"></endpoint>
      </service>

      <!--两个实现一个契约  老师服务-->
      <service behaviorConfiguration="customBehavior" name="WCFComplexLib.OneIntTowImp.Teacher">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6003/" />
          </baseAddresses>
        </host>
        <endpoint address="OneIntTowImpTeacher" binding="basicHttpBinding" contract="WCFComplexLib.OneIntTowImp.IPeople"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="customBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端

        static void Main(string[] args)
        {
            OneImpTwoIntPeople.StudentClient oitnps = new OneImpTwoIntPeople.StudentClient();
            Console.WriteLine(oitnps.GetStudentName(1));

            OneImpTwoIntPeople.TeacherClient oitnpt = new OneImpTwoIntPeople.TeacherClient();
            Console.WriteLine(oitnpt.GetTeacherName(1));

            OneIntTowImpStudent.PeopleClient oitisp = new OneIntTowImpStudent.PeopleClient();
            Console.WriteLine(oitisp.GetPeopleName(1));
            OneIntTowImpTeacher.PeopleClient oititp = new OneIntTowImpTeacher.PeopleClient();
            Console.WriteLine(oititp.GetPeopleName(1));

            Console.ReadKey();
        }

源码下载

原文地址:https://www.cnblogs.com/wudequn/p/7088948.html