I am attempting to write a WCF based web service that utilizes the Async and Await operators. So far, I almost have it running; however, when I call the method in debug mode the web service is receiving the parameter as null.
I know very little about the WCF (this is my first project) and was wandering if someone with a bit more experience can help shed some light as to what I may be doing wrong.
Here is my interface definition:
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Threading.Tasks;
namespace MappingService.Services.Interfaces
{
[DataContract]
public class Request
{
/// <summary>
/// ID of the record to process.
/// For a Policy based request this will be the Policy ID.
/// For a Frame based request this will be the Mapping Frame ID.
/// </summary>
[DataMember]
public long ID { get; private set; }
/// <summary>
/// Width for the image.
/// </summary>
[DataMember]
public int Width { get; private set; }
/// <summary>
/// Height for the image.
/// </summary>
[DataMember]
public int Height { get; private set; }
/// <summary>
/// Quality level for the image. 3 indicates high-quality PNG and 2 or lower indicates 90 quality compressed JPEG.
/// </summary>
[DataMember]
public int Quality { get; private set; }
/// <summary>
/// Determines whether to fetch from the IP tables or not.
/// </summary>
[DataMember]
public bool FromIP { get; private set; }
public Request(long id, int width, int height, int quality, bool fromIP)
{
ID = id;
Width = width;
Height = height;
Quality = quality;
FromIP = fromIP;
}
}
[ServiceContract]
public interface IMappingProcessor
{
///// <summary>
///// Rebuilds the mapping frame imagery
///// </summary>
///// <param name="request">The request object to process.</param>
//[OperationContract]
//[WebInvoke(Method = "POST", UriTemplate = "LoadFramesForPolicy", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
//void LoadFramesForPolicy(Request request);
/// <summary>
/// Rebuilds the mapping frame imagery
/// </summary>
/// <param name="request">The request object to process.</param>
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/LoadFrameImage", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
Task LoadFrameImage(Request request);
}
}
Note, I was originally going to go with multiple parameters, but passing multiple parameters via the POST was becoming a pain, so I decided to wrap them in a request object defined as a DataContract with its properties exposed as DataMembers.
Now, my implementation of that is as follows:
public Task LoadFrameImage(Request request)
{
try
{
//id = 61265;
//width = 744;
//height = 522;
//fromIP = false;
if (request == null)
throw new InvalidOperationException("Invalid parameter. Request cannot be null.");
// Ommitted excess code... i.e. thread process and whatnot
// Do work
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return Task.Factory.StartNew(() => { });
}
I omitted the bulk of the work to keep it simple. I am, however, not making it pass the check for the parameter being null.
Finally, I've tried many ways of calling the method via a service ranging from the ChannelFactory after adding the WCF service as a service reference to a regular web client, but have had no luck with any of the methods I have tried. The channel factory keeps barking on the client saying there is no endpoint configured to listen to the request and any other method I've used either doesn't format the request and parameters correctly to make the call in the first place, or it makes the request but the parameter is null. Here is my current iteration of how I am calling the method:
var request = new Request(frame.Frame, 744, 522, 2, false);
var client = Client();
var response = await client.PostAsJsonAsync<Request>(new Uri($"{EXTERNAL_ADDRESS}{UPDATE_ADDRESS}"), request);
Alas, I hit the correct breakpoint in the web method but the parameter is always null. Can anyone help shed some light on what I may be doing wrong?
Thanks
Edit: It appears that specifying the BodyStyle = WebMessageBodyStyle.Bare) resolves the issue the with parameter being null. Now I am getting an error "a route named 'CustomerGateway' is already in the route collection. Route names must be unique" that I need to figure out.
Aucun commentaire:
Enregistrer un commentaire