ITPub博客

首页 > IT基础架构 > 网络安全 > WCF Services Sample: Create and Host a Service in IIS

WCF Services Sample: Create and Host a Service in IIS

原创 网络安全 作者:yuzhangqi 时间:2010-05-31 10:05:51 0 删除 编辑

In this blog I will show you how to create and host a WCF services from scratch.

1. Define the Services Contract

Open Visual Studio 2008, File -> New -> Project. It will open the "New Project" window for you. Select "Web" under the "Visual C#" on the left treeview "Project types", then select "WCF Service Application" under the "Templates" on the right side window. Input the project Name and Location,check "Create dictionary for solution". then click "OK".

Visual Studio 2008 will create a service contract "IService1" and a service "Service1" automatically for you.

Now, let's remove code in the "IService1.cs" and "Service1.svc.cs" and define our service contract.

IService.cs

using System.ServiceModel;

namespace ServiceHost
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IService1
{ [OperationContract]
string DummyOperation(string userName);
}
}

Service1.svc.cs

using System.ServiceModel;

namespace ServiceHost
{ // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1 { public string DummyOperation(string userName) { string userToken = OperationContext.Current.IncomingMessageHeaders.GetHeader("SecurityToken", ""); return ("Hello World," + userName); } } }

Service1.svc

In the Solution Explorer, right click "Service1.svc", select "View Markup" in the popup menu.

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceHost.Service1" CodeBehind="Service1.svc.cs" %>

2. Config the WCF service

Open the web.config file, and look at the node.




<!-- Service Endpoints -->

<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->










<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

<!-- 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 -->




You need not to make any changes to the web.config at this point.

3. Test the WCF service

Now, we have created a WCF service and host it in IIS. To confirm the service can work, right click the "Service1.svc" in the Solution Explorer, select "View in Browser". If you can see the following page, then you succeed.

Next, I will show you how to create a Silverlight client to call this WCF service.

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13651903/viewspace-1034046/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: WCF Essentials (5)
请登录后发表评论 登录
全部评论

注册时间:2008-06-10

  • 博文量
    102
  • 访问量
    763304