设为首页 收藏本站
查看: 716|回复: 0

[经验分享] Hosting WCF Service in IIS

[复制链接]

尚未签到

发表于 2015-8-14 14:24:40 | 显示全部楼层 |阅读模式
Hosting WCF Service in IIS

  
  All right!! So I have created a WCF service and I want to consume it…what do I do? Where do I host the service? The first choice that comes to one’s mind (and which most of the examples talk about) is creating a console application and hosting the WCF service within this application. But this option is good only when you want to host the service quickly and test it out. In a practical scenario, we would require a service which sits there and is available all the time and allows a message based activation. Also, wouldn’t it be great if we can get rid of the extra code that we have to write just to host the service? Fortunately, WCF provides various options when it comes to hosting a service. These options are:
  1.       Console application
  2.       Winform application
  3.       IIS
  4.       Windows service
  5.       Windows Activation Service.
  Of course, the choice of the host restricts the type of transport that you can use for that service. I will not go into those details in this post. Let us look at how we can host the WCF service in IIS. When we use IIS to host WCF services, the services are integrated into ASP .NET. Thus the WCF services can take advantage of some of the inherent feature of ASP .NET such as process recycling, process health monitoring, message-based activation and idle shutdown. So let us get started…
  Create a WCF service
  1.       Create a blank solution and add a WCF Service Library project to it. For the purpose of this post, I am going to create a simple GreetingService that has a method Greet. This method accepts a single parameter name and returns a string “Hello “ appended to name
  
  
  
  2.       The following is the code for the GreetingService WCF service. We define a service contract, mark the operations in that contract that are going to be exposed to the external world and then implement the service.

  
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.ServiceModel;
  
  namespace Wcf.Samples.ServiceLibrary
  {
      [ServiceContract()]
      public interface IGreeting
      {
          [OperationContract()]
          string Greet(string name);
      }
  }
  GreetingService.cs
  using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace Wcf.Samples.ServiceLibrary
  {
      public class GreetingService : IGreeting
      {
  
          #region IGreeting Members
  
          public string Greet(string name)
          {
              return "Hello - " + name;
          }
  
          #endregion
      }
  }
  

  
  3.       Now that you added the service contract and its implementation (the GreetingService), build the project.
  
  Host the service in IIS
  1.       Now we create a Web site that will host the WCF service that we created in the previous section. For this, right-click the solution in the solution explorer and from the context menu, select Add -> New Web site ->WCF Service as shown in the following figure.
  
  
  
  2.       This will create a WCF Service Web site into your solution along with the standard folders that get created for a Web site project (App_Code, App_Data etc). A new type of file called Service.svc will be generated and placed into the root of the Web site project, along with the corresponding code-behind file (Service.cs). The Service.cs is provided to implement a WCF service, which then can be referred in the Service.svc file. Since we already have created the WCF service in a separate assembly, we will refer to it in the Service.svc. As such we don’t need Service.cs and hence, it can be deleted.
  3.       Open Service.svc file and modify the single line in it like this:
  <%@ ServiceHost Language="C#" Debug="true" Service="Wcf.Samples.ServiceLibrary.GreetingService" %>
  4.       In the above statement we point to the fully-qualified class name that implements the service that we want to host in IIS.
  5.       Add the reference of the Wcf.Samples.ServiceLibrary project to the web-site project and build the web-site.
  6.       This completes the creation of WCF service. Now, in order to let the service communicate with the external world, we need to define the communication behavior of the service. For defining this behavior, we will use the Service Configuration tool that comes along with Visual Studio 2005.
  7.       From the main menu of Visual Studio 2005, select Tools -> WCF Service Configuration Editor. The WCF Configuration Editor window will open up. Select File -> Open -> Config File&#8230; from the main menu. Browse and select the Web.config file of the WCF web-site.
  8.       Once selected, the screen will be displayed as follows. The Web.config already has the configuration for the default service MyService under the Services node in the left-panel. Select this service and deleted it. We are going to create a new configuration for our service.
  
  
  
  
  9.       Right-click the Services node and select &#8220;New service&#8221; from the context menu. A service with service type NewServiceType will be created. On the right-panel, select the property &#8220;Name&#8221; and click the ellipsis. Service Type Browser will open up. Browse the bin folder of the web-site and locate the service assembly Wcf.Samples.ServiceLibrary and double-click it. The Service Type Browser dialog now will list the service &#8220;Wcf.Samples.ServiceLibrary.GreetingService&#8221; service. Select this service and click Open. This will set the service-type that we are going to configure.
  10.   Now for the above service, we first need to specify the end-point. Right-click Endpoints node in the left-panel of the WCF Service Configuration Editor and select &#8220;New Endpoint&#8221;. This will create a default end-point with its properties being displayed in the right-panel.
  11.   Set the following properties:
  
  Name
  defaultEp
  Address
  http://localhost:28053/GreetingService/Service.svc
  &#183;         Here 28053 is the port-number where the local web-server is running. You need to check your port number and enter it here appropriately.
  Binding
  basicHttpBinding
  Contract
  Wcf.Samples.ServiceLibrary.IGreeting
  &#183;         You can click the ellipses to open the Contract Type Browser and select the appropriate assembly and contract from the bin directory of the web-site.
  
  12.   This sets the basic communication for our service. In order to enable the service for metadata exchange (thereby allowing us the browse its wsdl), we need to set the metadata exchange properties for the service. To do this, expand the Advanced node in the left-panel, right-click Service Behaviors and select &#8220;New Service Behavior Configuration&#8221;. This will add a new Behavior Configuration by the default name NewBehavior. Behavior is a collection of attributes (here, service attributes) that can be set and applied to the service together. Right now, we are going to define a behavior that allows the metadata exchange on the service. Set the Name property of the new behavior configuration to mexBehavior.
  13.   In the &#8220;Behavior element extension position&#8221; (lower part of the right-panel), click &#8220;Add&#8221; button. This opens up a dialog &#8220;Adding Behavior Element Extension Sections&#8221;. Select serviceMetadata from the list and click &#8220;Add&#8221; on the dialog. This adds the extension serviceMetadata to the grid in the right-panel. Double-click the extension to open up its property-page. Set the property HttpGetEnabled to true.
  
  
  
  
  
  14.   Now that we have defined the behavior separately, we need to associate the behavior with our service. To do this, select the Wcf.Samples.ServiceLibrary.GreetingService under the Services node in the left panel. The right-panel will display its properties. Select the BehaviorConfiguration property, and select &#8220;mexBehavior&#8221; from the drop-down.
  15.   This sets the service configuration and allows the service to communicate with the external world. Save the configuration by selecting the menu File->Save. Close the WCF Service Configuration Editor.
  16.   Test that the service is hosted by running the WCF Web Site application. The following screen should be displayed.
  
  
  
  You can test this service by creating a Console application and adding a proxy (generate the proxy by running the svcutil.exe utility). In the next post, I will explain how to invoke a WCF service hosted in IIS through the new WCF Adapter in BizTalk R2.
  
  Till then&#8230; happy coding J
  
  

Published Monday, May 21, 2007 4:26 PM by Amit Lale
Filed under: .Net, WCF, IIS, How-to

建立客户端:
1、添加一个console 应用程序;
2、生成客户端代理代码、配置文件;svcutil.exe /language:cs /out:proxy.cs config:app.config http://localhost:28053/GreetingService/Service.svc
3、添加这两个文件到工程,Program.cs中调用之。
build 工程。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-99044-1-1.html 上篇帖子: [原创] 给IIS站点启用GZIP压缩,效果明显(有详细评测+截图) 下篇帖子: 让ASPX和ASMX脱离IIS运行的例子(ASP.NET宿主程序)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表