一步步教你创建.NET 4服务并且寄宿在IIS 7.5中
本文译自Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5最近在学习WCF的时候。寄宿IIS7.5这部分总是搞不定。搜了很长时间。发现也是很多文章也是人云亦云。根本通不过。于是组合了一下关键字,搜了一下英文的文章。总算是搞定了。
目标
本文将会一步步教给你
[*]怎么样创建一个基本的 WCF 4.0 服务?
[*]怎么样把WCF服务寄宿在IIS 7.5?
[*]客户端如何测试服务可用
创建WCF服务
创建WCF服务,打开VS,选择新工程,然后从WCF的标签页里,选择WCF服务应用程序,来创建一个新的WCF服务。
http://beyondrelational.com/images/service_and_hosting_iis.JPG
在IService1 .cs 和Service1.svc.cs中删除WCF自己添加的默认的代码。
a. 因此删除了数据契约
b. 修改服务契约如下. 仅仅写一个方法契约返回一个字符串
IService1.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
usingSystem.Text;
namespaceWcfService5
{
publicinterfaceIService1
{
stringGetMessage();
}
}
Service1.svc.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
usingSystem.Text;
namespaceWcfService5
{
publicclassService1:IService1
{
publicstringGetMessage()
{
return"Hello From WCF Service ";
}
}
}
配置文件就保留为WCF默认的就行了
Web.Config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
寄宿WCF服务在IIS7.5中
打开IIS
a. 使用管理员权限打开命令行. 点击开始->运行 输入“inetmgr”打开 IIS .
http://beyondrelational.com/images/service_and_hosting_iis1.JPG
然后你就会到下面这个地方。
http://beyondrelational.com/images/service_and_hosting_iis2.JPG
b. 在“网站”上右击,点击“添加新网站”
http://beyondrelational.com/images/service_and_hosting_iis3.JPG
c. 然后就打开了新窗口
http://beyondrelational.com/images/service_and_hosting_iis4.JPG
网站名字随便给。. 我给了个叫做 HostedWcfService的名字
http://beyondrelational.com/images/service_and_hosting_iis5.JPG
然后点击选择,在应用程序池选择 ASP.Net v4.0,(ps:如果没有的话是IIS没装全)
http://beyondrelational.com/images/service_and_hosting_iis6.JPG
现在在物理路径这里,我们需要把服务的物理路径填上,为了知道这个物理路径,我们在VS里,资源管理视图的的我们的WCF工程上点击右键,选择在windows资源管理器打开。然后从资源管理器的地址栏把那个地址复制下来。或者你如果很清楚的话自己在浏览里找到就行了
http://beyondrelational.com/images/service_and_hosting_iis7.JPG
然后再绑定这里我们选择 HTTP. 给个任意的端口数字. 我写个4567
http://beyondrelational.com/images/service_and_hosting_iis8.JPG
主机名就不要乱填了,. 留空即可
http://beyondrelational.com/images/service_and_hosting_iis9.JPG
立即开始网站的选项也选上。
http://beyondrelational.com/images/service_and_hosting_iis10.JPG
点击ok。
d. 然后以管理员权限打开Visual Studio 命令提示(2010).
http://beyondrelational.com/images/service_and_hosting_iis11.JPG
http://beyondrelational.com/images/service_and_hosting_iis12.JPG
输入如下命令
aspnet_regiis.exe /iru
http://beyondrelational.com/images/service_and_hosting_iis13.JPG
一会会告诉你安装完成 ASP.Net 4.0(注意,有些人可能在前面的应用程序池那块没有.net 4 的选项,可以先把这一步做了再去执行前一步。)
e. 然后去 IIS, 会发现网站已经好了
http://beyondrelational.com/images/service_and_hosting_iis14.JPG
发布WCF服务
回到我们的VS中的 WCF 服务项目. 右击选择发布。
http://beyondrelational.com/images/service_and_hosting_iis15.JPG
On clicking of publish a window will come.
http://beyondrelational.com/images/service_and_hosting_iis16.JPG
给一个服务的URL,你随意给,只要保证名字唯一确定即可,服务名以svc结尾就好
http://beyondrelational.com/images/service_and_hosting_iis17.JPG
把前面创建的网站的名字 HostedWcfService 写到下面.
http://beyondrelational.com/images/service_and_hosting_iis18.JPG
证书这块就不要了
http://beyondrelational.com/images/service_and_hosting_iis19.JPG
Now click on Publish.
http://beyondrelational.com/images/service_and_hosting_iis20.JPG
点击发布后提示发布成功
在IIS中浏览服务
打开IIS,然后在 HostedWcfService.网站上点击右键 ->管理网站->浏览
http://beyondrelational.com/images/service_and_hosting_iis21.JPG
当你点击浏览。会出现下面的情况
http://beyondrelational.com/images/service_and_hosting_iis22.JPG
会得到上面的错误。但是这都不是事,在URL后面加上 Service1.svc 就打开了
http://localhost:4567/Service1.svc
在浏览器里你可以看到服务已经正常运行了
http://beyondrelational.com/images/service_and_hosting_iis23.JPG
现在你已经成功的创建了 WCF 4.0 服务并且寄宿在了 IIS 7.5中
在客户端测试WCF服务
a. 创建一个控制台工程
http://beyondrelational.com/images/service_and_hosting_iis24.JPG
b. 右键点击,添加服务引用
http://beyondrelational.com/images/service_and_hosting_iis25.JPG
c. 给出服务的地址,点击前往,(或者直接点击发现)选中服务,点击确定
http://beyondrelational.com/images/service_and_hosting_iis26.JPG
d. 如下使用服务
Program.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingConsoleApplication1.ServiceReference1;
namespaceConsoleApplication1
{
classProgram
{
staticvoidMain(string[]args)
{
Service1Client proxy=newService1Client();
Console.WriteLine(proxy.GetMessage());
Console.Read();
}
}
}
输出结果。
http://beyondrelational.com/images/service_and_hosting_iis27.JPG
原文链接:http://leaver.me/archives/1245.html
页:
[1]