2

我想知道如何使用:

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

4 回答 4

2

所以实际上你是在使用 WCF 来创建一个 REST 服务。我已经阅读了您在回答您正在创建可能重复的问题时的意思:如何在 WCF REST 服务中具有可选参数?

您可以通过在 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"];

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

于 2012-09-03T12:26:12.157 回答
1

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

1) 子类 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) 子类 System.ServiceModel.Description.WebHttpBehavior 并覆盖其 GetQueryStringConverter 方法以返回您修改后的查询字符串转换器

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

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

可以使用 QS 转换器、复杂类型、csv 列表、数组等支持相当复杂的场景。一切都将是强类型的,并且可以从单点进行转换 - 无需处理服务/方法级别的解析噩梦。

于 2012-10-25T13:21:19.163 回答
1

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

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==default?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
于 2016-08-23T20:32:39.637 回答
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 
}
于 2020-07-14T05:56:09.950 回答