我有一个接口,对于同一个接口,我有多个实现。 所以我想问你,如何使用一台主机公开端点?
服务代码
[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" />
答案 0 :(得分: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" />