MSDN上面的WCF入门教程练习

本文详细介绍了如何通过WCF创建并运行一个简单的计算器服务。包括服务端与客户端的搭建过程,涉及服务协定定义、服务承载及客户端调用等关键步骤。

http://msdn.microsoft.com/zh-cn/library/ms734712.aspx

照着说明测试了一下:
一、创建解决方案WCFtest
二、创建服务端
1、在WCFtest下添加一个“控制台应用程序”,命名为 Service
2、修改默认的命名空间为 Microsoft.ServiceModel.Samples
3、添加引用System.ServiceModel.dll
4、编写Program.cs代码:
ContractedBlock.gif ExpandedBlockStart.gif View Code

    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(Namespace
= " http://Microsoft.ServiceModel.Samples " )]
public interface ICalculator
{
[OperationContract]
double Add( double n1, double n2);
[OperationContract]
double Subtract( double n1, double n2);
[OperationContract]
double Multiply( double n1, double n2);
[OperationContract]
double Divide( double n1, double n2);
}
public class CalculatorService : ICalculator
{
public double Add( double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine(
" Received Add({0},{1}) " , n1, n2);
// Code added to write output to the console window.
Console.WriteLine( " Return: {0} " , result);
return result;
}
public double Subtract( double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine(
" Received Subtract({0},{1}) " , n1, n2);
Console.WriteLine(
" Return: {0} " , result);
return result;
}
public double Multiply( double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine(
" Received Multiply({0},{1}) " , n1, n2);
Console.WriteLine(
" Return: {0} " , result);
return result;
}
public double Divide( double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine(
" Received Divide({0},{1}) " , n1, n2);
Console.WriteLine(
" Return: {0} " , result);
return result;
}
}
class Program
{
static void Main( string [] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri( " http://localhost:8000/ServiceModelSamples/Service " );
// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost( typeof (CalculatorService), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof (ICalculator),
new WSHttpBinding(),
" CalculatorService " );
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled
= true ;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine(
" The service is ready. " );
Console.WriteLine(
" Press <ENTER> to terminate service. " );
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine(
" An exception occurred: {0} " , ce.Message);
selfHost.Abort();
}
}
}
}
验证服务是否正常运行:
1、按F5启动或者在项目目录下的bin\Debug目录下打开service.exe
2、打开浏览器并输入 http://localhost:8000/ServiceModelSamples/Service
三、创建客户端
1、在WCFtest下添加一个“控制台应用程序”,命名为 Client
2、 添加引用System.ServiceModel.dll
3、打开“Visual Studio 2010 命令提示”,运行ServiceModel 元数据实用工具 (Svcutil.exe) ,在项目Client目录下创建客户端代码和配置文件:
C:\Documents and Settings\lc\My Documents\Visual Studio 2010\Projects\WCFte
st\Client>svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config htt
p://localhost:8000/ServiceModelSamples/service
4、在解决方案资源管理器中点击“显示所有文件”,把隐藏的app.config、generatedProxy.cs右键包括在项目Client中
5、编写Program.cs代码:
ContractedBlock.gif ExpandedBlockStart.gif View Code

     
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Client
{
class Program
{
static void Main( string [] args)
{
// Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();

// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D ;
double value2 = 15.99D ;
double result = client.Add(value1, value2);
Console.WriteLine(
" Add({0},{1}) = {2} " , value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D ;
value2
= 76.54D ;
result
= client.Subtract(value1, value2);
Console.WriteLine(
" Subtract({0},{1}) = {2} " , value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D ;
value2
= 81.25D ;
result
= client.Multiply(value1, value2);
Console.WriteLine(
" Multiply({0},{1}) = {2} " , value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D ;
value2
= 7.00D ;
result
= client.Divide(value1, value2);
Console.WriteLine(
" Divide({0},{1}) = {2} " , value1, value2, result);
// Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine(
" Press <ENTER> to terminate client. " );
Console.ReadLine();
}
}
}
最后,先打开Server\bin\Debug下的Service.exe运行服务端,再打开Client\bin\Debug下的Client.exe运行客户端。
显示如下:
0_1303535883o26O.gif
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值