简体   繁体   English

使用WCF使用REST服务 - 可选的查询字符串参数?

[英]Consuming REST Service with WCF - Optional Querystring Parameters?

I am having problems trying to consume a simple service using WCF. 我在尝试使用WCF使用简单服务时遇到问题。 Everything has gone well so far except when coming to implement optional query string parameters. 到目前为止,除了实现可选的查询字符串参数之外,一切都进展顺利。 The interface looks a bit like this: 界面看起来有点像这样:

[ServiceContract]
[XmlSerializerFormat]
public interface IApi
{
    [OperationContract]
    [WebGet(UriTemplate = "/url/{param}?top={top}&first={first}")]
    object GetStuff(string param, int top, DateTime first);
}

Then this is consumed by creating a class that inherits ClientBase<IApi> . 然后通过创建继承ClientBase<IApi>的类来使用它。 I tried a few ways to make the parameters optional: 我尝试了几种方法使参数可选:

1) Make the parameters nullable 1)使参数可以为空

This did not work. 这没用。 I get a message from the QueryStringConverter like another question has asked: Can a WCF service contract have a nullable input parameter? 我从QueryStringConverter收到一条消息,就像另一个问题一样: WCF服务合同可以有一个可以为空的输入参数吗?

2) One Parameter at the end of the URL 2)URL末尾的一个参数

So, I thought about changing the UriTemplate to be more generic, building the query string and passing it through as a parameter. 因此,我考虑将UriTemplate更改为更通用,构建查询字符串并将其作为参数传递。 This didn't seem to work either, as the value passed in gets encoded such that it is not recognised as a querystring by the server. 这似乎也不起作用,因为传入的值被编码,使得它不被服务器识别为查询字符串。

Example: 例:

[WebGet(UriTemplate = "/url/{query}")]

3) Hackish Solution 3)Hackish Solution

The only way I found so far of getting this to work is to change all the parameters to strings, and NULL's seem to be allowed here. 到目前为止我找到的唯一方法是将所有参数更改为字符串,并且这里似乎允许使用NULL。

Example: 例:

[WebGet(UriTemplate = "/url/{param}?top={top}&first={first}")]
object GetStuff(string param, string top, string first);

The consumption of this interface still accepts the correct variable type, but ToString is used. 此接口的使用仍接受正确的变量类型,但使用ToString Thes query string parameters still appear in the actual request. Thes查询字符串参数仍出现在实际请求中。

So, is there a way when consuming a REST service using WCF, to make the query string parameters optional? 那么,使用WCF 消费 REST服务有没有办法让查询字符串参数可选?

UPDATE - How it was fixed 更新 - 如何修复

The advice to create a service behaviour was taken. 提出了创建服务行为的建议。 This inherits from WebHttpBehaviour . 这继承自WebHttpBehaviour It looks as follows: 它看起来如下:

public class Api : ClientBase<IApi>
{
    public Api() : base("Binding")
    {
        Endpoint.Behaviors.Add(new NullableWebHttpBehavior());
    }
}

The NullableWebHttpBehavior can be found at the following Stackoverflow question: Can a WCF service contract have a nullable input parameter? NullableWebHttpBehavior可以在以下Stackoverflow问题中找到: WCF服务契约可以有一个可以为空的输入参数吗? . The only problem was, ConvertValueToString wasn't overloaded, so I whipped a quick one up: 唯一的问题是, ConvertValueToString没有超载,所以我快速打了一个:

    public override string ConvertValueToString(object parameter, Type parameterType)
    {
        var underlyingType = Nullable.GetUnderlyingType(parameterType);

        // Handle nullable types
        if (underlyingType != null)
        {
            var asString = parameter.ToString();

            if (string.IsNullOrEmpty(asString))
            {
                return null;
            }

            return base.ConvertValueToString(parameter, underlyingType);
        }

        return base.ConvertValueToString(parameter, parameterType);
    }

This may not be perfect, but it seems to work as intended. 这可能不完美,但似乎按预期工作。

Your option 1) can work for a WCF client because the WebHttpBehavior can be applied to a ClientBase (or ChannelFactory) derived class as shown in this SO question & answer. 您的选项1)可以用于WCF客户端,因为WebHttpBehavior可以应用于ClientBase(或ChannelFactory)派生类,如此SO问题和答案中所示。 Just combine the code you reference in 1) with the configuration shown in the capturing 500 responses question and it show work. 只需将您在1)中引用的代码与捕获500响应问题中显示的配置相结合,即可显示工作。

你尝试过使用可空类型吗?

object GetStuff(string param, int? top, DateTime? first);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM