Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

servicestack - Json Format data from console application to service stack

I found you are the only one who answers for service stack, I don't have emails and what ever you provided for last questions to me, seems fine.

I have seen your profile and as you are the main founder of mythZ, i seems to ask you my question again.

For one of my question, POST data in JSON format to Service stack, i appreciate your answer. your answer is right, however in my case i am having following case.Let me describe in more detail.

I have seen "Hello World" example of service stack. I got link for https://github.com/ServiceStack/ServiceStack.Extras/blob/master/doc/UsageExamples/UsingRestAndJson.cs

In my case, I am having console application which calls service stack (which inserts data in DB) Now, in that console application, i have made one class (class1) which is there in service stack with the same properties.

I assign values to properties of that class in my console application and POST the whole object to service stack. Syntex is like below

 JsonServiceClient client = new JsonServiceClient(myURL);
       MYclass cls= MakeObjectForServiceStackToInsertData();
        var res = client.Post<MYClass>("/postrequest", cls); 

I have use POST as above. which seems okay. at the service stack end in OnPOST event, i get this data and insert in DB. It is working fine for me.

Now my client, wants that we need to pass data in any format. JSON/XML. I know it is possible as you provide me a "Hello world" example link, it is mention over there.

But all i found is, they have used ajax/Jquery to post data to service. In my case this is console application, so i am not able to use ajax/Jquery. I am wondering that, is it possible to pass data in JSON format and do operation in my case.

Thank you very much in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So if you want to post any untyped and free-text JSON or XML to ServiceStack then you wont be able to ServiceStack's generic typed C# clients (i.e. its JsonServiceClient, XmlServiceClient, etc). Instead you just need to use any basic Http Client like the HttpWebRequest that comes with .NET.

As I've mentioned earlier sending free-text json or xml is not the normal way to call ServiceStack web services (i.e. it's recommended to use typed DTOs and one of the generic service client), but since you've asked here are stand-alone, dependency-free examples of how to call ServiceStack's Hello World example web service:

Sending free-text JSON

const string RemoteUrl = "http://www.servicestack.net/ServiceStack.Hello/servicestack/hello";

var httpReq = (HttpWebRequest)WebRequest.Create(RemoteUrl);
httpReq.Method = "POST";
httpReq.ContentType = httpReq.Accept = "application/json";

using (var stream = httpReq.GetRequestStream())
using (var sw = new StreamWriter(stream))
{
    sw.Write("{"Name":"World!"}");
}

using (var response = httpReq.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    Assert.That(reader.ReadToEnd(), Is.EqualTo("{"Result":"Hello, World!"}"));
}

Sending free-text XML

var httpReq = (HttpWebRequest)WebRequest.Create(RemoteUrl);
httpReq.Method = "POST";
httpReq.ContentType = httpReq.Accept = "application/xml";

using (var stream = httpReq.GetRequestStream())
using (var sw = new StreamWriter(stream))
{
    sw.Write("<Hello xmlns="http://schemas.servicestack.net/types"><Name>World!</Name></Hello>");
}

using (var response = httpReq.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    Assert.That(reader.ReadToEnd(), Is.EqualTo("<HelloResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.servicestack.net/types"><Result>Hello, World!</Result></HelloResponse>"));
}

I have added the above examples into this Runnable Unit Test.

I recommend getting familiar with a HTTP traffic analyzer tool so you can easily see the HTTP traffic that is sent and received to and from your web service. From then on, being able to workout how to call your service becomes trivial. Some great HTTP traffic analyzers include:

  • Fiddler
  • Network inspectors in Browser (e.g. Chrome, Safari, Firefox and IE have great tools)
  • WireShark

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...