WCF服务
App_Code/Customer.cs
- C# code
-
using System.Runtime.Serialization; using System.ServiceModel; //实体类 [DataContract] public class Customer { [DataMember] public string FirstName; [DataMember] public string LastName; }
IWCF_WithDataContract.cs
- C# code
-
using System.Runtime.Serialization; using System.ServiceModel; [ServiceContract] public interface IWCF_WithDataContract { [OperationContract] string HelloFirstName(Customer cust); [OperationContract] string HelloFullName(Customer cust); }
- C# code
-
using System.Runtime.Serialization; using System.ServiceModel; public class WCF_WithDataContract : IWCF_WithDataContract { public string HelloFirstName(Customer cust) { return "Hello" + cust.FirstName; } public string HelloFullName(Customer cust) { return "Hello" + cust.FirstName + " " + cust.LastName; } }
客户端asp.net代码
- C# code
-
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class WithDataContractServiceTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnWithData_Click(object sender, EventArgs e) { WithDataContractService.WCF_WithDataContractClient ws = new WithDataContractService.WCF_WithDataContractClient(); WithDataContractService.Customer c = new WithDataContractService.Customer(); c.FirstName = "zhong"; c.LastName = "wei"; ws.HelloFullName(c); ws.Close(); } }
引用的实体要加DataContract标签和DataMember标签