मेरा सहकर्मी और मैं एक डब्ल्यूसीएफ सेवा में एक समस्या को डीबग कर रहा हूं जिस पर वह काम कर रहा है जहां स्ट्रिंग की लंबाई का सही मूल्यांकन नहीं किया जा रहा है। उन्होंने कहा कि इकाई परीक्षण अपने WCF सेवा में एक विधि के लिए इस विधि चल रहा है:स्ट्रिंग की लंबाई गलत तरीके से मूल्यांकन करना
// Unit test method
public void RemoveAppGroupTest()
{
string addGroup = "TestGroup";
string status = string.Empty;
string message = string.Empty;
appActiveDirectoryServicesClient.RemoveAppGroup("AOD", addGroup, ref status, ref message);
}
// Inside the WCF service
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public void RemoveAppGroup(string AppName, string GroupName, ref string Status, ref string Message)
{
string accessOnDemandDomain = "MyDomain";
RemoveAppGroupFromDomain(AppName, accessOnDemandDomain, GroupName, ref Status, ref Message);
}
public AppActiveDirectoryDomain(string AppName, string DomainName)
{
if (string.IsNullOrEmpty(AppName))
{
throw new ArgumentNullException("AppName", "You must specify an application name");
}
}
हम नेट स्रोत कोड में कदम करने के लिए क्या मूल्य string.IsNullOrEmpty
प्राप्त था देखने के लिए कोशिश की, लेकिन आईडीई इस संदेश को मुद्रित जब हम करने का प्रयास किया चर का मूल्यांकन करें: 'स्थानीय या तर्क' मान 'का मान प्राप्त नहीं कर सकता क्योंकि यह इस निर्देश सूचक पर उपलब्ध नहीं है, संभवतः क्योंकि इसे अनुकूलित किया गया है।' (शामिल परियोजनाओं में से कोई भी अनुकूलन सक्षम नहीं है)। इसलिए, हमने लंबाई जांच से ठीक पहले विधि के अंदर चर के मूल्य को स्पष्ट रूप से सेट करने का प्रयास करने का निर्णय लिया - लेकिन इससे मदद नहीं मिली।
// Lets try this again.
public AppActiveDirectoryDomain(string AppName, string DomainName)
{
// Explicitly set the value for testing purposes.
AppName = "AOD";
if (AppName == null)
{
throw new ArgumentNullException("AppName", "You must specify an application name");
}
if (AppName.Length == 0)
{
// This exception gets thrown, even though it obviously isn't a zero length string.
throw new ArgumentNullException("AppName", "You must specify an application name");
}
}
हम वास्तव में इस पर अपने बालों को खींच रहे हैं। क्या किसी और ने ऐसा व्यवहार किया है? इसे डीबग करने पर कोई सुझाव?
.method public hidebysig specialname rtspecialname instance void .ctor(string AppName, string DomainName) cil managed
{
.maxstack 5
.locals init (
[0] class [System]System.Net.NetworkCredential ldapCredentials,
[1] string[] creds,
[2] string userName,
[3] class [mscorlib]System.ArgumentNullException exc,
[4] class [System.DirectoryServices]System.DirectoryServices.ActiveDirectory.DirectoryContext directoryContext,
[5] class [System.DirectoryServices]System.DirectoryServices.ActiveDirectory.Domain domain,
[6] class [System.DirectoryServices.Protocols]System.DirectoryServices.Protocols.LdapException V_6,
[7] class [mscorlib]System.Exception V_7,
[8] bool CS$4$0000,
[9] char[] CS$0$0001,
[10] string[] CS$0$0002)
L_0000: ldarg.0
L_0001: ldsfld string [mscorlib]System.String::Empty
L_0006: stfld string MyNamespace.MyClass.AppActiveDirectoryDomain::appOU
L_000b: ldarg.0
L_000c: call instance void [mscorlib]System.Object::.ctor()
L_0011: nop
L_0012: nop
L_0013: ldstr "AOD"
L_0018: call bool [mscorlib]System.String::IsNullOrEmpty(string)
L_001d: ldc.i4.0
L_001e: ceq
L_0020: stloc.s CS$4$0000
L_0022: ldloc.s CS$4$0000
L_0024: brtrue.s L_0037
L_0026: nop
L_0027: ldstr "AppName"
L_002c: ldstr "You must specify an application name"
L_0031: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string, string)
L_0036: throw
और MSIL string.IsNullOrEmpty
कॉल के लिए:
.method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: brfalse.s L_000d
L_0003: ldarg.0
L_0004: callvirt instance int32 System.String::get_Length()
L_0009: ldc.i4.0
L_000a: ceq
L_000c: ret
L_000d: ldc.i4.1
L_000e: ret
}
संपादित करें:
यहाँ AppActiveDirectoryDomain
वस्तु है, जहां व्यवहार घटित है के लिए MSIL है
इसके अलावा, जब स्ट्रिंग की लंबाई की जाँच के बाद स्पष्ट रूप से घोषणा कर एक दूसरे अपवाद दिखा स्क्रीनशॉट फेंके:
यहाँ इस समय 'घड़ी' विंडो में चर का एक स्क्रीनशॉट ArgumentNullException फेंक दिया जाता है है यह ऊपर 5 लाइनें: http://imgur.com/lSrk9.png
अद्यतन # 3: हमने स्थानीय चर के नाम को बदलने की कोशिश की और यह शून्य जांच और लंबाई की जांच पास करता है, लेकिन जब हम string.IsNullOrEmpty
पर कॉल करते हैं तो विफल हो जाता है। यह स्क्रीनशॉट देखें: http://imgur.com/Z57AA.png।
जवाब:
हम किसी भी उपकरण है कि MSIL संशोधित करेगा प्रयोग नहीं करते। हमने एक क्लीनअप किया है, और बिल्ड निर्देशिकाओं से सभी फ़ाइलों को मैन्युअल रूप से हटा दिया है, और पुनर्निर्माण के लिए मजबूर किया है ... एक ही परिणाम।
निम्नलिखित कथन सत्य के रूप में मूल्यांकन करता है और यदि ब्लॉक:
if (string.IsNullOrEmpty("AOD")) { /* */ }
में प्रवेश करता है।निर्माता तो जैसे कहा जाता है:
try { using (AppActiveDirectoryDomain domain = new AppActiveDirectoryDomain(AppName, DomainName)) { } }
यह WCF सेवा विधि के भीतर ही तुरंत है, ऐपनाम और डोमेन नाम कॉल के पैरामीटर हैं। इन पैरामीटर को छोड़कर और नए तारों का उपयोग करके, हमें अभी भी त्रुटियां मिलती हैं।
यदि आप फेंकने वाले अपवाद पर ब्रेकपॉइंट डालते हैं, और 'ऐपनाम' और 'AppName.Length' को घड़ियों में जोड़ते हैं, तो वे क्या दिखाते हैं? –
वे 'AppName == "AOD" और' AppName.Length == 3 'दिखाते हैं - इसलिए वे सही ढंग से मूल्यांकन करते हैं। मैंने अपने प्रश्न के नीचे एक स्क्रीनशॉट संलग्न किया है। –
आप परीक्षण को भी कम अस्पष्ट बना सकते हैं: 'अगर ("एओडी"। तरंग == 0) फेंक ... '। यह आपके मुद्दों के स्रोत के रूप में परिवर्तनीय 'AppName' को रद्द कर देगा। यदि अपवाद तब भी फेंक दिया जाता है, तो आप इसके बारे में कुछ भी नहीं कर सकते हैं। – stakx