【问题标题】:Optional query string parameter passing to WCF service传递给 WCF 服务的可选查询字符串参数
【发布时间】:2012-08-28 02:11:05
【问题描述】:

我想知道如何使用:

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 服务中使参数可选。

【问题讨论】:

  • 使用 WCF 时,我从来不用WebOperationContext 来获取查询参数。你确定你做对了吗?
  • 这就是我要问的,我在一些链接中读到我可以在 wcf 方法中放置一个可选参数。 stackoverflow.com/questions/4969687/… 在此链接中,但我不知道我是否在做 ri8
  • @ThorstenDittmar 你知道如何在 WCF 服务中使用可选参数
  • @abatishchev 兄弟我想在 WCF 服务中使用它。
  • @AbhishekMathur:可能重复 - stackoverflow.com/questions/2649680/…

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


【解决方案1】:

我理解它很老的帖子,但可能对其他人有所帮助。

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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      【解决方案3】:

      最近有一个类似的问题,并通过覆盖默认的 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 列表、数组等支持相当复杂的场景。 一切都将是强类型的,并且可以从单点进行转换管理 - 无需处理服务/方法级别的解析噩梦。

      【讨论】:

      【解决方案4】:

      所以实际上您是在使用 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"];
      

      但是:我没有尝试过,但这就是我从您在 cmets 中引用的内容中了解到的。

      【讨论】:

      • 是的,这正是我想要的,但它不起作用,因为无论我是否给它一个值,在这个限制始终为空之后我不知道该怎么做
      • 你的方法不适合我。我已经在以下答案之一中发布了适合我的方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2011-02-27
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多