web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Custom service with co...
Finance forum

Custom service with contracts and JSON endpoint

(1) ShareShare
ReportReport
Posted on by 642

Hi,

I just created a custom service with one method that requires a contract in input and a contract in output.

Example :

ContractOuput myMethod(Contract Input)

I created a small C # application to test the service, I tested the SOAP endpoint and it worked very easily  but with the endpoint JSON how I can do it with contracts ?

I alos tried with success a simple case in JSON with a service method  that have two integers parametrs (like explained in the blog of Martin Drab : http://dev.goshoom.net/en/2017/02/json-based-custom-service-with-parameters/ )

How should I work with contracts for JSON calls?

Thanks in advance.

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    239,381 Most Valuable Professional on at

    It's very similar, just instead of a primitive value, you provide an object in JSON notation.

    For example:

    {
    '_input': { 'ContractProperty1': 'Value 1', 'ContractProperty2': 'Value 2' } }
  • DONA Profile Picture
    642 on at

    Thanks Martin, but is it possible to seralize my contract in JSON

    In D365fo, I have this contract :

    [DataContractAttribute('InputContract')]

    class InputContract

    {

    str myValue;

    [DataMemberAttribute('MyValue')]

    str parmMyValue(str _myValue = myValue)

    {

    myValue = _myValue;

    return myValue;

    }

    }

    In my project C#, I created a similar contract :

    public class InputContract

    {

    public int MyValue { get; set; }

    }

    How Can I do for serialize  my C# contract for sending to D365fo ?

    InputContract contract = new InputContract { MyValue = "Hello" };

    request.Content = new StringContent(JsonConvert.SerializeObject(contract),

                                       Encoding.UTF8,

                                       "application/json");

    This code does not work, I think that I have to do something in addition.

  • Martin Dráb Profile Picture
    239,381 Most Valuable Professional on at

    Can you please elaborate what you mean by "does not work"?

    I suspect that the JSON string you're sending doesn't match the required structure. You didn't show your service operation, but I assume that you use InputContract at the only parameter and the JSON object you're using passing to the request should have a property of the same name. That's _input in my example above.

  • DONA Profile Picture
    642 on at

    Yes this is right there is a mismatch with contract.

    My service operation :

    public str test(InputContract  _contract)

       {

           return _contract.parmMyValue();

       }

    I tried with a json hardcorded :

    string json ="{\"_contract\":{\"MyValue\":\"hello\"}}";

    request.Content = new StringContent(json, Encoding.UTF8,  "application/json");

    var result = client.SendAsync(request).Result;

    I obtain an internal server error

  • Martin Dráb Profile Picture
    239,381 Most Valuable Professional on at

    You should be able to find error details in event logs (I don't remember the path exactly, it's something like Application Logs > Microsoft > Dynamics > …).

    By the way, you can use "{'_contract':{'MyValue':'hello'}}" instead of "{\"_contract\":{\"MyValue\":\"hello\"}}". It's easier to read.

  • DONA Profile Picture
    642 on at

    Hi Martin,

    Thanks, It works when I use :  "{'_contract':{'MyValue':'hello'}}".
    Is-it a way to generate this JSON with root "_contract" automatically

    Thank you

     

  • Verified answer
    Martin Dráb Profile Picture
    239,381 Most Valuable Professional on at

    You're already using JsonConvert.SerializeObject(), therefore you know that you can serialize an object graph to JSON. The only problem is that your objects don't match the structure you want (the root object should have _contract data member pointing to your InputContract, while you're trying to serialize InputContract itself.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans