【问题标题】:Serialization/Deserialization of Parameters - WCF Service Call doesn't work参数的序列化/反序列化 - WCF 服务调用不起作用
【发布时间】:2010-09-07 21:31:14
【问题描述】:

我有一个简单的 WCF 服务方法:

[OperationContract]
public SetupGameResult SetupGame(long player1Id, long player2Id, long myPlayerId)
    {
        if(player1Id == 0 || player2Id == 0 || myPlayerId == 0)
        {
            throw new ArgumentException();
        }

        ... // other code
    }

我只在 silverlight 应用程序中的一个地方调用此服务。这里:

        if(player1Id == 0 || player2Id == 0 || myPlayerId == 0)
        {
            throw new ArgumentException();
        }

        // Setup Game
        GameServiceClient gameService = new GameServiceClient();
        gameService.SetupGameCompleted += new EventHandler<SetupGameCompletedEventArgs>(gameService_SetupGameCompleted);
        gameService.SetupGameAsync(player1Id, player2Id, myPlayerId);

通常情况下,SetupGame永远不会在 myPlayerId = 0 时调用。但要确保我在调用服务方法之前进行检查。

问题是服务方法被正确调用一次,在第二次调用时它抛出参数异常,因为 myPlayerId = 0。

这很奇怪,因为我在调用之前检查它是否为 0。

会是什么问题?

编辑:
看来序列化/反序列化确实有问题。

但那会有什么原因

编辑 2:
我在构建过程中收到以下警告。会不会是这个问题?

Warning 12  Client proxy generation for service 'Car_Motion.Web.Services.GameService' failed: Generating metadata files...
Warning: Unable to load a service with configName 'Car_Motion.Web.Services.GameService'. To export a service provide both the assembly containing the service type and an executable with configuration for this service.
Details:Either none of the assemblies passed were executables with configuration files or none of the configuration files contained services with the config name 'Car_Motion.Web.Services.GameService'.
Warning: No metadata files were generated. No service contracts were exported.
To export a service, use the /serviceName option. To export data contracts, specify the /dataContractOnly option. This can sometimes occur in certain security contexts, such as when the assembly is loaded over a UNC network file share. If this is the case, try copying the assembly into a trusted environment and running it.

提前致谢

【问题讨论】:

  • 不正确的序列化/反序列化导致默认值。
  • 好像。所有参数都是0。这真的很奇怪。
  • 在哪里设置 mPlayerId?在您的支票和服务电话之间是否可以将其设置为零?
  • 没有 myPlayerId 是调用服务的函数的参数。所以它不能从这个函数的范围之外改变,我在调用服务之前立即检查。真奇怪

标签: .net silverlight wcf


【解决方案1】:

Ben,我认为您的服务引用存在一些问题,并且代理类未正确生成,因此长值可能会获得默认值 0。 请检查以下项目一次

  1. 确保您的 WCF 服务可以编译
  2. 尝试删除和添加服务引用一次
  3. 尝试从 VS 命令行手动运行 svcutil.exe。 svcutil 有一个已知错误,当​​您使用具有 的配置文件对服务程序集运行它时,它将以您看到的方式失败。解决方法是在运行 svcutil 之前注释掉该部分。

【讨论】:

  • 您好,我的浏览器缓存有问题。浏览器没有下载最新的 Silverlight Control。因为我喜欢你的回答,所以我把它作为被接受的答案;-)
  • 答案中缺少的是配置文件中具有已知错误的项目。有问题的部分是&lt;baseAddresses&gt;
猜你喜欢
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2020-10-16
  • 2012-09-18
  • 1970-01-01
  • 2012-07-11
  • 2016-11-16
  • 2017-02-11
相关资源
最近更新 更多