当前位置首页 > IIS知识

一步步教你创建.NET4服务并且寄宿在IIS7.5中

阅读次数:360 次  来源:admin  发布时间:

本文译自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服务。

在IService1 .cs 和Service1.svc.cs中删除WCF自己添加的默认的代码。

a. 因此删除了数据契约

. 修改服务契约如下. 仅仅写一个方法契约返回一个字符串

IService1.c

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
usingSystem.Text;
namespaceWcfService5
{
    [ServiceContract]
    publicinterfaceIService1
    {
        [OperationContract]
        stringGetMessage();
    }  
}

Service1.svc.c

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 .

然后你就会到下面这个地方。

. 在“网站”上右击,点击“添加新网站”

c. 然后就打开了新窗口

网站名字随便给。. 我给了个叫做 HostedWcfService的名字

然后点击选择,在应用程序池选择 ASP.Net v4.0,(ps:如果没有的话是IIS没装全)

现在在物理路径这里,我们需要把服务的物理路径填上,为了知道这个物理路径,我们在VS里,资源管理视图的的我们的WCF工程上点击右键,选择在windows资源管理器打开。然后从资源管理器的地址栏把那个地址复制下来。或者你如果很清楚的话自己在浏览里找到就行了

然后再绑定这里我们选择 HTTP. 给个任意的端口数字. 我写个4567

主机名就不要乱填了,. 留空即可

立即开始网站的选项也选上。

点击ok。

d. 然后以管理员权限打开Visual Studio 命令提示(2010).

输入如下命令

aspnet_regiis.exe /iru

一会会告诉你安装完成 ASP.Net 4.0(注意,有些人可能在前面的应用程序池那块没有.net 4 的选项,可以先把这一步做了再去执行前一步。)

e. 然后去 IIS, 会发现网站已经好了

发布WCF服务

回到我们的VS中的 WCF 服务项目. 右击选择发布。

On clicking of publish a window will come.

给一个服务的URL,你随意给,只要保证名字唯一确定即可,服务名以svc结尾就好

把前面创建的网站的名字 HostedWcfService 写到下面.

证书这块就不要了

Now click on Publish.

点击发布后提示发布成功

在IIS中浏览服务

打开IIS,然后在 HostedWcfService.网站上点击右键 ->管理网站->浏览

当你点击浏览。会出现下面的情况

会得到上面的错误。但是这都不是事,在URL后面加上 Service1.svc 就打开了

http://localhost:4567/Service1.svc

在浏览器里你可以看到服务已经正常运行了

现在你已经成功的创建了 WCF 4.0 服务并且寄宿在了 IIS 7.5中

在客户端测试WCF服务

a. 创建一个控制台工程

. 右键点击,添加服务引用

c. 给出服务的地址,点击前往,(或者直接点击发现)选中服务,点击确定

d. 如下使用服务

Program.c

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://leaver.me/archives/1245.html

上一篇:Linux系统下DedeCMS安全设置详细教程
下一篇:配置iis支持.json格式的文件