JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了WCF复杂JSON INPUT错误(不能由QueryStringConverter转换)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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; }
    }
}@H_801_7@ 
 

使用以下URL:

http://localhost:1122/service1.svc/GetDataUsingDataContract?composite={"BoolValue":"True","stringvalue":"Hello"}@H_801_7@ 
 

使用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>@H_801_7@ 
 

我得到以下错误:

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'.@H_801_7@

解决方法

我不相信你可以使用这种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;
    }

}@H_801_7@ 
 

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

大佬总结

以上是大佬教程为你收集整理的WCF复杂JSON INPUT错误(不能由QueryStringConverter转换)全部内容,希望文章能够帮你解决WCF复杂JSON INPUT错误(不能由QueryStringConverter转换)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。