VS2010Webservice项目开发实例
来源:互联网 发布:ipad air2蜂窝数据 编辑:程序博客网 时间:2024/06/05 19:42
因为和VS2005存在很大差异,本文主要讲述一个简单的VS2010开发和测试Webservice项目.
主要流程为:
1.打开VS2010
2.新建立C#空白解决方案
3.添加新项目,选择左侧"已安装的模板"--"Visual C#"--"WCF",选择中间的"WCF 服务应用程序"--输入项目名称"WcfService1".
新建完成后,打开项目中的Service1.svc文件,查看代码,在末尾添加两个函数.HelloWorld()和Add( int a, int b).代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public String HelloWorld()
{
return " Hello, world! " ;
}
public int Add( int a, int b)
{
return a + b;
}
}
}
4.添加新项目,选择左侧"已安装的模板"--"Visual C#"--"Windows 窗体应用程序",选择中间的"Windows 窗体应用程序"--输入项目名称"TestService".新建完成后,双击项目中的Form1.cs文件,在界面中拖入一个menuStrip1菜单控件,加入"menuStrip1",依次输入"WebService方法调用","HelloWorld","Add",分别双击"HelloWorld"和"Add"菜单文字.产生点击事件.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void helloWorldToolStripMenuItem_Click(object sender, EventArgs e)
{
WcfService1.Service1 client = new WcfService1.Service1();
// 使用 "client" 变量在服务上调用操作。
// 始终关闭客户端。
this.richTextBox1.Text = client.HelloWorld();
}
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
WcfService1.Service1 client = new WcfService1.Service1();
// 使用 "client" 变量在服务上调用操作。
// 始终关闭客户端。
int result = client.Add(1, 2);
this.richTextBox1.Text = Convert.ToString(result);
}
}
}
5.选中TestService项目,点击鼠标右键,选择"设置为启动项目",添加项目引用选择WcfService1后按F5运行,点击"HelloWorld"和"Add"菜单即可看见运行效果.
项目源码到我的资源中下载WcfService1.rar
- VS2010Webservice项目开发实例
- 项目管理开发实例
- iOS项目开发实例
- PHP专业项目实例开发
- vue开发app项目实例
- 软件项目开发之实例类比
- java项目开发实例自学手册 下载
- 手机开发项目的具体实例
- JDBC的项目开发实例<轻量级封装>
- Java开发项目实例中的技术总结
- Linux上用Eclipse CDT开发c++项目的实例
- Android编程典型实例与项目开发案例
- SSH+ExtJs4开发项目的Demo实例视频讲解
- Struts2+Spring2+Hibernate3开发环境搭建 项目实例
- 使用ionic开发移动端项目学习实例(1)
- 使用ionic开发移动端项目学习实例(2)
- Android经典项目开发之天气APP实例分享
- Android Java开发实例项目+游戏视频教程免费下载咯。。
- 实现类似google搜索效果,文本框输入智能提示,(经过改进 支持多个文本框的效果)
- Android mp3 lyric 滚动显示 Demo
- Debian6.0安装后系统基本配置
- Objective-C利用协议实现回调函数(类似java的回调函数)
- 卡尔虎王 拥有梦想!!
- VS2010Webservice项目开发实例
- iphone实现文件的读写操作
- PAPI 介绍
- winlogon.exe错误:小心设置搜狗拼音输入法
- voip知识手册
- iPhone 应用中直接连接到特定的app store应用页面
- c#操作XML文件的方法
- (转)如何解决VC6.0中win32 error267错误
- 在UINavigationController的导航栏中添加UISegmentedControl