化零为整WCF(1) - 不能免俗,我也从Hello开始
来源:互联网 发布:国际原油看盘软件 编辑:程序博客网 时间:2024/06/04 01:25
[索引页]
[源码下载]
作者:webabcd
介绍
WCF(Windows Communication Foundation) - 废话不多说,俗也不能免,我也从Hello开始
示例
1、服务
IHello.cs
Hello.cs
2、宿主
Hello.svc
Web.config
3、客户端
Hello.aspx
Hello.aspx.cs
Web.config
运行结果:
单击"btnSayHello"后弹出提示框,显示"Hello: webabcd"
OK
[源码下载]
[源码下载]
化零为整WCF(1) - 不能免俗,我也从Hello开始
作者:webabcd
介绍
WCF(Windows Communication Foundation) - 废话不多说,俗也不能免,我也从Hello开始
示例
1、服务
IHello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Sample
{
/// <summary>
/// IHello接口
/// </summary>
[ServiceContract]
public interface IHello
{
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="name">人名</param>
/// <returns></returns>
[OperationContract]
string SayHello(string name);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Sample
{
/// <summary>
/// IHello接口
/// </summary>
[ServiceContract]
public interface IHello
{
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="name">人名</param>
/// <returns></returns>
[OperationContract]
string SayHello(string name);
}
}
Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Sample
{
/// <summary>
/// Hello类
/// </summary>
public class Hello : IHello
{
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="name">人名</param>
/// <returns></returns>
public string SayHello(string name)
{
return "Hello: " + name;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Sample
{
/// <summary>
/// Hello类
/// </summary>
public class Hello : IHello
{
/// <summary>
/// 打招呼方法
/// </summary>
/// <param name="name">人名</param>
/// <returns></returns>
public string SayHello(string name)
{
return "Hello: " + name;
}
}
}
2、宿主
Hello.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Sample.Hello" %>
Web.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SampleBehavior">
<!--httpGetEnabled - 使用get方式提供服务-->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!--name - 提供服务的类名-->
<!--behaviorConfiguration - 指定相关的行为配置-->
<service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior">
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Sample.IHello" />
</service>
</services>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SampleBehavior">
<!--httpGetEnabled - 使用get方式提供服务-->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!--name - 提供服务的类名-->
<!--behaviorConfiguration - 指定相关的行为配置-->
<service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior">
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Sample.IHello" />
</service>
</services>
</system.serviceModel>
</configuration>
3、客户端
Hello.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs"
Inherits="Sample_Hello" Title="不能免俗,我也从Hello开始" %>
<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="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" />
</asp:Content>
Inherits="Sample_Hello" Title="不能免俗,我也从Hello开始" %>
<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="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" />
</asp:Content>
Hello.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 Sample_Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSayHello_Click(object sender, EventArgs e)
{
Sample.HelloClient proxy = new Sample.HelloClient();
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format("alert('{0}')", proxy.SayHello(txtName.Text)),
true);
proxy.Close();
}
}
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 Sample_Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSayHello_Click(object sender, EventArgs e)
{
Sample.HelloClient proxy = new Sample.HelloClient();
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"js",
string.Format("alert('{0}')", proxy.SayHello(txtName.Text)),
true);
proxy.Close();
}
}
Web.config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="http://localhost:3502/ServiceHost/Sample/Hello.svc" binding="basicHttpBinding" contract="Sample.IHello" />
</client>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<client>
<!--address - 服务地址-->
<!--binding - 通信方式-->
<!--contract - 服务契约-->
<endpoint address="http://localhost:3502/ServiceHost/Sample/Hello.svc" binding="basicHttpBinding" contract="Sample.IHello" />
</client>
</system.serviceModel>
</configuration>
运行结果:
单击"btnSayHello"后弹出提示框,显示"Hello: webabcd"
OK
[源码下载]
- 化零为整WCF(1) - 不能免俗,我也从Hello开始
- 不能免俗,开始学C#,发现一个msdn的一个问题?
- 从今天开始我也在这里“博”一下了
- 从今天开始我也要学习敏捷了。。。
- Google Gadgets (1):从 Hello World 开始
- No.1 Spring MVC 从hello开始
- 从Hello WPF开始
- 从Hello World开始
- 从hello world开始
- 从hello world 开始
- 从hello world开始
- 化零为整WCF系列文章索引
- 化零为整WCF(17) - 安全(Security)
- 化零为整WCF(14) - 事务(Transaction)
- 化零为整WCF(14) - 事务(Transaction)
- 化零为整WCF(14) - 事务(Transaction)
- 化零为整WCF(17) - 安全(Security)
- 化零为整WCF系列文章索引
- 化零为整WCF(4) - 异常处理(Exception、FaultException、FaultException<T>、IErrorHandler)
- 扩展Label控件(1) - 实现回发(Postback)功能
- 扩展DropDownList控件和ListBox控件(1) - 支持分组功能(optgroup标签)
- 化零为整WCF(3) - 绑定Binding(basicHttpBinding和netTcpBinding)
- 化零为整WCF(2) - 契约Contract(ServiceContract、OperationContract、DataContract、ServiceKnownType和DataMember)
- 化零为整WCF(1) - 不能免俗,我也从Hello开始
- 积少成多Flash ActionScript 3.0(3) - 基础之以文本形式、XML形式和JSON形式与ASP.NET通信
- 积少成多Flash ActionScript 3.0(2) - 基础之包、类、包外类、命名空间、属性、方法、接口和继承
- 积少成多Flash ActionScript 3.0(1) - 基础之数据类型、操作符和流程控制语句
- Oracle分析函数详述
- 身份证验证算法(SQL版)
- 步步为营VS 2008 + .NET 3.5系列文章索引
- 步步为营VS 2008 + .NET 3.5(14) - XLINQ(LINQ to XML)之针对XML文件的添加、查询、更新和删除
- 步步为营VS 2008 + .NET 3.5(13) - DLINQ(LINQ to SQL)之用户自定义函数、在不同的DataContext之间做更新、缓存、获取信息、数据加载选项和延迟加载