Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:13:38+00:00 2026-05-27T19:13:38+00:00

I’m trying to allow flexible method parameters inside of my WCF service, but I’ve

  • 0

I’m trying to allow flexible method parameters inside of my WCF service, but I’ve run into a few issues.

First, I’ll just explain the goal. I want to be able to declare a web method, such as:

    [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
    public StatisticEventArgs GetStatistic(DateTime? startDate = null, DateTime? endDate = null)
    {
        return new StatisticEventArgs() { Count = 50; }
    }

and be able to call it through the jquery $.ajax method:

$.ajax({
        type: 'GET',
        url: 'service/MyService.svc,
        data: '', // or '?startDate=12/1/2011&endDate=12/10/2011','?startDate=12/1/2011', etc. Any combination of startDate or endDate parameters
        contentType: "application/json",
        dataType: "json",
        processdata: true,
        success: endCallback,
        error: errorCallback 
    });

The goal is to create methods where all parameters are optional, and for whichever parameters are not specified, their default value will be used instead. In this example, I want to be able to call the method where I can provide either the startDate
or endDate parameter, both, or neither.

I quickly found that the default QueryStringParameter class (as seen in this question: In the WCF web programming model, how can one write an operation contract with an array of query string parameters (i.e. with the same name)?) doesn’t support nullable types, but JsonQueryStringConverter does. Also, as noted in the above question, there is a bug in .NET 4.0 and below related to this solution that prevents the JsonQueryStringConverter class from being instantiated in a custom behavior (GetQueryStringConverter is never called).

After more research, I found this Microsoft bug that provides a workaround, but only if instantiating the service through code.

My question is, how can I apply that workaround in an ASP.NET WCF Web service? I believe I need to set it up in the web.config, but I’m not sure how to accomplish that. Here is the relevant example code for what I have so far (I’ve modified names, so it may contain some typos):

WCF class contained inside of my ASP.NET Web Application project:

namespace Namespace.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService
    {

    [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
    public StatisticEventArgs GetStatistic(DateTime? startDate = null, DateTime? endDate = null)
    {
        return new StatisticEventArgs() { Count = 50; }
    }

    public class NullableTypeBehaviorBehaviorExtension :  BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(NullableTypeBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new NullableTypeBehavior();
        }
    }

    public class NullableTypeBehavior : WebHttpBehavior
    {

        protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
        {
            return new JsonQueryStringConverter();
        }
    }
}

Javascript:

$.ajax({
        type: 'GET',
        url: 'service/MyService.svc/GetStatistic',
        data: '', // or '?startDate=12/1/2011&endDate=12/10/2011','?startDate=12/1/2011', etc. Any combination of startDate or endDate parameters
        contentType: "application/json",
        dataType: "json",
        processdata: true,
        success: endCallback,
        error: errorCallback 
    });

web.config:

<system.serviceModel>
<extensions>
  <behaviorExtensions>
    <add name="nullableTypeBehavior" type="Namespace.Services.NullableTypeBehaviorBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </behaviorExtensions>
</extensions>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
  <service name="MyWcfService">
    <endpoint address="" behaviorConfiguration="MyBehaviorConfig"
      binding="webHttpBinding"  contract="Namespace.Services.MyService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="web">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="MyBehaviorConfig">
      <nullableTypeBehavior />
    </behavior>
  </endpointBehaviors>
</behaviors>
</client>

UPDATE 1: SOLUTION

Here’s the answer I ended up with (Courtesy of VinayC):

public class CustomFactory : System.ServiceModel.Activation.ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type t, Uri[] baseAddresses)
   {
       return new CustomHost(t, baseAddresses);
   }
}

public class CustomHost : ServiceHost
{
    public CustomHost(Type t, params Uri[] baseAddresses) : base(t, baseAddresses) { }

   protected override void OnOpening()
   {
       var nullableTypeBehavior = new NullableTypeBehavior();
      foreach(var ep in this.Description.Endpoints)
      {
         ep.Behaviors.Add(nullableTypeBehavior);
      }
      base.OnOpening();
   }
}

and at the top of my MyService.svc file:

<%@ ServiceHost Language="C#" Debug="true" Service="Namespace.Services.MyService" CodeBehind="MyService.svc.cs" Factory="Namespace.Services.CustomFactory" %>

UPDATE 2: Issue with passing dates for this solution

Currently passing dates does not work:

http://localhost/Services/MyService.svc?startDate=11/10/2011&endDate=11/11/2011

but these requests work:

http://localhost/Services/MyService.svc?startDate=null&endDate=null
http://localhost/Services/MyService.svc

Seems like that’s JsonQueryStringConverter specific, so I’m going to look into it and post back when I find a solution. Seems like it should be simple enough to debug.

UPDATE 3: Solution for passing dates:

The problem is that the dates I was passing are not in the expected format for .NET JSON. I was passing something like:

"12/01/2011"

and .NET JSON (including JsonQueryStringConverter) expects this format:

"\/Date(1283219926108)\/"

That date format is not native to javascript. In order to retrieve a date in this format, I found this post. After adding this script, I can do this in jQuery, and the date will be parsed appropriately:

 $.ajax({
        type: 'GET',
        url: 'service/MyService.svc/GetStatistic',
        data: '?startDate=' + JSON.stringifyWCF(new Date('12/1/2011'))
        contentType: "application/json",
        dataType: "json",
        processdata: true,
        success: endCallback,
        error: errorCallback 
    });

Everything seems to work as expected. I can ignore any optional parameters, set them to null, or give them a value and all requests work.

Updated the Microsoft bug with the workaround. The workaround is a bit more elegant than the solution provided here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:13:39+00:00Added an answer on May 27, 2026 at 7:13 pm

    I am not certain if you need nullable types. If you want to pass multiple date/time values – why not use signature such as

    public StatisticEventArgs GetStatistic(DateTime[] startDates = null, DateTime[] endDates = null)
    

    In your CustomQueryStringConverter, you need to check if the parameter is null/empty string then your array will be null. Start dates and end dates will be correlated by index. Only possible combination that you may not able to pass would be having one pair where start-date is missing and another pair where end date is missing. As a work-around, you can always use some epoch start date.

    EDIT:
    If you are trying to apply the work-around from MS Connect in your WCF service then you need to create custom service host factory. For example,

    public class CustomFactory : ServiceHostFactory
    {
       public override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
       {
          return new CustomHost( t, baseAddresses )
       }
    }
    
    public class CustomHost : ServiceHost
    {
       public DerivedHost( Type t, params Uri baseAddresses ) : base( t, baseAddresses ) {}
    
       public override void OnOpening()
       {
          var nullableTypeBehavior = new NullableTypeBehavior();
          foreach(var ep in this.Description.EndPoints)
          {
             ep.Behaviors.Add(nullableTypeBehavior);
          }
       }
    }
    

    And finally use @ServiceHost directive in svc file to plug-in the factory – for example:

    <% @ServiceHost Factory=”CustomFactory” Service=”MyService” %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.