浅析第一个WCF应用

来源:互联网 发布:js 判断有没有滚动条 编辑:程序博客网 时间:2024/05/18 05:22

首先我们需要创建一个WCF服务应用程序。

包含一个IService接口

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;namespace FirstWCF{    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。    [ServiceContract]//服务契约,指定了操作文件继承接口后可以执行的操作。    public interface IService1    {        //Request/Reply模式消息传递,当该方法被调用时,需要登录服务器的响应才能得到服务器返回的值        [OperationContract]//方法声明        string GetData(int value);  //定义方法        [OperationContract]        CompositeType GetDataUsingDataContract(CompositeType composite);        // TODO: 在此添加您的服务操作        [OperationContract]        DateTime PaseDate();        //OneWay消息模式传递,此时方法必须是Void方法,不能使用ref或者out参数;当该方法被调用后会立即返回值。        [OperationContract(IsOneWay = true)]        void OutPutString(string val);    }    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。    [DataContract]//数据契约,指定了客户端与服务端交互的数据类型。    public class CompositeType    {        bool boolValue = true;        string stringValue = "Hello ";        [DataMember]        public bool BoolValue        {            get { return boolValue; }            set { boolValue = value; }        }        [DataMember]        public string StringValue        {            get { return stringValue; }            set { stringValue = value; }        }    }}

一个Service.svc文件

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;using System.IO;namespace FirstWCF{    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。    public class Service1 : IService1//继承IService接口,实现接口中的方法    {        public string GetData(int value)        {            return string.Format("You entered: {0}", value);        }        public DateTime PaseDate()        {            return DateTime.Now;        }        public void OutPutString(string val)        {            Console.WriteLine(val);        }        public CompositeType GetDataUsingDataContract(CompositeType composite)        {            if (composite == null)            {                throw new ArgumentNullException("composite");            }            if (composite.BoolValue)            {                composite.StringValue += "Suffix";            }            return composite;        }    }}
web.config中也会添加相应的节点

<?xml version="1.0" encoding="utf-8"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0" />  </system.web>  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior>          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->          <serviceMetadata httpGetEnabled="true"/>          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->          <serviceDebug includeExceptionDetailInFaults="false"/>        </behavior>      </serviceBehaviors>    </behaviors>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  </system.serviceModel> <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>  </system.webServer>  </configuration>
创建完成后F5进入调试,

请求内容:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Header>    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetData</Action>  </s:Header>  <s:Body>    <GetData xmlns="http://tempuri.org/">      <value>111</value>    </GetData>  </s:Body></s:Envelope>

响应内容:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Header />  <s:Body>    <GetDataResponse xmlns="http://tempuri.org/">      <GetDataResult>You entered: 111</GetDataResult>    </GetDataResponse>  </s:Body></s:Envelope>
从消息内容中我们可以看出,在Action节点中,使用了GetData方法,再编程中可以通过使用OperationContract.Action捕获相应的Action消息。

        //通过action和replyaction来捕获相应的消息        [OperationContract(Action = "GetData", ReplyAction = "GetData")]        Message ProcessMessage(Message m);

      public Message ProcessMessage(Message m)        {            CompositeType t = m.GetBody<CompositeType>();            Console.WriteLine(t.StringValue);            return Message.CreateMessage(MessageVersion.Soap11, "Add", new CompositeType());        }




0 0
原创粉丝点击