简体   繁体   中英

WCF Service Application (Project) or WCF Service (Website) for WCF REST Service

I want to create a RESTful WCF Service for a Project I am about to write (in C#). The front end will be in JavaScript and will be made by another programmer, so he has to be able to access my Service from the same local network.

When I wanted to create a new Project in IIS, I saw that there also is an option for a new Web Site where I can also choose WCF Service image

Which one will be the right one to use, if I want to be able to host my Application in IIS (in development without having to publish it over and over again)

I already read the MSDN Article about the differences, and from the sound of it a Web Site project is more of what I want.

And I would also be thankful for a short explanation on what to do, because I already found a quick tutorial on creating a WCF REST Application, but not for a WebSite

That MSDN article is extremely old. You want a WCF Service Application project.

For a REST service only accessed from JavaScript you might be better off with Web API. WCF is overkill for that, especially if you don't already know WCF. See http://msdn.microsoft.com/en-us/library/jj823172.aspx for the differences and http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api for a tutorial.

There is more than one way to skin a cat (as the expression goes). So I will tell you how I went about creating a solution for a similar requirement (front was going to be developed using Angular).

I decided on creating both: a wcf service library (project) and a wcf service (website). I chose this because I preferred to separate the class library into its own project for future re-use, but this guy explains well why a service application is better https://stackoverflow.com/a/12339815/1005142 . The choice is yours.

The steps I took were:

Note: This example doesnt include authentication or Cross Domain issues which could be worth researching.

  1. Create a new WCF service library (Project)
  2. Create a new WCF Service (Website)
  3. Reference the WCF Service library in the Website (RightClick bin folder)
  4. Rename the IService and Service files and Refacter the names to something more suitable.
  5. Add a service interface such as:

     /// <summary> /// Test method that returns pings the service /// </summary> /// <returns></returns> [WebGet(UriTemplate = "ping", ResponseFormat = WebMessageFormat.Json)] [OperationContract] string Ping(); 
  6. Create the associated service method, such as

     /// <summary> /// Service method to ping the RESTful service /// </summary> /// <returns></returns> public string Ping() { return "{Message" + ":" + "Pong" + "}"; //here I return a raw json string, but you could return a serialised object (like the //CompositeType example) Then the conversion to JSON would be done transparently. } 
  7. Right click on the Web.Config file and select 'Edit WCF configuration' this gives you a tool that helps configuring your file. Im not going to explain all the option, but heres how my config file looks.

Web.Config文件(对不起,它是一个jpg,粘贴xml并没有显示)

  1. Change the Service.svc file to include the service you just created

  2. Build the project and run the service website, which should take you to the root directory web page. Click on Service.svc and it should give you a summary.

  3. Copy the url which looks like http://localhost:63718/Service.svc and add your URITemplate name to the end (which was ping). So the URL looks like http://localhost:63718/Service.svc/ping

  4. Consume the link. Use Fiddler, Chrome/Firefox plugin to test the link

  5. You will still have to publish to IIS. You can right click the Service website, select 'Publish Web Site' and then configure your server details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM