Monday, May 4, 2015

HƯỚNG DẪN TẠO VB SERVICE JSON:

B1:
 Tạo project như sau.

B2:
Mở file IService trong thư mục App_Code và thêm dòng
Class IService:

[ServiceContract]
public interface IService
{

[OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]
MyService.Service1.Persion GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}


Class Service:


namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public Persion GetData(int value)
        {
            return new Persion() {
            ID=5,Name=""};
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public class Persion {
            public int ID { get;set;}
            public String Name { get; set; }
        }
    }
}

B3:
Tiếp theo là đến mục config cho service.
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="MyService.Service1">
        <endpoint binding="webHttpBinding" contract="MyService.IService1"></endpoint>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

B4:
Test Service. Cài đặt restclient cho FireFox.
Download bản cài đặt tại : https://addons.mozilla.org/vi/firefox/addon/restclient/
Bặt firefox vào mục tools -> Add-ons -> Install Add-on from file.

Kết quả sau khi test.


19 comments:

  1. I have a fancy on your posts.Your writings supply information and knowledge I need.They have lots of knowledge and tips. I hope you can upload more posts. I want to thank you for sharing.
    best app currency converter currency converter app download

    ReplyDelete
  2. I like to read your posts very much.interesting challenge for a weblog. I’ll apt to be again to study much more, thank you that information.
    https://bit.ly/2QslLr4
    https://autoketing.com/project/sale-pop-master, autoketing app

    ReplyDelete
  3. Wow I can say that this is another great article as expected of this blogThis post is basically easy to look at and see without overlooking any genuine segments.
    autoketing.com facebook inbox facebook live chat help

    ReplyDelete
  4. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.
    autoketing
    Shipping Bar for Shopify
    Free shipping bar

    ReplyDelete
  5. I do not even understand how I ended up here, but I thought this publish was great. I don’t recognize who you might be however certainly you are going to a famous blogger if you happen to are not already. Cheers!
    email with love online
    apps autoketing
    email with love free

    ReplyDelete
  6. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
    friv10games club Games to play, jogos kizi-2018, gogy xyz Games to play

    ReplyDelete
  7. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    gaigoionline, clip gai goi, gai goi hn, gai bao, gai goi hcm

    ReplyDelete
  8. Thanks for sharing the post.The articles bring insight and knowledge.Thank you for your contribution to the community.
    gogy for kids motox3m3 free games abcya3 play now

    ReplyDelete
  9. Shell Shockers Game
    The-light-temple-fireboy-and-watergirl-2 Game
    Sling-drift Game
    Hello, Neat post. There is a problem together with your site in internet explorer, may test this? IE still is the market chief and a huge portion of other people will leave out your fantastic writing because of this problem.

    ReplyDelete
  10. Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet!
    dynamons world club free games , my friend pedro club for school , pixel gun apocalypse 3 ,

    ReplyDelete
  11. Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information.
    Games xmas magic tiles
    Truck physics free
    Gunmaster onslaught games for kid

    ReplyDelete
  12. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing
    free mr dubstep , moto x3m 4 winter games , game santa claus jump ,

    ReplyDelete
  13. Hi! Thanks for the great information you have provided!This is a great inspiring article.Among the lots of comments on your articles. Thanks for sharing.
    Blocky Craft Police Squad play for free game Mommy Elsa Baby Caring Tomb Runner play game for free

    ReplyDelete
  14. The site is very interesting, you made some compelling remarks and the matter is on point. I’d choose to make use of some with the content on my blog whether or not you don’t mind.Fantastic.
    online game Papa's Scooperia online game Def Island Kick The Buddy online game

    ReplyDelete
  15. I just found this blog and have high hopes for it to continue. Keep up the great work. its hard to find good ones. I have added to my favorites. Thank You.bob the robber 4 japan games , fireboy and watergirl 4 online games to play , free mario banzai ,

    ReplyDelete
  16. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best!
    miniclip games, a10 games to play, jogos para crianças

    ReplyDelete
  17. This was truly a fascinating point and I kinda concur with what you have said here! Hi, I have perused a large portion of your posts. This post is likely where I got the most helpful data for my exploration
    friv 4 game
    2player games for kids
    io jogos for kids

    ReplyDelete

  18. food games free
    basketball games online
    soccer free games
    This was truly a fascinating point and I kinda concur with what you have said here! Hi, I have perused a large portion of your posts.

    ReplyDelete