Hi Community,

 

We have this requirement, where we need to receive a complex json object and then store it in our database as a string, without using DataContracts.

 

To achieve this we though about using the "object" type and then serialize it into a string using JsonConvert.SerializeObject method. 

 

	  [OperationContract]
	  [WebInvoke(Method = "POST", UriTemplate = "/store", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
      public Response AddStore(object store) {
 
         Response response = new Response();
         response.MessageId = "1";
         response.Description = "Store Inserted!";
 
        string storeString = JsonConvert.SerializeObject(store, Formatting.Indented);
 
        var ins = new Insert(UserConnection)
        .Into("ImdStagingInArticle")
        .Set("ImdMessage", Column.Parameter(storeString))
        .Set("ImdStateId", Column.Parameter(new Guid("2d1dee23-058b-45fe-95a0-2e1612fc4261")));
 
        ins.Execute();
 
        return response;   
      }

 

However, the string is always empty. 

 

Since the json is to complex, the serializer cannot process the entire json data. 

 

We also tried to use WebOperationContext, but that doesn't work.

 

Can someone help us solve this issue? Did anyone have something similar?

 

Thank you.

Like 0

Like

7 comments

Hi,

 

First of all you need to make sure that you can convert the object into a string in general. Like create a Visual Studio project, add the logic to convert the object into a string using JsonSerializer like

 

string jsonString = JsonSerializer.Serialize(myObject); Console.WriteLine(jsonString);

 

And see the result. After the number of tests find the way when the data you pass is converted into the string and then start developing an additional logic of storing this data in the Creatio database using the insert query you used.

 

Alternatively find the way to make the object to have a constant set of keys so to be sure it will be converted to the string as needed.

Oleg Drobina,

 

After some changes I got it to work, by reading the raw body before the automatic parsing. However, this is working only on our Linux environment. As for for windows environment (Creatio Cloud), we are receiving some parse errors. 

 

      [OperationContract]
      [WebInvoke(Method = "POST", UriTemplate = "/article", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
      public Response AddArticle(string articleJson) {
 
        string result = "";
        Stream inputStream = HttpContext.Current.Request.Body;
 
        inputStream.Position = 0;
        using (StreamReader reader = new StreamReader(inputStream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }
 
        var parsedJson = JsonConvert.DeserializeObject(result);
        string jsonString = JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
 
 
 
 
 
        Response response = new Response();
        response.MessageId = "1";
        response.Description = "Article Inserted!";
 
 
 
        var ins = new Insert(UserConnection)
        .Into("ImdStagingInArticle")
        .Set("ImdMessage", Column.Parameter(jsonString))
        .Set("ImdStateId", Column.Parameter(new Guid("2d1dee23-058b-45fe-95a0-2e1612fc4261")));
        ins.Execute();
 
        inputStream.Close();
 
        return response;  
      }

 

What could be the cause for such weird behaviour?

 

Thank you.
      

 

Pedro Pinheiro,

 

Well, it shouldn't happen. Maybe there is still some difference in the content we pass in the local machine and in the Windows machine?

Oleg Drobina,

 

The requests are the same for both local and cloud environments, except the address and credentials.

 

However, when I execute the request on the cloud environment I get this error:

 

 <p class="heading1">Request Error</p>
       <p>The server encountered an error processing the request. The exception message is 'This method or property is
           not supported after HttpRequest.GetBufferlessInputStream has been invoked.'. See server logs for more
           details. The exception stack trace is: </p>
       <p> at System.Web.HttpRequest.get_InputStream()
           at Terrasoft.Configuration.ImdArticleAPI.ArticleAPI.AddArticle(Object article)
           at SyncInvokeAddArticle(Object , Object[] , Object[] )
           at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp;
           outputs)
           at Terrasoft.Web.Common.ServiceModel.ThreadContextInitializer.Invoke(Object instance, Object[] inputs,
           Object[]&amp; outputs)
           at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
           at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
           at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)
           at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>

 

Have you encountered something similiar? What could be the cause for such error?

 

Thank you.

 

Pedro Pinheiro,

 

Creatio doesn’t know how to serialize/deserialize object. If you change object to string in Response AddStore(object store) you will be able to parse it with JsonParser of your choice.

Kirill Krylov CPA,

 

The problem is, if I change it to string I can only send strings to this endpoint (The Json must be converted to a string). Because, if I try to send a Json Object I get "400 Bad Request".

 

 

 

Hello everyone,

 

I managed to get it working by receiving the body as a Stream instead of a String.

 

With this solution, I don't get the "400 Bad request" and I can process any Json object.

 

I don't know if this is the best solution, but it's working for now.
 

	  [OperationContract]
      [WebInvoke(Method = "POST", UriTemplate = "/article", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
      public Response AddArticle(Stream requestBody) {
 
        string result = "";
 
        using (StreamReader reader = new StreamReader(requestBody, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }
 
        var parsedJson = JsonConvert.DeserializeObject(result);
        string jsonString = JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
 
        Response response = new Response();
        response.MessageId = "1";
        response.Description = "Article Inserted!";
 
        var ins = new Insert(UserConnection)
        .Into("ImdStagingInArticle")
        .Set("ImdMessage", Column.Parameter(jsonString))
        .Set("ImdStateId", Column.Parameter(new Guid("2d1dee23-058b-45fe-95a0-2e1612fc4261")));
        ins.Execute();
 
        return response;  
      }

 

Feel free to give any feedback on this solution.

 

Thank you.

 

Show all comments

Hi Creatio academy,

We have a Creatio Instance which is running in the Cloud with version: 8.0.8.4807_SalesEnterprise_Marketing_CustomerCenter_Softkey_PostgreSQL_ENU.

 

We have developed a custom package (with the 'Compile into a separate assembly' checkbox true). 

 

As a result we can not access the Newtonsoft.Json package from UserTask.

 

The normal solution is to import the dll in our custom pacakge as a reference assembly.

Question: Wich is the correct version of the dll to import ?

 

I have the binary files of '8.0.8.4807_SalesEnterprise_Marketing_CustomerCenter_Softkey_PostgreSQL_ENU' and in the root/bin folder i can see the NewtonSoft.Json dll. Is this the correct one to import ?

 

Like 0

Like

4 comments
Best reply

Hi Sasori,

 

Yes, you can use the existing lib and upload it to your assembly package. This should do the same as the lib from the main workspace.

Hi Team,

Any update regarding this question ?

Hi Sasori,

 

Yes, you can use the existing lib and upload it to your assembly package. This should do the same as the lib from the main workspace.

Thanks for the confirmation Oleg!

Hi all, 
I would like to use an external library called csvHelper in a SourceCode file. 
 

I managed to download the .dll file of that library but I want to import that to my package. Can you please help me with that ?

Show all comments