WCF 复杂 JSON 输入错误(无法通过 QueryStringConverter 转换)

2024-05-02

我在将复杂 JSON 作为 WCF 服务中的参数工作时遇到问题。

在 Visual Studio 2008 SP1 中使用 Microsoft.Net 3.5 SP1

签订以下合同:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebGet(UriTemplate="GetDataUsingDataContract?composite={composite}", 
        BodyStyle=WebMessageBodyStyle.Wrapped, 
        RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
    string boolValue = "true";
    string stringValue = "Hello ";

    [DataMember]
    public string BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

使用以下 URL:

http://localhost:1122/Service1.svc/GetDataUsingDataContract?composite={"BoolValue":"True","StringValue":"Hello"}

使用端点配置:

<system.serviceModel>
    <services>
        <service name="WebHTTPBindingExample.Service1" behaviorConfiguration="WebHTTPBindingExample.Service1Behavior">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8731/Design_Time_Addresses/WebHTTPBindingExample/Service1/"/>
                </baseAddresses>
            </host>
            <!-- Service Endpoints -->
            <!-- Unless fully qualified, address is relative to base address supplied above -->
            <!--<endpoint address="" binding="wsHttpBinding" contract="WebHTTPBindingExample.IService1">
                --><!-- 
      Upon deployment, the following identity element should be removed or replaced to reflect the 
      identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
      automatically.
  --><!--
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>-->
            <endpoint address="Web" behaviorConfiguration="ChatAspNetAjaxBehavior" binding="webHttpBinding" name="ajaxEndpoint" contract="WebHTTPBindingExample.IService1"/>
            <!-- Metadata Endpoints -->
            <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
            <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WebHTTPBindingExample.Service1Behavior">
                <!-- To avoid disclosing metadata information, 
  set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="True"/>
                <!-- To receive exception details in faults for debugging purposes, 
  set the value below to true.  Set to false before deployment 
  to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="ChatAspNetAjaxBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

我收到以下错误:

Operation 'GetDataUsingDataContract' in contract 'IService1' has a query variable named 'composite' of type 'WebHTTPBindingExample.CompositeType', but type 'WebHTTPBindingExample.CompositeType' is not convertible by 'QueryStringConverter'.  Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

我不相信您可以通过这种方式使用 WCF 在查询字符串上传递复杂类型。看这个回应 http://forums.asp.net/t/1408622.aspx/1?Passing+complex+types+as+a+parameter来自 ASP.NET 论坛中的 Microsoft 技术人员 - 这与您的情况类似。

根据您删除服务方法的方式,似乎更合适的动词是 POST 或 PUT,您可以将您的CompositeType请求正文中的负载。但我猜测你的意图。

我能够通过更改查询字符串类型来使您的代码工作并且仍然使用 GETCompositeType to String然后将 JSON 字符串反序列化为CompositeType通过使用 ASP.NETJavaScriptSerializer班级。 (您可以在这里使用您最喜欢的 JSON 帮助器类——我偏向于JSON.NET http://james.newtonking.com/pages/json-net.aspx,但我也听到FlexJson http://flexjson.sourceforge.net/javadoc/flexjson/JSONSerializer.html也非常好。)

我没有碰你的 web.config (除了让它在我的本地测试应用程序中工作)。我唯一的变化是服务方法签名和服务方法的实现。

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebGet(UriTemplate = "GetDataUsingDataContract?composite={composite}",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    CompositeType GetDataUsingDataContract(String composite);

    // TODO: Add your service operations here
}


public class Service1 : IService1
{
    public CompositeType GetDataUsingDataContract(String composite)
    {
        //use the JavaScriptSerializer to convert the string to a CompositeType instance
        JavaScriptSerializer jscript = new JavaScriptSerializer();
        CompositeType newComp = jscript.Deserialize<CompositeType>(composite);
        newComp.StringValue += " NEW!";
        return newComp;
    }

}

我希望这有帮助。如果您对此还有其他疑问,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WCF 复杂 JSON 输入错误(无法通过 QueryStringConverter 转换) 的相关文章

随机推荐

  • 在 Django 1.9 中使用信号

    在 Django 1 8 中 我能够使用信号执行以下操作 一切顺利 init py from signals import 信号 py receiver pre save sender Comment def process hashtag
  • 如何为特定存储库配置 AWS CodeCommit 配置文件

    我有以下问题 作为我工作的一部分 我处理多个 AWS 账户 每个账户都有一个单独的 AWS CodeCommit 存储库和特定于账户的 IAM 用户 这会导致不同的用户 ID 我想找到一种方法来配置我的 ssh 以根据存储库访问不同的帐户
  • 有没有办法在Python中调用子类定义的方法?

    The init 方法定义了创建类的实例时要执行的操作 创建子类时我可以做类似的事情吗 假设我有抽象类Entity class Entity def onsubclasscreation cls for var in cls annotat
  • 杰克逊:引用同一个对象

    在某些情况下 主体 例如 JSON 主体 中序列化或非序列化的数据包含对同一对象的引用 例如 包含球员列表以及由这些球员组成的球队列表的 JSON 正文 players name Player 1 name Player 2 name Pl
  • 在Android中获取Fragment中的应用程序上下文?

    我已通过在一个活动中使用应用程序上下文将一些数据存储到全局类中 稍后我必须在片段中检索这些值 我已经做了类似的事情来存储在全局类中 AndroidGlobalClass AGC AndroidGlobalClass getApplicati
  • 如何转换温度传感器得到的值?

    我在ST工作Temperature sensor hts221 我用I2C与传感器的命令通信 我从文档中看到类似以下文字 enter code here Temperature data are expressed as TEMP OUT
  • WPF 向我的 GUI 添加时钟

    简单请求 我希望能够在 WPF 应用程序窗口中显示当前时间 有免费的控件吗 只需要显示时间 没有别的 您可以有一个标签或文本块 并将其内容绑定到 System DateTime Now
  • Hibernate 验证器:违规消息语言

    我有一个测试类 我正在测试一个域模型 该模型用例如注释 NotNull 在我的测试课中 我首先得到验证器 private static Validator validator BeforeClass public static void s
  • Ember.js 动态子视图

    我无法让 ember 渲染动态子视图 似乎一旦渲染了子视图 绑定就会丢失 这是一个jsfiddle http jsfiddle net zaius XYzfa http jsfiddle net zaius XYzfa 当您在两个编辑器页面
  • CSS:悬停在多个 div 上时显示样式

    我有 2 个不同尺寸的 div 一个放在另一个上面 所以有一个共同的交叉区域 这两个 div 都有 CSS hover 规则集 如果我将鼠标悬停在每个 div 上 则规则适用 但是 如果我移过交叉区域 则只会激活顶部 div 悬停 当鼠标悬
  • 如何找到给定数组的所有可能的子集?

    我想在 C 或 C 中提取数组的所有可能子集 然后计算所有子集数组各自元素的总和 以检查其中有多少等于给定数字 我正在寻找的是算法 我确实理解这里的逻辑 但我现在还无法实现这一逻辑 考虑一组S of N元素 以及给定的子集 每个元素要么属于
  • 使用 pymongo 查询空字段

    我想使用 python 查询 mongo 中的空字段 但是它很难处理单词 null 或 false 它要么给我错误 它们在 python 中未定义 要么在 mongo 中搜索字符串 null 和 false 这两种情况我都不希望发生 col
  • Cloudflare Worker 缓存 API 出现问题

    我现在花了无数的时间尝试让缓存 API 来缓存一个简单的请求 我让它在中间工作过一次 但忘记向缓存键添加一些内容 现在它不再工作了 不用说 cache put 没有指定请求是否实际被缓存的返回值并不完全有帮助 我只能进行反复试验 有人可以给
  • 如何解决警告“没有明确的所有权”

    我有一种方法 它采用间接指针作为参数 然后 如果出现错误 将其设置为错误对象 我正在尝试打开尽可能多的警告 但其中之一 Implicit ownership types on out parameters 在此方法中生成警告 id doWi
  • asp.net 3.5 是否支持单个页面上的多个表单?

    我只是想确认这是否属实 我记得在某处读到现在这是可能的 但经过一个小时的谷歌搜索后我找不到任何明确的证据 在 ASP NET WebForms 中 您不能使用多个表单 因为它使用单个表单模型 在 ASP NET MVC 中您可以
  • 如何将2个匹配查询加入到elasticsearch的查询中?

    我想查询以下数据user id is 1 and name is John 写一个常用的SQL很容易 select from t where user id 1 and name John 但对我来说进行elasticsearch的查询并不
  • 从 Azure 流分析将数据插入 Azure SQL 数据库表时出现“OutputDataConversionError.TypeConversionError”

    我正在尝试学习 Azure IoT 我正在尝试将 MQTT 消息发送到 IoT 中心 在 IoT 中心 我使用流分析将数据输出到 SQL 数据库中 但目前在流分析输出中 我遇到此错误 9 12 30 AM 源 OUTPUTSQL 在处理时间
  • 如何设置按钮的大小?

    我将按钮放在带有 GridLayout 的 JPane 中 然后我用 BoxLayout Y AXIS 将 JPanel 放入另一个 JPanel 中 我希望 GridLayout 中的按钮是方形的 我使用 tmp setSize 30 3
  • 将列表视图项转换为单个位图图像

    参考这个主题 Android 获取所有 ListView 项目的屏幕截图 https stackoverflow com questions 12742343 android get screenshot of all listview i
  • WCF 复杂 JSON 输入错误(无法通过 QueryStringConverter 转换)

    我在将复杂 JSON 作为 WCF 服务中的参数工作时遇到问题 在 Visual Studio 2008 SP1 中使用 Microsoft Net 3 5 SP1 签订以下合同 ServiceContract public interfa