如何为两个环境质量检查和PROD环境进行编码.定义一个代理,提供对两个asmx http链接的两个Web引用 [英] How to code for two environment QA and PROD env. to define a proxy providing there are two web references to two asmx http links

查看:136
本文介绍了如何为两个环境质量检查和PROD环境进行编码.定义一个代理,提供对两个asmx http链接的两个Web引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#(VS2010、4.0 .net框架)的新手,希望在我的项目中拥有一个可以在QA与PROD Web服务HTTP/ASMX之间进行选择的类,我已经创建了两个Web服务引用在我的解决方案中(每个环境一个),现在我在代码中执行以下操作,但是也许有更好的方法,这是执行此逻辑的好方法吗?:

Hi,

I am new in C# (VS2010, 4.0 .net framework), want to be able to have a class within my project that have the ability to choose between a QA versus a PROD web service HTTP/ASMX, I already created two web service references within my solution (one per env.), now I am doing the following in my code but maybe there is a better way, is this a good way to do this logic?:

EDITranslatorQA.EDITranslator proxyQA = null;
EDITranslatorQA.EDIOptionsDTO optionsQA = null;       
EDITranslatorPROD.EDITranslator proxyPROD = null; 
EDITranslatorPROD.EDIOptionsDTO optionsPROD = null; 

// Below checking ENV variable in the Cache class (defined in app.config):
if (Cache.CacheInstance.ENV == "QA")
{
  proxyQA = new EDITranslatorQA.EDITranslator();
  optionsQA = new EDITranslatorQA.EDIOptionsDTO();
                optionsQA.doTranslate = true;
                optionsQA.getAudit = true;
 }
 else if (Cache.CacheInstance.ENV == "PROD")
 {
 proxyPROD = new EDITranslatorPROD.EDITranslator();
 optionsPROD = new EDITranslatorPROD.EDIOptionsDTO();
                optionsPROD.doTranslate = true;
 }
                
 var failCount = 0;
 var succeeded = false;

                while ((failCount < 2) && (!succeeded))
                {
                    try
                    {
                        if (Cache.CacheInstance.ENV == "PROD")
                        {
                         EDITranslatorPROD.EDIResponseDTO output = proxyPROD.TransVal(System.Convert.ToString(input), optionsPROD);
                         succeeded = true;
                         return (System.Convert.ToString(output.native.ToString()));
                        }
                        else if (Cache.CacheInstance.ENV == "QA")
                        {
                         EDITranslatorQA.EDIResponseDTO output = proxyQA.TransVal(System.Convert.ToString(input), optionsQA);
                         succeeded = true;
                         return (System.Convert.ToString(output.native.ToString()));
                        }
                    }
                    catch (WebException wex)
                    {.....etc. }

推荐答案

我已经看到它是这样做的.解决它不是一个坏方法.它很容易更改,但是这意味着在某些地方加倍编码,可能会变得很毛茸茸.

我还看到它是使用编译器指令完成的.

#IF调试
//使用质量检查服务
#ELSE
//使用PROD服务
#ENDIF

您也可以使用条件属性.我没有使用过,但是我已经读过,这是了解它的人的首选方法.

https://msmvps.com/blogs/peterritchie/archive/2011/11/24/if-you-re-using-if-debug-you-re-doing-it-wrong.aspx

使用此代码,您实际上将为QA编写一段代码,为PROD编写另一段代码,然后在此QA上添加此属性.这是MS的一些阅读材料.

http://msdn.microsoft.com/zh-CN/library/system.diagnostics.conditionalattribute.aspx
http://msdn.microsoft.com/zh-CN/library/aa664622(v=vs.71).aspx

让我们知道您的决定和使用方式.我会感兴趣的.我只是从来没有理由自己使用/学习它.只是了解一下.
I''ve seen it done this way. It''s not a bad way to solve it. It''s easy to change, but it means doubling the coding in places, which can get hairy.

I''ve also seen it done using compiler directives.

#IF DEBUG
//use the QA service
#ELSE
//use the PROD service
#ENDIF

You could also use Conditional Attribute. I haven''t used this, but I''ve read that this is the preferred method by those who know about it.

https://msmvps.com/blogs/peterritchie/archive/2011/11/24/if-you-re-using-if-debug-you-re-doing-it-wrong.aspx

Using this, you''d actually write one piece of code for QA and another for PROD, then on the QA piece you''d add this attribute. Here is some reading material from MS.

http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx
http://msdn.microsoft.com/en-us/library/aa664622(v=vs.71).aspx

Let us know what you decide and how you use it. I would be interested. I just have never had a reason to use it/learn it myself. Just know about it.


这篇关于如何为两个环境质量检查和PROD环境进行编码.定义一个代理,提供对两个asmx http链接的两个Web引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆