首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >静态字段- EasyPost - ClientManager初始化

静态字段- EasyPost - ClientManager初始化
EN

Stack Overflow用户
提问于 2017-02-23 15:07:05
回答 1查看 84关注 0票数 0

当涉及到C#中的静态字段时,我目前遇到了一个问题,问题出在EasyPost实例化ClientManger的方式上。然而,我认为对静态字段有更好理解的人可能能够帮助我。

我正在创建一个插件,允许多个用户访问EasyPost来跟踪包裹。

我已经编写了一个单元测试来测试多人同时使用它的场景。

单元测试:

代码语言:javascript
复制
[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
             });


    }

问题是Client2的密钥不正确,所以如果我尝试用它创建装运,应该会失败,因为ClinetManager是静态的,所以它使用客户端初始化,因为如果是稍后的话。

下面是我的代码:

代码语言:javascript
复制
public cpEasyPost(string secretKey)
    {            
        SecretKey = secretKey;
        //Original way of init Client
        //ClientManager.SetCurrent(SecretKey);

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

下面是我的方法:

代码语言:javascript
复制
 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,所以我不能修改它。在该方法中,我考虑在每次调用之前设置管理器,但这似乎不是最好的解决方案,因为从理论上讲,它仍然可能导致我用**标记的问题。

任何帮助都将不胜感激。谢谢你的支持。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-22 17:58:59

最后,我只是将他们的SDK中的代码重新构建为更适合我需求的东西。没有其他方法可以绕过这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42409135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档