1.简单的WCF模型

1.结构

2.契约:

 

using System.ServiceModel;
using Rhythmk.Entity;
namespace Rhythmk.Contracts
{
[ServiceContract(Namespace
= " http://wwww.wangkun.com " )]

public interface ICalculate
{
[OperationContract(Name
= " AddServices " )]
double Add( double x, double y);

/*
Name :则体现在客户端调用此方法时候 显示的真实方法名
IsOneway:这意味着客户端仅仅是向服务端发送一个运算的请求
并不会通过回复消息得到任何运算结果。

*/
[OperationContract(Name
= " AddStrServices " )]
string Add( string x, string y);


}
}

3. 服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Rhythmk.Contracts;
using Rhythmk.Entity;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace Rhythmk.Services
{
/// <summary>
/// 计算器
/// </summary>
///
/*
通过
ADO.NET 数据服务[.svc] 建立服务宿主的时候 需要引用
*using System.ServiceModel.Activation;
*同时在服务类前面需要配置
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
*/
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Required)]
public class Calculate:ICalculate
{
public double Add( double x, double y)
{

return x + y;
}

public string Add( string x, string y)
{
return " Result " + x + y;
}

}
}

4.控制台进行寄宿

<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< behaviors >
< serviceBehaviors >
< behavior name ="metaBehavior" >
< serviceMetadata httpGetEnabled ="true" />
</ behavior >
</ serviceBehaviors >
</ behaviors >
< services >
< service name ="Rhythmk.Services.Calculate" behaviorConfiguration ="metaBehavior" >
< endpoint address =""
binding
="basicHttpBinding" bindingConfiguration ="" contract ="Rhythmk.Contracts.ICalculate" >
</ endpoint >
< host >
< baseAddresses >
< add baseAddress ="http://127.0.0.1:1234/Rhythmk.Services.Calculate" />
</ baseAddresses >
</ host >
</ service >
</ services >
</ system.serviceModel >
</ configuration >

代码:

代码:
using System;
using System.ServiceModel;

using Rhythmk.Services;

namespace Rhythmk.ConsoleApp
{
class Program
{
static void Main( string [] args)
{
CreateCalculateService();
}
static void CreateCalculateService()
{
Console.WriteLine(
" ----------CreateCalculateService---Star--------- " );
using (ServiceHost host = new ServiceHost( typeof (Calculate)))
{
host.Opened
+= delegate { Console.WriteLine( " CalculateService已经启动,按任意键终止服务! " ); };
host.Open();
Console.Read();
}

}
}
}

5.ADO.NET 数据服务[.svc] 建立服务宿主

Rhythmk.WebHostMonitor\Calculate\Calculate.svc

<%@ ServiceHost Language="C#"  Debug="true" Service="Rhythmk.Services.Calculate" %>

6.客户端调用

<? xml version="1.0" encoding="utf-8" ?>
< configuration >
< system.serviceModel >
< client >
< endpoint address ="http://wcf.wangkun.w/Calculate/Calculate.svc"
binding
="basicHttpBinding" contract ="Rhythmk.Contracts.ICalculate"
name
="CalculateEndPoint" >
</ endpoint >
</ client >
</ system.serviceModel >
</ configuration >

代码:

using Rhythmk.Contracts;
using System.ServiceModel;
using System.Web.Services;

ChannelFactory
< ICalculate > calculatorChannelFactory = new ChannelFactory < ICalculate > ( " CalculateEndPoint " );
ICalculate proxy
= calculatorChannelFactory.CreateChannel();
Utitl.Alert(proxy.Add(
10 , 20 ).ToString());

转载于:https://www.cnblogs.com/rhythmK/archive/2011/05/25/2056610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值