您的位置:首页 > 运维架构

化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)

2007-12-17 11:33 856 查看
[索引页]

[源码下载]

[align=center]化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)[/align]

作者:webabcd

介绍

WCF(Windows Communication Foundation) - 契约(Contract):服务契约(ServiceContract),操作契约(OperationContract),数据契约(DataContract),服务已知类型(ServiceKnownType),数据成员(DataMember)。

示例

1、服务

IPersonManager.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract

Student.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

namespace WCF.ServiceLib.Contract

2、宿主

PersonManager.svc

<?xml version="1.0"?>

<configuration>

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name="ContractBehavior">

<!--httpGetEnabled - 使用get方式提供服务-->

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

</behaviors>

<services>

<!--name - 提供服务的类名-->

<!--behaviorConfiguration - 指定相关的行为配置-->

<service name="WCF.ServiceLib.Contract.PersonManager" behaviorConfiguration="ContractBehavior">

<!--address - 服务地址-->

<!--binding - 通信方式-->

<!--contract - 服务契约-->

<endpoint address="" binding="basicHttpBinding" contract="ConfigurationNameTest" />

</service>

</services>

</system.serviceModel>

</configuration>

3、客户端

PersonManager.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<asp:TextBox ID="txtName" runat="server" Text="webabcd" />

 

<asp:Button ID="btnGetName" runat="server" Text="GetName"

onclick="btnGetName_Click" />

</asp:Content>

PersonManager.aspx.cs

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 Contract_PersonManager : System.Web.UI.Page

Web.config

<?xml version="1.0"?>

<configuration>

<system.serviceModel>

<client>

<!--address - 服务地址-->

<!--binding - 通信方式-->

<!--contract - 服务契约-->

<endpoint address="http://localhost:3502/ServiceHost/Contract/PersonManager.svc" binding="basicHttpBinding" contract="Contract.IPersonManager" />

</client>

</system.serviceModel>

</configuration>

运行结果:

单击"btnGetName"后弹出提示框,显示"Name: webabcd"

OK

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