具有Complex Type参数的WebInvoke对Json端点不起作用 [英] WebInvoke with Complex Type parameter does not work for Json endpoint

查看:103
本文介绍了具有Complex Type参数的WebInvoke对Json端点不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建WCF Restful服务,该服务将复杂类型"作为Json格式的参数并返回Json.我读了很多文章,并在网上检查了许多样本.一些文章建议在端点行为中添加标签,并如下装饰服务方法,

I want to create WCF Restful service that takes Complex Type as a parameter in Json format and return Json. I read a lots of article and examine many samples on the web. Some article suggest to add tag in Endpoint behavior and decorate Service method as below,

[WebInvoke(UriTemplate = "/PlaceOrder", 
        RequestFormat= WebMessageFormat.Json,   
        ResponseFormat = WebMessageFormat.Json, Method = "POST")]

在这种情况下,WCF返回"Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'."错误消息.

In this case, WCF returns "Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'." error message.

另一种建议的方式(如本文

Another suggested way(as in this article http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx) is add "" tag to Endpoint behavior instead of . But in this case IIS returns ("The remote server returned an error: (400) Bad Request.") error message.

能帮我如何创建以json格式接受复杂类型参数并返回json的Restful Service.

Can you please help me how to create Restful Service that takes complex type argument in json format and return json.

推荐答案

这有效:

[ServiceContract]
public interface IService
{
    [WebInvoke(Method = "POST", UriTemplate = "/ModifyCustomer", RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    Customer ModifyCustomer(Customer customer);
}

public class Service : IService
{
    public Customer ModifyCustomer(Customer customer)
    {
        customer.Age += 1;
        return customer;
    }
}

public class Customer
{
    public string Name { get; set; }
    public int Age { get; set; }
}

自托管方式如下:

var webServiceHost = new WebServiceHost(typeof(Service), 
    new Uri("http://localhost:12345"));
webServiceHost.AddServiceEndpoint(typeof(IService), new WebHttpBinding(),"");
webServiceHost.Open();

在邮递员中:

并且在IIS Express中具有以下配置:

And in IIS Express with the following config:

<system.serviceModel>
    <services>
        <service name="RestServiceTest.Service" behaviorConfiguration="myServiceBehavior">
            <endpoint address="" binding="webHttpBinding" contract="RestServiceTest.IService" behaviorConfiguration="myEndpointBehavior">
            </endpoint>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="myServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="myEndpointBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

邮递员的结果:

这篇关于具有Complex Type参数的WebInvoke对Json端点不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆