1

我有一个接口,对于同一个接口,我有多个实现。所以我想问你,我如何使用一台主机公开端点?

服务代码

[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;
    }
}

主机代码

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

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

主机配置

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

1 回答 1

3

尽管您没有说出来,但我假设您希望能够在单个应用程序中同时托管虚假和真实服务。如果是这样,您可以在单个应用程序中托管多个 WCF 服务。为此,您需要更改代码以创建多个主机。

代码更改

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();
    }
}

配置更改

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

<endpoint address="http://localhost:8000/FakeCalService"
          binding="basicHttpBinding"
          contract="WCF_Service.ICalculator" />
于 2013-09-06T13:42:23.757 回答