您的位置:首页 > 其它

一个简单的wcf实现

2012-06-18 21:32 211 查看
打开vs2010,新建项目如图



添加新建项目





添加服务引用





添加新建项webform.aspx

<div>
<p><asp:TextBox ID="chuancan" runat="server"></asp:TextBox><asp:Button ID="btn_ok"
runat="server" Text="提交" onclick="btn_ok_Click" /></p>
<p>
<asp:Label ID="showtext" runat="server" Text=""></asp:Label></p>
</div>


  webform.aspx.cs

protected void btn_ok_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
showtext.Text = client.HelloWorld(chuancan.Text.Trim().ToString());
}


  

wcf项目里的IService1.cs

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
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: 在此添加您的服务操作

[OperationContract]
string HelloWorld(string user);//自己添加的一个方法
}

// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[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; }
}
}
}


  Services1.svc.cs

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(string user)
{
return "你好," + user + "!";
}
}
}


  以上就是我的第一个wcf应用程序



点此下载相关代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐