现在的位置: 首页 > 综合 > 正文

WCF 开发示例向导 Part 2 — 经典推荐!

2013年03月17日 ⁄ 综合 ⁄ 共 3752字 ⁄ 字号 评论关闭

使用Visual Studio 2008 创建WCF 应用程序

这里简要说明操作步骤: New Project 类型选择WCF Service Application,默认的项目中包含有IService1.cs, Service1.svc , Service1.svc.cs, web.config 等文件。

 

其中IServices1 接口有ServiceContract attribute,并且其中的方法也定义了OperationContract attributeService1 IService1 接口的具体实现,Endpoints 和其他行为属性在web.config 配置文件的system.serviceModel 节点定义。

 

如果你改变services 的名称,你需要同时更改 web.config 配置文件中的相关配置,否则外部系统不能识别服务。

 

 

定制默认的WCF 服务

现在我们根据实际示例项目的需求,更改默认的WCF 服务。第一步是对现有的服务文件进行改名,或删除现有的文件,然后增加新的文件。在本示例程序中,我们采用第一种方法,对现有的文件进行改名。

 

IService1.cs 改名为IWCFForumService.csService1.svc 改名为 WCFForumService.svc。代码文件Service1.svc.cs 会自动更名为 WCFForumService.svc.cs。下一步,需要更改web.config 的相关配置。

         <services>

              <service behaviorConfiguration="WcfServiceEntlib.ServiceBehavior" name="WcfServiceEntlib.WCFForumService">

                   <endpoint address="" binding="wsHttpBinding" contract="WcfServiceEntlib.IWCFForumService">

                   </endpoint>

                   <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

              </service>

         </services>

 

同时,对IWCFForumService.cs 代码进行修改,代码如下。另外,增加DataContact – MessageInfo.cs,其中的成员变量通过DataMemeber 进行标识,MessageInfo 将作为DataContract IWCFForumService 中使用。MessageInfo 是一个独立的类,标识为public,允许外部系统访问。

 

WCFForumService 则是对 IWCFForumService接口的实现类,代码如下:

        private string strConn = ConfigurationManager.ConnectionStrings["EntLibForum"].ConnectionString;

 

        public string GetMessage(int messageID)

        {

            string msg = string.Empty;

 

            string strSelect = "Select Message From yaf_message where MessageID=" + messageID.ToString();

 

            using (SqlConnection connection = new SqlConnection(strConn))

            {

                SqlCommand command = new SqlCommand(strSelect, connection);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                try

                {

                    if (reader.Read())

                    {

                        msg = reader["Message"].ToString().Trim();

                    }

                }

                finally

                {

                    // Always call Close when done reading.

                    reader.Close();

                }

            }

 

            return msg;

        }

 

        public MessageInfo GetMessageInfo(int messageID)

        {

            MessageInfo messageInfo = null;

            string strSelect = "Select * From yaf_message where MessageID=" + messageID.ToString();

 

            using (SqlConnection connection = new SqlConnection(strConn))

            {

                SqlCommand command = new SqlCommand(strSelect, connection);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                try

                {

                    if (reader.Read())

                    {

                        messageInfo = new MessageInfo();

                        messageInfo.MessageID = messageID;

                        messageInfo.Message = reader["Message"].ToString().Trim();

                        messageInfo.PostedDate = Convert.ToDateTime(reader["Posted"]);

                    }

                }

                finally

                {

                    // Always call Close when done reading.

                    reader.Close();

                }

            }

           

            return messageInfo;

        }

 

 

承载和测试WCF 服务

WCF 服务可以部署在IIS 6.0Windows Activation Service (WAS) with vista IIS 7.0。本示例中,我们采用IIS 6.0部署WCF 服务。编译、运行WCF 示例程序,从IE浏览器显示的文件列表,选择WCFForumService.svc 文件。


 

如果没有客户端程序,你不能直接测试WCF 服务,就像web service 一样。

Microsoft 提供了一个简单的方法来测试WCF 服务 – WcfTestClient 工具(关于该工具的具体介绍,请参考文章:http://forum.entlib.com/Default.aspx?g=posts&t=233 )。如果WCF 服务承载在WCF Service Host (WcfSvcHost.exe) WcfTestClient 工具将自动打开并运行。你也可以通过命令行来运行WcfTestClient 工具,脚本如下:

 

C :/> WcfTestClient http://localhost:4691/WCFForumService.svc

 

http://localhost:4691/WCFForumService.svc 是等待测试的WCF 服务地址。WcfTestClient 在左侧面板显示暴露的方法,同时在右上侧面板也可以输入参数。当输入参数后,点击Invoke 按钮,将调用指定的WCF 方法,在右下侧面板显示结果。


 

抱歉!评论已关闭.