Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WcfTestClient-序列不包含元素[当值为null时]_C#_.net_Asp.net Mvc_Wcf_Wcftestclient - Fatal编程技术网

C# WcfTestClient-序列不包含元素[当值为null时]

C# WcfTestClient-序列不包含元素[当值为null时],c#,.net,asp.net-mvc,wcf,wcftestclient,C#,.net,Asp.net Mvc,Wcf,Wcftestclient,我已经编写了一个WCF服务来处理基于web游戏的主要业务逻辑,在使用WcfTestClient进行测试时遇到了一个问题。从我在WCF上公开的一个方法的签名可以看出,它有几个可选参数 public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null); 因此,动作类型以及执行动作的播放器的用户id(uid)都是一

我已经编写了一个WCF服务来处理基于web游戏的主要业务逻辑,在使用WcfTestClient进行测试时遇到了一个问题。从我在WCF上公开的一个方法的签名可以看出,它有几个可选参数

public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null);
因此,动作类型以及执行动作的播放器的用户id(uid)都是一项明确的要求,之后参数变得可选,因为某些动作可能需要也可能不需要另一个播放器、整数或通用字符串值(即,攻击另一个玩家需要另一个玩家对象,该对象使用角色名称[pparam]进行查找。)以下是DoAction方法的整个实现:

 public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null) 
    {
        Player playerparam;
        Player player;
        // Verify players exist
        if (!PlayerExists(uid))
        {
            return new PlayerActionResult(false, ResultType.NotFound);
        }
        else { player = GetPlayer(uid); }

        if (pparam != null & !PlayerExists(pparam))
        {
            return new PlayerActionResult(false, ResultType.NotFound);
        }
        else { playerparam = GetPlayer(pparam); }

        // Construct action and pass parameters
        return player.DoAction(new PlayerAction(type, playerparam, amount, svalue));
    }
考虑到该方法,我决定运行一些测试,以确保该方法在大多数情况下都能正常工作。在我尝试通过WcfTestClient将(null)传递到该方法之前,一切都正常工作,此时我收到以下错误:

序列不包含任何元素

服务器堆栈跟踪: System.ServiceModel.Channels.ServiceChannel.ThrowiffaultUnderstanding(消息回复,>MessageFault故障,字符串操作,MessageVersion版本,FaultConverter故障转换器) 位于System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime>operation,ProxyRpc&rpc) 在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,>ProxyOperationRuntime操作,对象[]输入,对象[]输出,时间跨度超时) 位于System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage>methodCall,ProxyOperationRuntime操作) 位于System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)

在[0]处重试异常:

在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,>IMessage retMsg)中

at System.Runtime.Remoting.proxy.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)

在IConquestService.DoAction(PlayerActionType、Int32 uid、字符串pparam、Int32 amount、字符串svalue)

在ConquestServiceClient.DoAction(PlayerActionType,Int32 uid,String pparam,Int32 amount,String svalue)

我已经在这个困境中苦苦挣扎了一段时间,并多次搜索“序列不包含任何元素”关键字对此特定问题无效。但是请记住,当我从我的MVC应用程序调用此方法时,一切都非常顺利,调用和处理操作逻辑时没有问题。似乎只有在我尝试并通过(null)时才会调用和处理此方法作为WcfTestClient中的pparam。如果有人偶然发现这一点并能启发我,我将不胜感激

更新:

谢谢你的建议!在我发布这篇文章的15分钟内,我对它进行了一些修改,最终解决了问题。我保留了可选参数,并对该方法进行了一些修改,先实例化PlayerAction对象,然后直接在meth中设置该对象的playerparam属性如果存在pparam字符串,则为od。下面是它现在的样子:

   public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam = null, int amount = 0, string svalue = null) 
    {
        Player player;
        PlayerAction action = new PlayerAction();
        action.type = type;
        // Verify executor exists
        if (!PlayerExists(uid))
        {
            return new PlayerActionResult(false, ResultType.NotFound);
        }
        else { player = GetPlayer(uid); }

        if (pparam != null & !PlayerExists(pparam))
        {
            if (!PlayerExists(pparam))
            {
                return new PlayerActionResult(false, ResultType.NotFound);
            }
            action.playerparam = GetPlayer(pparam);
        }

        // Construct action and pass parameters
        return player.DoAction(new PlayerAction(type, amount, svalue));
    }

您是否尝试过将这些实现为重载而不是可选参数

例如:

public PlayerActionResult DoAction(PlayerActionType type, int uid);
public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam);
public PlayerActionResult DoAction(PlayerActionType type, int uid, string pparam, int  amount);
//etc...

Windows通信基础可能不理解被指派的默认参数,而只是将操作契约作为WSDL中的两个参数发送。 这可以解释为什么它在MVC中工作,因为WCF需要SOAP信封中传输的信息来正确决定在服务器上执行哪个方法,而MVC使用被访问的路由