简体   繁体   English

WCF REST服务的WCF服务应用程序(项目)或WCF服务(网站)

[英]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#). 我想为要编写的项目创建一个RESTful WCF服务(使用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. 前端将使用JavaScript,并且将由另一个程序员制作,因此他必须能够从同一本地网络访问我的服务。

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 当我想在IIS中创建新项目时,我看到有一个用于新网站的选项,我也可以选择WCF服务映像

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) 如果我希望能够将我的应用程序托管在IIS中(正在开发中而不必一遍又一遍地发布它),那么哪一个将是正确的选择?

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. 我已经阅读了有关差异的MSDN文章 ,从它的声音来看,Web站点项目更是我想要的。

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 我也很感谢您对操作做一个简短的解释,因为我已经找到了有关创建WCF REST应用程序的快速教程,但没有WebSite的教程。

That MSDN article is extremely old. 该MSDN文章非常古老。 You want a WCF Service Application project. 您需要WCF服务应用程序项目。

For a REST service only accessed from JavaScript you might be better off with Web API. 对于仅通过JavaScript访问的REST服务,使用Web API可能会更好。 WCF is overkill for that, especially if you don't already know WCF. 为此,WCF显得过于矫kill过正,尤其是如果您还不了解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. 有关差异,请参见http://msdn.microsoft.com/zh-cn/library/jj823172.aspx ,以及http://www.asp.net/web-api/overview/getting-started-with-aspnet-web- api / tutorial-your-first-web-api的教程。

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). 因此,我将告诉您如何为类似的需求创建解决方案(前端将使用Angular开发)。

I decided on creating both: a wcf service library (project) and a wcf service (website). 我决定同时创建:wcf服务库(项目)和wcf服务(网站)。 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 . 我之所以选择它,是因为我更喜欢将类库分成自己的项目以供将来重用,但是这个家伙很好地解释了为什么服务应用程序更好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) 创建一个新的WCF服务库(项目)
  2. Create a new WCF Service (Website) 创建一个新的WCF服务(网站)
  3. Reference the WCF Service library in the Website (RightClick bin folder) 引用网站中的WCF服务库(RightClick bin文件夹)
  4. Rename the IService and Service files and Refacter the names to something more suitable. 重命名IService和Service文件,然后将名称重新命名为更合适的名称。
  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. 右键单击Web.Config文件,然后选择“编辑WCF配置”,这将为您提供一个有助于配置文件的工具。 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 更改Service.svc文件以包括您刚刚创建的服务

  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. 单击Service.svc,它将为您提供摘要。

  3. Copy the url which looks like http://localhost:63718/Service.svc and add your URITemplate name to the end (which was ping). 复制看起来像http://localhost:63718/Service.svc的URL,然后将URITemplate名称添加到末尾(即ping)。 So the URL looks like http://localhost:63718/Service.svc/ping 因此,URL看起来像http://localhost:63718/Service.svc/ping

  4. Consume the link. 使用链接。 Use Fiddler, Chrome/Firefox plugin to test the link 使用Fiddler,Chrome / Firefox插件测试链接

  5. You will still have to publish to IIS. 您仍然必须发布到IIS。 You can right click the Service website, select 'Publish Web Site' and then configure your server details 您可以右键单击服务网站,选择“发布网站”,然后配置服务器详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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