【问题标题】:WebInvoke with Complex Type parameter does not work for Json endpoint具有复杂类型参数的 WebInvoke 不适用于 Json 端点
【发布时间】:2014-06-26 23:39:32
【问题描述】:

我想创建 WCF Restful 服务,它将复杂类型作为 Json 格式的参数并返回 Json。我阅读了很多文章并检查了网络上的许多示例。一些文章建议在 Endpoint 行为中添加标签并装饰 Service 方法,如下所示,

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

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

另一种建议的方法(如本文中的http://dotnetmentors.com/wcf/wcf-rest-service-to-get-or-post-json-data-and-retrieve-json-data-with-datacontract.aspx)是将“”标签添加到端点行为而不是 .但是在这种情况下 IIS 返回 ("The remote server returned an error: (400) Bad Request.") 错误消息。

您能否帮我创建一个以 json 格式接受复杂类型参数并返回 json 的 Restful 服务。

【问题讨论】:

    标签: c# json web-services wcf rest


    【解决方案1】:

    这行得通:

    [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 中使用以下配置:

    <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>
    

    邮递员的结果:

    【讨论】:

    • 自助托管在我这边不起作用,我收到 "System.ServiceModel.AddressAccessDeniedException" 。你能确认这是在 IIS(或 express)下工作吗?
    • 感谢 Jon_Lindeheim,您使用的是哪个版本的 VS 和 .net Framework。
    • 谢谢乔恩,我接受了你的回答,即使它在这里仍然不起作用。我认为问题出在我的环境/设置等。非常感谢。
    • AddressAccessDeniedException 是另一个问题。 stackoverflow.com/q/22686469/586754 基本上,您正在监听 *(无论出于何种原因,这似乎是 WCF 默认的)而不是仅 localhost,这需要管理员权限 - 或通过 netsh 为您的端口/url 提供您的用户权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2015-04-24
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多