我在WCF服务中作为参数使用复杂 JSON作为参数.

在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"}

使用Enpoint配置:

<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在查询字符串上传递复杂的类型.请参阅ASP.NET论坛中的Microsoft技术的 this response – 它与您的情况相似.

根据你如何扼杀你的服务方法,看起来像一个更合适的动词是POST或PUT,你可以把你的CompositeType有效载荷放在请求体内.但我猜你的意图.

我可以通过将查询字符串类型从CompositeType更改为String,然后使用ASP.NET JavaScriptSerializer类将JSON字符串反序列化为CompositeType,从而使您的代码工作并仍然使用GET. (你可以在这里使用你最喜欢的JSON助手类 – 我偏偏到JSON.NET,但是我也听到FlexJson也是非常好的)

我没有触摸你的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;
    }

}

我希望这有帮助.如果您有其他问题,请告诉我们.

WCF复杂JSON INPUT错误(不能由QueryStringConverter转换)的更多相关文章

  1. 从Android应用程序中消耗WCF Web服务?

    我想从Android应用程序中使用WCFWeb服务.我曾经使用过.asmxweb服务,但我不知道如何在Android应用程序中使用SCFWeb服务.我用谷歌搜索它但没有找到任何东西.如果有人做过,请帮助我.提前致谢.解决方法Hereisanarticleexplaininghowtoconsumewebservicewithandroidingeneral当涉及到WCF并且可以与您想要小心的jav

  2. 具有证书的WCF客户端身份验证

    同一服务正在使用.pfx证书进行身份验证。客户端不愿意共享.pfx文件,并坚持要求我实现一种使用.cer证书的方法。我已尝试安装证书并将其导出为.pfx格式,但由于.cer文件没有私钥,因此禁用了以.pfx导出的选项。

  3. wcf – ClientCredentialType = Windows和= Ntlm之间的差异

    FeedbackID=354236这种情况听起来类似于clientCredentialType=Windows在域帐户下运行时失败,并且在本地帐户下运行时工作).问题是建议的修复程序需要更改WCF客户端配置文件–但在我的情况下,我使用SOAP1.1与非WCF客户端.clientCredentialType=Windows使用内置的Windows身份验证,可以通过ActiveDirectory和NTLM.显然,NTLM类型将仅使用NTLM进行身份验证.我相信你已经看过了,但这里是WCF安全性的链接:http

  4. 如何在1个Windows服务中托管2个WCF服务?

    我有一个WCF应用程序,它有两个服务,我试图使用net.tcp在一个Windows服务中托管.我可以运行任何一种服务,但只要我尝试将它们都放在Windows服务中,只有第一个服务加载.我已经确定第二个服务ctor正在被调用,但OnStart永远不会被激活.这告诉我WCF发现加载第二个服务有问题.使用net.tcp我知道我需要打开端口共享并启动服务器上的端口共享服务.这一切似乎都运作正常.我已经尝试

  5. windows-services – Msmq和WCF服务

    我是否遗漏了为队列和Windows服务设置的任何权限,如果是这样,你能否建议在哪里添加这些权限?汤姆·霍兰德有一个关于使用WCF的MSMQ的三部分博客系列–非常值得一试!

  6. 在带有WCF的App.config中使用Windows角色身份验证

    谢谢如果我理解得很好,你想在运行时选择角色.这可以通过WCF操作中的permission需求来完成.例如.

  7. wcf – 什么是任务管理器中的CPU时间?

    我有一些WCF服务托管在Windows服务中.昨天我看了任务管理器,注意到我的Windows服务进程的cpu时间是5个小时以上,而大多数其他进程都是0.这意味着什么?

  8. .net – 在IIS 7.5中托管WCF与Windows服务的性能注意事项

    我有一个简单直接的问题:是否有任何性能优势,我不知道什么时候比较主机的IIS7.5中的WCF服务与Windows服务?

  9. wcf – 为我配置“使用端点配置服务”演示失败(Microsoft / endpoint.tv / Pluralsight)

    HTTP无法注册URLhttp://:8732/EvalService/….:P我试过评论新的终点无济于事.即使是原始的也会因此错误而失败.如果我把它们放回原来的端口[不是那么]1337也没关系.这是App.config,并没有什么突出的问题:有什么问题/我该如何解决?

  10. 需要一步一步的WCF作为Windows服务

    我正在尝试找到一个(好的)分步创建WCF并将其作为Windows服务托管的示例.我正在使用VS2010并且有一个带有1个函数的简单WCF.请不要谷歌和发布;我正在寻找某人实际使用过的资源.我所做的大部分Google搜索都没有达到我想要做的事情.我只想获取我的WCF库,并找到一种方法将其安装为WindowService.我在2008年完成了,但2010年是……

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部