Wcf序列化IEnumerable <keyvaluepair <key,value =“”>&gt;失败</keyvaluepair <键,>

时间:2014-08-21 07:32:19

标签: .net wcf serialization

我的代码如下:

// interfaces
[ServiceContract]
public interface IService1
{
    [OperationContract]
    MyResponse MyOperation(MyRequest request);
}

// services
public class Service1 : IService1
{
    public MyResponse MyOperation(MyRequest request)
    {
        // do nothing.
        throw new NotImplementedException();
    }
}

// request & data contracts
[DataContract]
public class MyRequest
{
    [DataMember(IsRequired = true)]
    public IEnumerable<KeyValuePair<Enums, string>> Settings { get; set; }
}

[DataContract]
public class MyResponse
{
    //...
}

[DataContract]
public enum Enums
{
    [EnumMember] E1,
    [EnumMember] E2
}

调用&#34; MyOperation&#34;。

时会出现错误

堆栈:

  

System.Reflection.TargetInvocationException未处理
  HResult = -2146232828消息=目标已抛出异常   一个调用。 Source = mscorlib StackTrace:          at System.RuntimeMethodHandle.InvokeMethod(Object target,Object [] arguments,Signature sig,Boolean constructor)          在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,   Object []参数,Object []参数)          在System.Delegate.DynamicInvokeImpl(Object [] args)          在System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry)   TME)          在System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)          在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry)   TME)          在System.Windows.Forms.Control.InvokeMarshaledCallbacks()          在System.Windows.Forms.Control.WndProc(消息&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr)   dwComponentID,Int32原因,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.Run(Form mainForm)          at Microsoft.Tools.TestClient.Program.Main(String [] args)InnerException:System.Reflection.TargetInvocationException          的HResult = -2146232828          消息=操作期间发生异常,导致结果无效。检查InnerException以获取异常详细信息。          来源=系统          堆栈跟踪:               在System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()               在System.ComponentModel.RunWorkerCompletedEventArgs.get_Result()               at Microsoft.Tools.TestClient.UI.MainForm.invokeServiceWorker_RunWorkerCompleted(Object   sender,RunWorkerCompletedEventArgs e)               在System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs)   E)               在System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object   ARG)          InnerException:System.ArgumentNullException               的HResult = -2147467261               Message = Value不能为null。参数名称:elementType               来源= mscorlib程序               PARAMNAME =元素类型               堆栈跟踪:                    在System.Array.CreateInstance(Type elementType,Int32 length)                    在Microsoft.Tools.TestClient.Variables.ArrayVariable.CreateObject()                    在Microsoft.Tools.TestClient.Variables.CompositeVariable.CreateObject()                    在Microsoft.Tools.TestClient.ServiceExecutor.BuildParameters(Variable []   输入)                    在Microsoft.Tools.TestClient.ServiceExecutor.PopulateInputParameters(String   methodName,Variable []输入,类型contractType,MethodInfo&amp;方法,   的ParameterInfo []&安培;参数,对象[]&amp; parameterArray)                    在Microsoft.Tools.TestClient.ServiceExecutor.Execute(ServiceInvocationInputs   inputValues)                    在Microsoft.Tools.TestClient.ServiceExecutor.Execute(ServiceInvocationInputs   inputValues)                    在Microsoft.Tools.TestClient.ServiceExecutor.ExecuteInClientDomain(ServiceInvocationInputs)   输入)                    在Microsoft.Tools.TestClient.UI.MainForm.invokeServiceWorker_DoWork(Object   发件人,DoWorkEventArgs e)                    在System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)                    在System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object   参数)               的InnerException:

1 个答案:

答案 0 :(得分:1)

WCF服务不能与KeyValuePair

很好地配合使用

这可能是造成错误的原因:

    [DataMember(IsRequired = true)]
    public IEnumerable<KeyValuePair<Enums, string>> Settings { get; set; }

根据建议here创建自己的KeyValuePair类。