传递给WCF服务的可选查询字符串参数

时间:2012-09-03 10:35:09

标签: c# .net wcf query-string optional-parameters

我想知道如何使用:

string limit = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["Limit"];

在我的wcf中使用此方法:

CityNewsList GetNewsByCity(string DeviceType,string id,string limit);

这里'devicetype'和'id'是默认参数,我想要的是'limit'作为可选参数意味着用户可以选择传递他可以传递或不能传递的参数。

想要使用限制为:

if (limit == some value)
{
    //do this.
}
if (limit == null)
{
    // do this.
}

我经历了许多链接,但我没有得到如何在我的wcf中使用它。

或者如果有人可以告诉我如何在WCF服务中使参数可选。

4 个答案:

答案 0 :(得分:1)

实际上,您正在使用WCF创建REST服务。我已经在您创建可能重复的问题的答案中阅读了您的意思:How to have optional parameters in WCF REST service?

您可以通过在WebGet或WebInvoke属性上忽略UriTemplate中的查询字符串,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters 来获得所需的效果。

那么归结为:

更改方法的签名以省略参数:

CityNewsList GetNewsByCity(string DeviceType,string id /*,string limit*/);

更改属性,以便在查询字符串上不需要参数:

[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}", RequestFormat = WebMessageFormat.Xml)]

而不是

[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}/{limit}", RequestFormat = WebMessageFormat.Xml)]

最后你会得到类似的东西:

[OperationContract]
[WebGet(UriTemplate = "/whatever/{DeviceType}/{id}", RequestFormat = WebMessageFormat.Xml)]
CityNewsList GetNewsByCity(string DeviceType,string id);

实施的首要任务是:

string limit = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["Limit"];

但是:我没有尝试过,但这就是我从您对问题的评论中所引用的内容中所理解的。

答案 1 :(得分:1)

最近有一个类似的问题,并通过覆盖默认的QueryStringConverter来解决它。

1)Subclass System.ServiceModel.Dispatcher.QueryStringConverter并覆盖其ConvertStringToValue方法。

示例,它使所有枚举可选(如果没有值,将使用默认值)

  public override object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType.IsEnum)
            {
                if (string.IsNullOrEmpty(parameter))
                {
                    return Activator.CreateInstance(parameterType);
                }
            }

            return base.ConvertStringToValue(parameter, parameterType);

        }

2)Subclass System.ServiceModel.Description.WebHttpBehavior并覆盖其GetQueryStringConverter方法以返回修改后的查询字符串转换器

public class ExtendedQueryStringBehavior : WebHttpBehavior
{
    protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
    {
        return new ExtendedQueryStringConverter();
    }
}

3)将新的WebHttpBehavior连接到所需的端点(可能需要将此功能与您可能拥有的其他内容合并)。

可以使用QS转换器,复杂类型,csv列表,数组等支持相当复杂的场景。 从单点开始,所有内容都将是强类型和转换可管理的 - 无需在服务/方法级别处理解析噩梦。

答案 2 :(得分:0)

我解决可选/默认问题的方法是

Interface.cs :(将可选参数设置为默认值{x = y})

[OperationContract]
[WebGet(UriTemplate = "draw/{_objectName}/{_howMany}/{_how=AnyWay}"
, ResponseFormat = WebMessageFormat.Json
)]
YourShape[] doDraw(string _objectName, string _howMany, string _how);

method.svc.cs :(测试x ==默认?a:x)

YourShape[] doDraw(string _objectName, string _howMany, string _how) {
    if (_how == "AnyWay")
       _how = findTheRightWay();  
    return (drawShapes(_objectName, _howMany, _how));
}

您的终端可以是

yoursite/draw/circle/5

yoursite/draw/circle/5/ThisWay

答案 3 :(得分:0)

我对这则帖子很了解,但可能会对其他人有所帮助。

在Interface中:deviceType在下面不是可选的,但是在调用端点时,ID和limit是可选的。

        [OperationContract]
        [WebGet(UriTemplate = "newsbycity/{DeviceType}/{id=1}/{limit=10}", ResponseFormat = WebMessageFormat.Json)]
        CityNewsList GetNewsByCity(string DeviceType,string id,string limit);

服务:

CityNewsList GetNewsByCity(string DeviceType,string strid,string strlimit)
{
int ID = id; // perform null check 
}
相关问题