简体   繁体   English

从单个服务托管多个WCF ServiceContract实现

[英]Hosting multiple WCF ServiceContract implementations from a single service

I have a interface and for the same interface i have multiple implementation. 我有一个接口,对于同一个接口,我有多个实现。 so i would like to ask you that how do i expose the endpoint, using one host? 所以我想问你,如何使用一台主机公开端点?

SERVICE CODE 服务编号

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int num1, int num2);
}
public class Calculator : ICalculator
{
    public int Add(int num1, int num2)
    {
        return num1 + num2;
    }
}
public class Calculator_Fake : ICalculator
{
    public int Add(int num1, int num2)
    {
        return num1 + num1;
    }
}

HOST CODE 主机代码

class Program
{
    static void Main(string[] args)
    {

        ServiceHost host = new ServiceHost(typeof(WCF_Service.CalService));
        host.Open();
        Console.ReadLine();
    }
}

Host Config 主机配置

<endpoint address="http://localhost:8000/CalService"
          binding="basicHttpBinding"
          contract="WCF_Service.ICalculator" />

Although you don't say it I am assuming that you want to be able to host both the fake and the real service in a single applcation. 尽管您没有说出来,但我假设您希望能够在一个应用程序中同时托管假服务和真实服务。 If so you can host more than one WCF service in a single application. 如果是这样,您可以在一个应用程序中托管多个WCF服务。 In order to do so you will need to change the code so that it creates more than one host. 为此,您将需要更改代码,以使其创建多个主机。

Code change 代码变更

class Program
{
    static void Main(string[] args)
    {
        ServiceHost host1 = new ServiceHost(typeof(Calculator));
        host1.Open();

        ServiceHost host2 = new ServiceHost(typeof(Calculator_Fake));
        host2.Open();

        Console.ReadLine();
    }
}

Config change 配置变更

<endpoint address="http://localhost:8000/CalService"
          binding="basicHttpBinding"
          contract="WCF_Service.ICalculator" />

<endpoint address="http://localhost:8000/FakeCalService"
          binding="basicHttpBinding"
          contract="WCF_Service.ICalculator" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM