2017-02-23 9 views
0

현재 C#의 정적 필드에 문제가있는 경우 EasyPost가 ClientManger를 인스턴스화하는 방식으로 문제가 발생하지만 정적 필드와 관련하여 더 잘 이해할 수있는 사람이 문제가 될 수 있습니다. 나를 도와주기 위해서.정적 필드 - EasyPost - ClientManager Init

필자는 여러 사용자가 소포를 추적하기 위해 EasyPost에 액세스 할 수 있도록하는 플러그인을 만들고 있습니다.

여러 사람이 동시에 사용하는 시나리오를 테스트하기 위해 단위 테스트를 작성했습니다.

단위 테스트 :

[TestMethod()] 
public void TestMultiInit() 
    { 
     var Client2 = new cpEasyPost("123"); 
     var Client = new cpEasyPost("123456qq785412"); 

     var Shipment = Client.CreateShipment(
      new EasyPost.Address 
      { 
       street1 = "417 MONTGOMERY ST", 
       street2 = "FLOOR 5", 
       city = "SAN FRANCISCO", 
       state = "CA", 
       zip = "94104", 
       country = "US", 
       company = "EasyPost" 
      }, 
      new EasyPost.Address 
      { 
       street1 = "417 MONTGOMERY ST", 
       street2 = "FLOOR 5", 
       city = "SAN FRANCISCO", 
       state = "CA", 
       zip = "94104", 
       country = "US", 
       company = "EasyPost" 
      }, 
      new EasyPost.Parcel 
      { 
       length = 20.2, 
       width = 10.9, 
       height = 5, 
       weight = 65.9 
      }); 

     var Shipment2 = Client2.CreateShipment(
      new EasyPost.Address 
      { 
       street1 = "417 MONTGOMERY ST", 
       street2 = "FLOOR 5", 
       city = "SAN FRANCISCO", 
       state = "CA", 
       zip = "94104", 
       country = "US", 
       company = "EasyPost" 
      }, 
      new EasyPost.Address 
      { 
       street1 = "417 MONTGOMERY ST", 
       street2 = "FLOOR 5", 
       city = "SAN FRANCISCO", 
       state = "CA", 
       zip = "94104", 
       country = "US", 
       company = "EasyPost" 
      }, 
      new EasyPost.Parcel 
      { 
       length = 20.2, 
       width = 10.9, 
       height = 5, 
       weight = 65.9 
      }); 


    } 

문제는 내가 시도하고 있기 때문에 ClinetManager 정적 인의 그러나되어 실패로 선적을 만들 그래서 만약 클라이언트 2는 잘못된 키를 가지고있다가 나중에 때문에 경우의 클라이언트 초기화를 사용합니다. 여기

내 ctor에 있습니다 :

public cpEasyPost(string secretKey) 
    {    
     SecretKey = secretKey; 
     //Original way of init Client 
     //ClientManager.SetCurrent(SecretKey); 

     //Create ClientManager 
     ClientManager.SetCurrent(() => new Client(new ClientConfiguration(SecretKey))); 
    } 

그리고 여기 내 방법 :

public Shipment CreateShipment(Address AddressFrom, Address AddressTo, Parcel Parcel, CustomsInfo customs = null, string CustomReference = "", double? InsauranceAmount = null) 
    { 
     //Validate Adress 
     var isValidFrom = ValidateAddress(AddressFrom); 
     var isValidTo = ValidateAddress(AddressFrom); 

     if (!isValidFrom.isSuccess) 
      throw new Exception("Address From is not Valid"); 

     if (!isValidTo.isSuccess) 
      throw new Exception("Address To is not Valid"); 

     //Validate Pacrcel 
     var isValidParcel = ValidateParcel(Parcel); 

     if (!isValidFrom.isSuccess) 
      throw new Exception("Parcel is not Valid"); 

     //Create Shipment 
     Shipment shipment = new Shipment() 
     { 
      reference = CustomReference, 
      to_address = AddressTo, 
      from_address = AddressFrom, 
      parcel = Parcel, 
      customs_info = customs    
     }; 

     //ClientManager.SetCurrent(SecretKey); ** 
     shipment.Create(); 

     //Add Insurance 
     if (InsauranceAmount != null) 
      shipment.Insure(InsauranceAmount.Value); 

     return shipment; 

    } 

나는 아니오 문제가 ClinetManager가 정적 인 것입니다,이 잠긴 DDL 그래서 난 할 수있다 ' 이것을 수정하십시오. 이 방법에서는 모든 호출 전에 관리자를 설정하는 것이 고려되었지만 이론적으로 이론적으로는 **로 표시된 이슈로 이어질 수 있기 때문에 이것이 최상의 솔루션처럼 보이지 않습니다.

도움을 주시면 감사하겠습니다. Adnvacne에 감사드립니다.

답변

0

결국 SDK의 코드를 내 필요에 더 잘 맞는 것으로 다시 작성합니다. 이 문제를 해결할 다른 방법은 없습니다.