[Data sharing (dry goods)] wcf rest Service releases data and obtains data, wcfrest

Source: Internet
Author: User

[Data sharing (dry goods)] wcf rest Service releases data and obtains data, wcfrest

Recently, we are working on A project that requires us to do something. Let's call it data sharing. Our company is Company A, and the other company is Company B, company A provides A data service interface. Company B can directly call our service interface when it wants to obtain our data. We also need to obtain data from Party B, take the data of the other party directly. After negotiation, we decided to use the REST mode of the WCF Service for data sharing. The data format adopts the json and POST methods for requests.

1, first of all we want to release our data, how to write a wcf rest service, please read my article http://www.cnblogs.com/huchaoheng/p/6379026.html

1) the interface is as follows:

 [OperationContract]        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]        string GetProductListByCategoryName(string categoryName);

2) implementation interface:

JsonConvert is really amazing. json and object can be exchanged.
public string GetProductListByCategoryName(string categoryName)        {            List<Product> ProductList = Products.Instance.ProductList.Where(t => t.CategoryName == categoryName).ToList();            var result = JsonConvert.SerializeObject(ProductList);            return result;        }

 

2. How can I publish a WCF Service? We know that the WCF Service can be stored on IIS. In fact, it is not just IIS. Now, let's just store it on IIS and publish a program, enable 32-bit program to True

3. Now our service has been released. How can we get it. Click the URL of the released program, as shown in:

 

 

1) First of all, the wcf rest service is equivalent to a URL here, how do we request a URL, we need to use HttpWebRequest, how to use this, you can refer to the blog written by someone else in the http://www.cnblogs.com/kissdodog/archive/2013/04/06/3002779.html, well, now we need to write our own request URL method, so that we can easily get the json format of the Publishing Service.

 

/// <Summary> /// huchao // data request // 2017.2.12 // </summary> /// <param name = "url"> wcf rest request address </param> /// <param name = "parametrds"> the wcf rest service request parameters are in json format </param> /// <returns> </returns> public string GetResponeDataByPost (string url, string parametrds) {// request data HttpWebRequest httpRequest = (HttpWebRequest) WebRequest. create (url); httpRequest. contentType = "application/json"; httpRequest. method = "POST"; byte [] payload; payload = Encoding. UTF8.GetBytes (parametrds); httpRequest. contentLength = payload. length; Stream pRequestStream = httpRequest. getRequestStream (); pRequestStream. write (payload, 0, payload. length); pRequestStream. close (); // obtain the corresponding HttpWebResponse httpRespone = (HttpWebResponse) httpRequest. getResponse (); var myStream = httpRespone. getResponseStream (); StreamReader = new StreamReader (myStream, Encoding. UTF8); var data = StreamReader. readToEnd (); StreamReader. close (); myStream. close (); return data ;}

2) Next we will write a class for passing in the url and our parameters.

  public List<Product> GetObject(string servicesFullUrl, Object ParamInput)        {            var ParamInputs = JsonConvert.SerializeObject(ParamInput);            string s = GetResponeDataByPost(servicesFullUrl, ParamInputs);          return JsonConvert.DeserializeObject<List<Product>>(s);                    }

In this way, can we get the desired result? The s obtained here is shown in the figure below:

 

We call JsonConvert. DeserializeObject <List <Product> (s); will report an error: because the above get json and cannot be deserialized directly to list need us to deal with a little: Reference blog: http://blog.csdn.net/yyixin/article/details/7243472

/// <Summary> /// json deserialization to Dictionary // </summary> /// <param name = "strJson"> </param> // <returns> </returns> public Dictionary <string, string> JsonToDictionary (string strJson) {JavaScriptSerializer jsSerializer = new JavaScriptSerializer (); return jsSerializer. deserialize <Dictionary <string, string> (strJson );}

3) The method above is rewritten

 

Enter parameters again

GetObject ("http: /localhost: 8011/ProductRESTService. svc/GetProductListByCategoryName", new
{
CategoryName = "Category 1 ",

});

 

Then you can get the published data. Note that the categoryName must be the same as the parameter name of the published program. Otherwise, the data cannot be obtained.

 

 

You are welcome to leave a message. I will reply immediately.

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.