System.ArgumentNullException未被用户代码处理 [英] System.ArgumentNullException was unhandled by user code

查看:112
本文介绍了System.ArgumentNullException未被用户代码处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解决此异常,希望从此处获得帮助,谢谢

 受保护 < span class =code-keyword> override  WorkflowCreationContext OnGetCreationContext( object  [] inputs,OperationContext operationContext,Guid instanceId,WorkflowHostingResponseContext responseContext)
{
WorkflowCreationContext creationContext = new WorkflowCreationContext();

if (operationContext.IncomingMessageHeaders.Action.EndsWith( StartWorkflow))
{
IDictionary< string,object> arguments =(IDictionary< string,object>)输入[ 1 ];

if (arguments!= null && arguments.Count < span class =code-keyword>> 0
{
foreach (KeyValuePair< string,object> pair in arguments)
{
if (pair.Value DTOWrapper)
{
// Unwrap()中的错误
creationContext.WorkflowArguments.Add(pair.Key,(pair.Value as DTOWrapper).Unwrap());
}
else
creationContext.WorkflowArguments.Add(pair.Key,pair.Value);
}
}
}
其他
{
new Logger(_connectionString)
.Log((Guid)输入[ 0 ], 无效的操作: + operationContext.IncomingMessageHeaders.Action);
}

responseContext.SendResponse( null null );

return creationContext;
}





类DTOWrapper:

 [Serializable] 
[DataContract]
public class DTOWrapper
{
public DTOWrapper();
public DTOWrapper( object obj);

[DataMember]
public string AssemblyName {获得; set ; }
[DataMember]
public string 数据{获得; set ; }
[DataMember]
public string DataType {获得; set ; }

public object Unwrap();
}





然后抛出异常:

系统。用户代码未处理ArgumentNullException 
HResult = -2147467261
消息=值不能为空。
参数名称:type
Source = System.Runtime.Serialization
ParamName = type
StackTrace:
at System.Runtime.Serialization.XmlObjectSerializer.CheckNull(Object obj,字符串名称)
at System.Runtime.Serialization.DataContractSerializer.Initialize(Type type,IEnumerable`1 knownTypes,Int32 maxItemsInObjectGraph,Boolean ignoreExtensionDataObject,Boolean preserveObjectReferences,IDataContractSurrogate dataContractSurrogate,DataContractResolver dataContractResolver,Boolean serializeReadOnlyTypes)
at System .Runtime.Serialization.DataContractSerializer..ctor(Type type,IEnumerable`1 knownTypes,Int32 maxItemsInObjectGraph,Boolean ignoreExtensionDataObject,Boolean preserveObjectReferences,IDataContractSurrogate dataContractSurrogate,DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer..ctor(Type type,IEnumerable`1 knownTypes,Int32 maxItemsInObjectGraph,Boolean ignoreExtensi onDataObject,Boolean preserveObjectReferences,IDataContractSurrogate dataContractSurrogate)
at System.Runtime.Serialization.DataContractSerializer..ctor(Type type,IEnumerable`1 knownTypes)
at System.Runtime.Serialization.DataContractSerializer..ctor(Type type )
at Achievo.ServiceManager.ServiceModel.DTOWrapper.Deserialize(Type t,String data)
at Achievo.ServiceManager.ServiceModel.DTOWrapper.Unwrap()
at Achievo.Workflow.Runtime.WorkflowRuntimeEndpoint .OnGetCreationContext(Object [] input,OperationContext operationContext,Guid instanceId,WorkflowHostingResponseContext responseContext)
at System.ServiceModel.Activities.Dispatcher.PersistenceContext.GetInstance(WorkflowGetInstanceContext parameters)
at System.ServiceModel.Activities.Dispatcher。 DurableInstanceManager.GetInstanceAsyncResult.TryAcquire(Boolean fromCache)
at System.ServiceModel.Activities.Dispatcher.DurableInstanceManager.GetI在System.Runtime.AsyncResult.AsyncCompletionWrapperCallback(IAsyncResult result)$ n $ b InnerException:

Hello tanghuawei,



System.ArgumentNullException是因为您忘记检查代码或用户是否将值代码传递为null。



调试代码,看看什么是NULL,并对待它。如果它意味着这样,你可以在变量和标准值之后添加一个?? xx代替你想要使用的xx。



示例

 字符串 aString = MethodReturningString()??  ; 
//

// nullable int
int ? NUM;

MethodThatPassesInt(num ?? 5 );





如果你真的需要传递一些东西,那么也许你应该确保你有数据可以使用。执行if / else子句或try / catch;谁能更好地服务于你的目的


感谢您的帮助,但在工作流程中启动的相同xml文件解包很好,在运行时终点处解包抛出ArgumentNullException。



Xml文件:

 <   donationrequestdto     xmlns:i   =  http://www.w3.org/2001/XMLSchema-instance    xmlns   =  http://schemas.datacontract。 org / 2004/07 / Achievo.Service.WorkFlowForm.Contract >  <   createdby     i:nil   =  true    /  >  <   createddate     i:nil   =  true    /  >  <   updatedby     i:nil   =  true     /  >  <   updateddate     i:nil   =  true    /  >  <  金额 >  11 <   / amountoffunding  >  < ;  申请人 > 管理员<   / applicant  >  <   applicantid  >  2 <  < span class =code-leadattribute> / applicantid  > ;  <   approveridsearch  >  0 <   / approveridsearch  >  <   approvernamesearch     i:nil   =  true    /  >   <   approvernames     i:nil   =  true    /  >  <   approvingstatus     i:nil   =  true    /  >  <  助理 > 管理员<   / assistant  >  <   assistantid  >  2 <   / assistantid  >  <   assoinfo     i:nil   =  true    /  >  < ;   authorid  >  2 <   / authorid  >  <  预算 >  false <  < span class =code-leadattribute> / budgeted  >  <  类别 > 一般捐款<   / category  >  <   dd  >  11111 <   / dd  >  < ;   dateofrequestororrecipientrequest  >  2014-04-30T00:00:00 <   / dateofrequestororrecipientrequest  >  <   descriptionofhowfundwillbeused  >  111 <   / descriptionofhowfundwillbeill  >  < ;   detaileddescriptionoftype  >  1111 <   / detaileddescriptionoftype  >  <   duediligenceperformeddate  >  2014-04-30T09:16:06 <   / duediligenceperformeddate  >  <   editingstatus  > 草案<   / editingstatus  >  <   explain  >  false < ;   / explain  >  <   explainmakingproposeddonation  >  111 <   / explainmakingproposeddonation  >  <   filedescription     i:nil   =  true    /  >  <   FormName  > 捐款<   / FormName  >  <   fromformid     i:nil   =  true    /  >  <   id  >  310 <   / id  >  <  < span class =code-leadattribute> intvalueofeditingstatus  >  1 <   / intvalueofeditingstatus  >  <   nature  >  Nature1 <   / nature  >  <   natureid  >  1 <   / natureid  >  < ;   nextduediligencereviewdate  >  2014-04-30T09:16:07 <   / nextduediligencereviewdate  >  <   ownerid  >  2 <   / ownerid  >  < ;   postevent     i:nil   =  true    /  > ;  <   productbrandids     xmlns:d2p1   =  http://schemas.microsoft .com / 2003/10 /序列化/数组 >  <   d2p1:int  >  9 <   / d2p1:int  >  <   / productbrandids  >  <   productbrandnames  >  Cubicin <   / productbrandnames  >  <   proposalenddateofsupport  >  2014-04-30T00: 00:00 + 08:00 <   / proposalenddateofsupport  > ;  <   proposalenddateofsupportfrom  >  0001-01-01T00:00:00 <   / proposalenddateofsupportfrom  >  <   proposalenddateofsupportto   >  2014-05-01T00:00:00 <   / proposalenddateofsupportto  >  <   proposedstartdateofsupport  < span class =code-keyword>>  2014-04-30T00:00:00 + 08:00 <   / proposalstartdateofsupport  >  <  建议启动支持 >  0001-01-01T00:00:00 <   / proposedstartdateofsupportfrom  >  <   proposalstartdateofsupportto  >  2014-04-30T00:00:00 <   / proposedstartdateofsupportto  >  <   recipientid  >  3 <   / recipientid  >  <  收件人名称 > 反对虐待儿童有限公司<  < span class =code-leadattribute> / recipientname  >  <  备注    i:nil   =  true    /  >  <   representationorsuggestion     i:nil   =  true    /  >  <   requestno  >  EG14000057  <   / requestno  >  <   revisionno     i:nil   =  true    /  >  <   showstatusstring     i:nil   =  true    /  >  <   sort     i:nil   =  true    /  >  <   sortdir  < span class =code-attribute>   i:nil   =  true    /  >  <   specificifothers     i:nil   =  true    /  >  <   status     i:nil   =  true    /  >   <   submitdate  >  2014-04-30T09:16:19.6339099 + 08:00 <   / submitdate  >  <   submitdatefrom  >  0001-01-01T00:00:00 <   / submitdatefrom  >  <   submitdateto  > ;  0001-01-01T00:00:00 <   / submitdateto  >  <   supportfiletoattach     /  >  <  紧急 > 正常<   / urgent  >  <   usedfunddescription     i:nil   =  true    /  >  <   userid  >  2 <   / userid  >  < span class =code-keyword><   workflowinstanceid  >  0 <   / workflowinstanceid  >  <   workflowname  > 捐款<   / workflowname  >  <   workflowversionid  >  41 <   / workflowversionid  >  <   / donationrequestdto  >  


How can i solve this exception,i hope get help from here,thanks

protected override WorkflowCreationContext OnGetCreationContext(object[] inputs, OperationContext operationContext, Guid instanceId, WorkflowHostingResponseContext responseContext)
        {
            WorkflowCreationContext creationContext = new WorkflowCreationContext();

            if (operationContext.IncomingMessageHeaders.Action.EndsWith("StartWorkflow"))
            {
                IDictionary<string, object> arguments = (IDictionary<string, object>)inputs[1];

                if (arguments != null && arguments.Count > 0)
                {
                    foreach (KeyValuePair<string, object> pair in arguments)
                    {
                        if (pair.Value is DTOWrapper)
                        {
                            //Error in Unwrap()
                            creationContext.WorkflowArguments.Add(pair.Key, (pair.Value as DTOWrapper).Unwrap());
                        }
                        else
                            creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
                    }
                }                
            }            
            else
            {
                new Logger(_connectionString)
                    .Log((Guid)inputs[0], "Invalid Action: " + operationContext.IncomingMessageHeaders.Action);
            }

            responseContext.SendResponse(null, null);

            return creationContext;
        }   



Class DTOWrapper:

[Serializable]
    [DataContract]
    public class DTOWrapper
    {
        public DTOWrapper();
        public DTOWrapper(object obj);

        [DataMember]
        public string AssemblyName { get; set; }
        [DataMember]
        public string Data { get; set; }
        [DataMember]
        public string DataType { get; set; }

        public object Unwrap();
    }



Then throw exception:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: type
  Source=System.Runtime.Serialization
  ParamName=type
  StackTrace:
       at System.Runtime.Serialization.XmlObjectSerializer.CheckNull(Object obj, String name)
       at System.Runtime.Serialization.DataContractSerializer.Initialize(Type type, IEnumerable`1 knownTypes, Int32 maxItemsInObjectGraph, Boolean ignoreExtensionDataObject, Boolean preserveObjectReferences, IDataContractSurrogate dataContractSurrogate, DataContractResolver dataContractResolver, Boolean serializeReadOnlyTypes)
       at System.Runtime.Serialization.DataContractSerializer..ctor(Type type, IEnumerable`1 knownTypes, Int32 maxItemsInObjectGraph, Boolean ignoreExtensionDataObject, Boolean preserveObjectReferences, IDataContractSurrogate dataContractSurrogate, DataContractResolver dataContractResolver)
       at System.Runtime.Serialization.DataContractSerializer..ctor(Type type, IEnumerable`1 knownTypes, Int32 maxItemsInObjectGraph, Boolean ignoreExtensionDataObject, Boolean preserveObjectReferences, IDataContractSurrogate dataContractSurrogate)
       at System.Runtime.Serialization.DataContractSerializer..ctor(Type type, IEnumerable`1 knownTypes)
       at System.Runtime.Serialization.DataContractSerializer..ctor(Type type)
       at Achievo.ServiceManager.ServiceModel.DTOWrapper.Deserialize(Type t, String data)
       at Achievo.ServiceManager.ServiceModel.DTOWrapper.Unwrap()
       at Achievo.Workflow.Runtime.WorkflowRuntimeEndpoint.OnGetCreationContext(Object[] inputs, OperationContext operationContext, Guid instanceId, WorkflowHostingResponseContext responseContext)
       at System.ServiceModel.Activities.Dispatcher.PersistenceContext.GetInstance(WorkflowGetInstanceContext parameters)
       at System.ServiceModel.Activities.Dispatcher.DurableInstanceManager.GetInstanceAsyncResult.TryAcquire(Boolean fromCache)
       at System.ServiceModel.Activities.Dispatcher.DurableInstanceManager.GetInstanceAsyncResult.HandleEndLoad(IAsyncResult result)
       at System.Runtime.AsyncResult.AsyncCompletionWrapperCallback(IAsyncResult result)
  InnerException:

解决方案

Hello tanghuawei,

System.ArgumentNullException is because you forgot to check if a value code be passed as null by the code or the user.

Debug your code to see what is coming as NULL and treat it. If it meant to come like that, you can add a " ??xx " after the variable and standard value in the place of xx you would want to use instead.

example

String aString = MethodReturningString()?? " ";
//or

//nullable int
int? num;

MethodThatPassesInt(num??5);



If you something really needs to be passed, then maybe you should make sure you have data to work with. Do a if/else clause or a try/catch; the one who who better serves your purpose


thanks for you help,but the same xml file unwrap in workflow started is fine, unwrap at runtime end point throw ArgumentNullException.

Xml file:

<donationrequestdto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Achievo.Service.WorkFlowForm.Contract"><createdby i:nil="true" /><createddate i:nil="true" /><updatedby i:nil="true" /><updateddate i:nil="true" /><amountoffunding>11</amountoffunding><applicant>Administrator</applicant><applicantid>2</applicantid><approveridsearch>0</approveridsearch><approvernamesearch i:nil="true" /><approvernames i:nil="true" /><approvingstatus i:nil="true" /><assistant>Administrator</assistant><assistantid>2</assistantid><assoinfo i:nil="true" /><authorid>2</authorid><budgeted>false</budgeted><category>General Donation</category><dd>11111</dd><dateofrequestororrecipientrequest>2014-04-30T00:00:00</dateofrequestororrecipientrequest><descriptionofhowfundwillbeused>111</descriptionofhowfundwillbeused><detaileddescriptionoftype>1111</detaileddescriptionoftype><duediligenceperformeddate>2014-04-30T09:16:06</duediligenceperformeddate><editingstatus>Draft</editingstatus><explain>false</explain><explanationmakingproposeddonation>111</explanationmakingproposeddonation><filedescription i:nil="true" /><FormName>Donation</FormName><fromformid i:nil="true" /><id>310</id><intvalueofeditingstatus>1</intvalueofeditingstatus><nature>Nature1</nature><natureid>1</natureid><nextduediligencereviewdate>2014-04-30T09:16:07</nextduediligencereviewdate><ownerid>2</ownerid><postevent i:nil="true" /><productbrandids xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><d2p1:int>9</d2p1:int></productbrandids><productbrandnames>Cubicin</productbrandnames><proposeenddateofsupport>2014-04-30T00:00:00+08:00</proposeenddateofsupport><proposeenddateofsupportfrom>0001-01-01T00:00:00</proposeenddateofsupportfrom><proposeenddateofsupportto>2014-05-01T00:00:00</proposeenddateofsupportto><proposestartdateofsupport>2014-04-30T00:00:00+08:00</proposestartdateofsupport><proposestartdateofsupportfrom>0001-01-01T00:00:00</proposestartdateofsupportfrom><proposestartdateofsupportto>2014-04-30T00:00:00</proposestartdateofsupportto><recipientid>3</recipientid><recipientname>Against Child Abuse Ltd</recipientname><remarks i:nil="true" /><representationorsuggestion i:nil="true" /><requestno>EG14000057</requestno><revisionno i:nil="true" /><showstatusstring i:nil="true" /><sort i:nil="true" /><sortdir i:nil="true" /><specificifothers i:nil="true" /><status i:nil="true" /><submitdate>2014-04-30T09:16:19.6339099+08:00</submitdate><submitdatefrom>0001-01-01T00:00:00</submitdatefrom><submitdateto>0001-01-01T00:00:00</submitdateto><supportingfiletoattach /><urgent>Normal</urgent><usedfunddescription i:nil="true" /><userid>2</userid><workflowinstanceid>0</workflowinstanceid><workflowname>Donation</workflowname><workflowversionid>41</workflowversionid></donationrequestdto>


这篇关于System.ArgumentNullException未被用户代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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