浅析第一个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
- 浅析第一个WCF应用
- 第一个WCF程序
- WCF教程-创建第一个WCF程序
- 【WCF】创建第一个WCF应用程序
- 创建第一个WCF服务
- 创建WCF第一个应用程序
- 我的第一个WCF
- 创建第一个WCF服务
- WCF 第一个服务练习
- 创建第一个WCF程序
- 创建第一个WCF服务
- 学习WCF遇到的第一个困难
- 创建第一个简单WCF服务
- 第一个WCF程序(一)
- WCF教程系列(1)-创建第一个WCF程序
- WCF开发实战系列一:创建第一个WCF服务
- WCF开发实战系列一:创建第一个WCF服务
- WCF开发实战系列一:创建第一个WCF服务
- org.springframework.web.context.ContextLoaderListener
- WPF 中的数据绑定
- TI AM3517 EVM 评估板Flash烧写工具中文解析
- 多级数据级联选择,附省市县级联选择功能   -----   JQUERY特效模板
- java编程中的断言工具类(org.springframework.util.Assert)
- 浅析第一个WCF应用
- 情感分析资源
- WebKit/Blink 如何加载网页资源?
- 关于c的可变参数
- 一个图片页面悬浮的例子
- 如何在cocos2d-x 项目中增加全局变量
- c# cpu使用率查询
- PE格式之 区块表与区块
- dos自动关机 at命令及shutdown命令