托管在两个不同服务器中的访问服务

时间:2019-07-01 18:27:08

标签: c# .net wcf

我在http://serverA.com/service.svc上托管了带有basichttpbinding的WCF服务,客户端代码为

class myapp{
    private servicereference1.ICar _car;
    public myapp(servicereference1.ICar car){
        _car = car; //DI
    }

    public void getCarDetails(servicereference1.ICarRequest carRequest){
        ..
        ..
        ..
        var response = carRequest.GetDetails(carRequest);
        ..
        ..
        ..
    }
}

但是现在我在http://serverB.com/service.svc有另一台具有相同代码库/服务的服务器,并且希望在客户端执行以下操作,而无需重复代码或添加额外的方法来调用serverB的GetDetails()

class myapp{
    private servicereference1.ICar _car;
    public myapp(servicereference1.ICar car){
        _car = car; //DI
    }

    public void getCarDetails(servicereference1.ICarRequest carRequest, string selectServer){
        ..
        ..
        ..
        var response = (selectServer == "A") ? GET_SERVER_A.GetDetails(carRequest) : GET_SERVER_B.GetDetails(carRequest);
        ..
        ..
        ..
    }
}

是否可以做类似的事情?

这就是我一直坚持的目标

class myapp{
    private servicereference1.ICar _car;
    private servicereference2.ICar _car2;

    public myapp(servicereference1.ICar car, servicereference2.ICar car2){
        _car = car; //DI
        _car2 = car2;
    }

    public void getCarDetails(servicereference1.ICarRequest carRequest, string selectServer){ 
        ..
        ..
        ..
        var response = (selectServer == "A") ? servicereference1.GetDetails(carRequest) : servicereference2.GetDetails(carRequest);

        //Error at servicereference2.GetDetails(carRequest); since carRequest is servicereference1.ICarRequest and not servicereference2.ICarRequest
        ..
        ..
    }
}

0 个答案:

没有答案