Arun Rana
Arun Rana

Reputation: 8606

What is better option to consume REST WCF using servicestack

I got some good solution from here about servicestack, now I am between 2 step and I have choose one of them. Please understand my practical scenario as per below

I have created one REST WCF using Servicestack and one Model (class) is as per below

public class Perfmon
{
        public long id { get; set; }
        public string appliationId { get; set; }

        public string cpuUsage { get; set; }
        public string availableMemory { get; set; }
        .......
        .......
} 

Now I would like to make post call on this service form another EXE project as per below

 JsonServiceClient client = new JsonServiceClient("myserviceurl");
 RESTWCF.ServiceModel.Perfmon p = new RESTWCF.ServiceModel.Perfmon();
 var res = client.Post<RESTWCF.ServiceModel.Perfmon>("/perfmon", p);

Now I have 2 options as per below

1) Need to convert XSD to class and use object of that to pass in post request as per i have asked question How can i convert XSD file to C# Class But I could not generate the class using URL directly with XSD.exe utility

2) Manually pass the json string If I have json string then it seems like below

[{1:"22", 2:"123", 3:"60", ..... }] 

(where 1 is for id, 2 is for applicationid ..to just shorten json string) then I need to convert it to C# class to pass object in post request, still I need to find the way to map with (1, 2 ..)

2nd option is some way confusing but if I can go it with then it's my client requirement to pass manually json string in post request.

Please help me to choose the better option because in simple Rest WCF we need not use class (Model) reference to make post request.

If it doesn't make sense then I can clarify it in more details

Thanks in advance

Upvotes: 2

Views: 1580

Answers (2)

mythz
mythz

Reputation: 143359

Not sure why you weren't able to generate the Model classes from XSD.exe - but that's not actually required. The normal way to use ServiceStack is to put all your ServiceModel classes (i.e. DTOs) in a separate dependency-free assembly and use it with one of the Generic JSON/JSV/XML/SOAP Service Clients.

If you had gotten your XSD.exe to generate the DTO classes than it would've simply have just generated a replica assembly of your ServiceModel.dll. If you don't want to ship them the dll, then just give them the DTO source code - which is basically what the XSD.exe utility generates (only cleaner since code-gen includes a lot of cruft/boilerplate).

So why go through all the extra code-gen + build steps? especially if you're having problems getting to generate.

Both options will work, the most ideal would be to provide the strong-typed DTOs and to use the generic service clients.

If you want to pass a JSON string instead then you would then need to use a pure HTTP client e.g. HttpWebRequest or the new HttpClient.

Calling ServiceStack REST Web Services Without C# Models

If don't want to call ServiceStack web services using your services DTOs or C# XSD.exe generated service models than instead of sending JSON, use the URL QueryString for GET requests or send standard HTTP POST Key value pairs i.e. application/x-www-form-urlencoded for HTTP POST requests. The Request DTO is automatically populated with any variables that is sent on the QueryString or POST'ed FORM data.

A common standard is to use curl or wget to show how to communicate with your REST web service since its functional and users are easily able to emulate the web service request with their favourite HTTP client.

curl -d "id=1&appliationId=2" http://example.com/myserviceurl/perfmon

See Google's Weather API for an example of this. Otherwise you can just document your REST apis like twitter does and just show them the GET Query String or POST form data examples with example output.

Upvotes: 3

amit patel
amit patel

Reputation: 2297

1> In your case if you create a class using XSD and if it is going to use Dynamically in your application than in that case you are not able to use it, because you will not get its properties that you are supposed to map in your application. 2> I think the only solution is use WCF with service reference in your application. In your case, if you go with

USING SERVICESTACK:

The main thing is service stack “POST” response. While we send the “POST” to service stack, it takes DTO Request only.

JsonServiceClient client = new JsonServiceClient("myserviceurl");
            RESTWCF.ServiceModel.Perfmon p = new RESTWCF.ServiceModel.Perfmon();
            var res = client.Post<RESTWCF.ServiceModel.Perfmon>("/perfmon", p);

constrains in for above. This is the code which we need to do in console application. It require object of “perfmon” class when we send the POST request.

Let’s consider that we are having perfmon class in console application. Then we don’t need to use JSON string, because if we create JSON string than also we have to assign properties to class, so than it can be inserted at the service stack end. (then why you want to use JSON)

USING RESTWCF:

Let’s now assume the same scenario using RESTWCF which accept “GET”,”POST” request using WebRequest. (the normal RESTWCF) In this case, of course we don’t need a reference (COPY) of the perfmon class in Monitor agent exe. Now let’s assume than we make a JSON string as you suggested

     WebRequest request = WebRequest.Create(your URL");
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "POST";
        string json = "{\"Id\":1,\"2\":\"100\",\"3\":\"1000\"}";

For above, 2 stands for CPU,3 Stands fo availableMemory etc. But, if we use this kind of JSON structure, than it will not assign properties values (because JSON Key value converts to C# class properties) Our Perfmon class shoule be like below.

of course, your class shoule be as below

[DataContract(Name = "perfmon")]
public class Performance
{

    [DataMember(Order = 1)]
    public long Id
    {
        get;
        set;
    }
    [DataMember(Order = 2)]
    public string CpuUsage
    {
        get;
        set;
    }

    [DataMember(Order = 3)]
    public string AvailableMemory
    {
        get;
        set;
    }
}

Now, Of course, if we make a JSON string like ….

string json = "{\"Id\":1,\"CpuUsage\":\"100\",\"AvailableMemory\":\"1000\"}";

than in this case, it works and assign values to properties of class, but in this case string will get more length?

SO, I GUESS THE ULTIMATE SOLUTION IS GO WITH WCF WITH SERVICE REFERENCE OR to omit JSON and pass object to service stack from exe as below(COPY OF CLASS IN REQUIRE IN EXE)

JsonServiceClient client = new JsonServiceClient("myserviceurl");
        RESTWCF.ServiceModel.Perfmon p = new RESTWCF.ServiceModel.Perfmon();
        var res = client.Post<RESTWCF.ServiceModel.Perfmon>("/perfmon", p);

Upvotes: 0

Related Questions