消费REST服务与WCF - 可选查询字符串参数?可选、字符串、参数、REST

由网友(只想与你走′)分享简介:我有问题想消费使用WCF一个简单的服务。一切都进展顺利,到目前为止来实现可选的查询字符串参数时除外。该界面看起来有点像这样:I am having problems trying to consume a simple service using WCF. Everything has gone well so fa...

我有问题想消费使用WCF一个简单的服务。一切都进展顺利,到目前为止来实现可选的查询字符串参数时除外。该界面看起来有点像这样:

I am having problems trying to consume a simple service using 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);
}

那么这是通过创建一个类继承 ClientBase&LT消耗; IAPI> 。我尝试了一些办法,使得参数可选的:

Then this is consumed by creating a class that inherits ClientBase<IApi>. I tried a few ways to make the parameters optional:

1)使参数为空的

这没有奏效。我从 QueryStringConverter 像另一个问题已经问了一个消息:Can WCF服务合同有一个为空的输入参数?

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?

在网址

于是,我想到了改变UriTemplate更加通用,建立查询字符串,并通过将它作为一个参数。这似乎并没有擦出火花,在传递的值变EN codeD,使其不被识别为一个查询字符串的服务器。

So, I thought about changing the UriTemplate to be more generic, building the query string and passing it through as a parameter. 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.

例如:

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

3)hackish的解决方案

我发现迄今得到这个工作的唯一办法是将所有的参数更改为字符串,空的似乎是允许在这里。

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.

例如:

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

该接口的消费仍然接受正确的变量类型,但的ToString 被使用。帖查询字符串参数仍出现在实际的请求。

The consumption of this interface still accepts the correct variable type, but ToString is used. Thes query string parameters still appear in the actual request.

那么,有没有一种方式,当消耗使用WCF,使查询字符串参数可选REST服务?

So, is there a way when consuming a REST service using WCF, to make the query string parameters optional?

更新 - 如何它是固定

要创建服务行为的建议被采取。这继承了 WebHttpBehaviour 。它看起来如下:

The advice to create a service behaviour was taken. This inherits from WebHttpBehaviour. It looks as follows:

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

NullableWebHttpBehavior 可在以下问题#1中找到:Can WCF服务合同有一个为空的输入参数?。唯一的问题是, ConvertValueToString 不超载,所以我掀起一个快速上升:

The NullableWebHttpBehavior can be found at the following Stackoverflow question: Can a WCF service contract have a nullable input parameter?. The only problem was, ConvertValueToString wasn't overloaded, so I whipped a quick one up:

    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.

推荐答案

您选择1)可以为WCF客户端工作,因为 WebHttpBehavior 可应用于ClientBase(或的ChannelFactory)如本SO问题和放大器;的回答。只需将您在1引用code)与在捕获500响应问题所示的配置,并将其显示工作。

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. Just combine the code you reference in 1) with the configuration shown in the capturing 500 responses question and it show work.

阅读全文

相关推荐

最新文章