第四章:WCF托管(1)

[size=large][color=red]原文:[url]http://www.wcftutorial.net/Introduction-to-WCF.aspx[/url][/color][/size]

[size=x-large][color=orange]IIS 5/6托管[/color][/size]

将服务托管在IIS中最大的好处是,当他接受到客户端的第一个请求的时候会自动的启动托管进程。它使用了IIS的众多特性,比如进程回收,空闲关闭,进程健康状态监视和消息驱动。而最大的不足在于,它只支持HTTP协议。

让我们随手做点什么吧,创建一个托管在IIS上的服务。

Step 1:启动VS2008,File->New->Web Site,选择WCF Service并且存放在路径http。这将会直接将服务托管在IIS上,然后选择OK。

[img]http://www.wcftutorial.net/Images/060100_VS2008Start.jpg[/img]

Step 2:创建一个HelloWorld的服务,这个服务将会接受一个name作为参数,然后返回Hello name。接口和实现如下所示。

[b]IMyService.cs[/b]

[ServiceContract]
public interface IMyService
{
[OperationContract]
string HelloWorld(string name);
}


[b]MyService.cs[/b]

public class MyService : IMyService
{
#region IMyService Members
public string HelloWorld(string name)
{
return "Hello " + name;
}
#endregion
}


Step 3:Service文件(.svc)包含了服务名称和后台的程序代码。这个文件被用来公布服务。

MyService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>


Step 4:需要在服务端的config文件中进行配置。这里我们只配置了一个wsHttpBinding的终结点,我们还可以配置多个不同绑定协议的终结点。因为我们是托管在IIS中的。我们只能使用Http绑定。在后面的教程中,我们会了解到更多有关于终结点和配置的知识。

[b]Web.Config[/b]


<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the
metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
</behaviors>
</system.serviceModel>


[b]注意:[/b]

你需要在配置文件中配置服务名和服务地址。

[img]http://www.wcftutorial.net/Images/060100_IIS.jpg[/img]

当我们运行程序的时候。

[img]http://www.wcftutorial.net/Images/060100_ServiceRun.jpg[/img]

Step 5: 我们现在成功的将服务托管在了IIS上,下一步我们在创建客户端应用程序来调用该服务。在创建客户端应用程序之前,我们需要为服务创建代理。这些代理会在客户端程序中使用来与服务进行交互。为了创建代理,运行VS2008命令行工具,使用service工具来创建代理类和它的配置信息。

[i]svcutil http://localhost/IISHostedService/MyService.svc[/i]

[img]http://www.wcftutorial.net/Images/060100_ProxyCreation.jpg[/img]

Step 6: 现在我们开始创建VS2008客户端的控制台应用程序。

[img]http://www.wcftutorial.net/Images/060100_ClientVS2008start.jpg[/img]

Step 7: 添加System.ServiceModel引用

[img]http://www.wcftutorial.net/Images/060100_Addreference.jpg[/img]

Step 8: 为代理类创建对象,并且调用HelloWorld方法


static void Main(string[] args)
{
//Creating Proxy for the MyService
MyServiceClient client = new MyServiceClient();
Console.WriteLine("Client calling the service...");
Console.WriteLine(client.HelloWorld("Ram"));
Console.Read();
}


Step 9: 运行该程序,输出如下

[img]http://www.wcftutorial.net/Images/060100_Output.jpg[/img]

我希望你会喜欢将服务托管在IIS上,现在让我们看看如何将程序托管在self-host之上。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值