0

I have a json string which needs to be accepted by WCF Post method.I have used class to accept the request and is working fine. But I want to accept the request dynamically without using class. Can I use string, Jobject or any other data type to accept the same?

enter image description here

my post method. I am getting null value when using stream and string

[OperationContract(Name = "PostResponse")]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "PostJsonResponse?Plugin={plugin}&Action={action}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]      
    Stream PostJsonResponse(String plugin, String action, Stream jsonStr);

Json String. We want to bind below json data to jsonStr (Parameter) in Post method

{"short_description":"BF INC 008489","description":"","u_issue":"Paper Jam","business_service":"Printer and Copier Devices","impact":"3","urgency":"3"}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • your question is unclear, anyway if you wanted to pass a single string then use string yourVariableName as parameter in your method, but from the image u have uploaded it seems like you need a Class type. There is nothing wrong with the current approach – Arunprasanth K V Sep 03 '19 at 06:54

2 Answers2

1

You can do this with a stream or a string

public bool PostString(string json)
{
    Model model = JsonConvert.DeserializeObject<Model>(json);
    return true;
}

public bool PostStream(Stream value)
{
    // Read the stream into a string
    string json;

    using(var streamReader = new StreamReader(stream))
    {
        json = streamReader.ReadToEnd();
    }     

    // Deserialise string to object       
    Model model = JsonConvert.DeserializeObject<Model>(json);

    return true;
}

With that said, WCF are really used for strongly typed objects... and usually whenever I see code that uses a stream as the parameter when it could be strongly typed, its generally a telltale sign the developer couldn't figure out how to make WCF work.

timkly
  • 793
  • 6
  • 14
  • If you have a StreamReader already you can also opt to use the JsonSerializer and desarialize from the reader, that prevents allocating a string that can potentially be a huge allocation: https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm – rene Sep 03 '19 at 07:42
  • I hv modified the question. please see that. I am getting null and 500 error while using Stream and String respectively – Mathews Davis Sep 03 '19 at 08:14
  • You can enabled tracing to check out the error or check out the event viewer to see if it has anything in it - https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing – timkly Sep 05 '19 at 07:19
0

It seems that you want to upload a file stream and also want to submit other text content. But in fact, this interface does not conform to the definition of WCF, even if it is compiled correctly, it will not work properly. When the request parameter contains a stream type parameter, other types of parameters are not allowed in the parameter list. So this feature is impossible to achieve.
This application scenario is usually implemented when we submit a Form data. Form data are not supported by the Restful-style WCF service.
[https://stackoverflow.com/questions/57386814/wcf-service-call-to-upload-image-from-angular/57391618#57391618]1
I suggest you use Asp.net WebAPI to accecpt the multipart-data(form-data).
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22