user949286
user949286

Reputation: 1279

WCF Web Service inside ASP.NET Application - how to support nullable parameters?

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.

Upvotes: 0

Views: 1107

Answers (1)

VinayC
VinayC

Reputation: 49185

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” %>

Upvotes: 1

Related Questions