使用SOA来实现两个数字的相加,不包含验证,仅供练习使用。

PDF文档下载地址:http://files.cnblogs.com/chenyongblog/SOA_Demo.pdf

源码下载:http://files.cnblogs.com/chenyongblog/WCFTest.7z

1、首先定义一个接口ICalculate

(1)引入System.ServiceModel程序集

(2)公开接口,使用ServiceContract特性定义服务契约(标注interface),OpeattionContract特性标注Method

using System.ServiceModel;

namespace CalculateImplement
{
[ServiceContract]
public interface ICalculate
{
[OperationContract]
double Add(double x, double y);
}
}

2、接口的实现

namespace CalculateImplement
{
public class Calculate : ICalculate
{
public double Add(double x, double y)
{
return x + y;
}
}
}

3、Host管理服务

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> <!--WCF Setting-->
<system.serviceModel>
<services>
<service name="CalculateImplement.Calculate" behaviorConfiguration="serviceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001"/>
</baseAddresses>
</host>
<endpoint name="CalculateImplementEndPoint"
address="CalculateImplement"
binding="basicHttpBinding"
contract="CalculateImplement.ICalculate"/>
<endpoint name="mex"
binding ="mexHttpBinding"
contract="IMetadataExchange"
address="mex"/>
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
using System;
using System.ServiceModel;
using CalculateImplement; namespace HostService
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Calculate)); try
{
host.Open();
Console.WriteLine("Service is open......");
Console.ReadLine();
host.Close();
}
catch (Exception et)
{ throw et;
}
}
}
}

4、开启Host,在Client端添加Service,修改命名空间

Client代码:

using System;

namespace Client
{
class Program
{
static void Main(string[] args)
{
CalculateService.CalculateClient calculate = new CalculateService.CalculateClient();
Console.WriteLine("SOA Demo");
Console.Write("Please enter the first number:");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter the second number:");
double num2 = Convert.ToDouble(Console.ReadLine());
double result = calculate.Add(num1, num2);
Console.WriteLine("Add result:" + result);
Console.ReadLine();
}
}
}

程序运行:

最新文章

  1. AVAssetReader+AVAssetReaderTrackOutput播放视频
  2. 解决idea中执行maven命令失败的问题
  3. think in java学习笔记
  4. 8.Fluent API in Code-First【Code-First系列】
  5. C# Label显示多行文本及换行(WinForm/WebForm)
  6. [c#基础]集合foreach的必要条件和自定义集合
  7. ARM字节对齐问题详解
  8. C# DataContract DataMember
  9. Tarjan 离线算法LCA
  10. 面向GC的Java编程
  11. HMM模型详解
  12. iOS文件保存策略
  13. cocos2d-lua SDK接入
  14. struts2中的拦截器
  15. Linux 普通用户之间免密登陆
  16. 为什么要使用TypeScript开发Web应用程序
  17. Docker在Linux上运行NetCore系列(一)配置运行DotNetCore控制台
  18. Department and Student
  19. maven的pom.xml文件标签含义
  20. 加快cin读取速度

热门文章

  1. Oracle 基础 游标
  2. VsFTP+本地文件认证+SSl实现虚拟用户配置
  3. linux初学 :linux 常用命令(二)
  4. 虚拟机的MAC地址分配与修改
  5. 关于Java中获取当前系统时间
  6. MAC如何直接管理安卓手机文件(转)
  7. 获取IOS bundle中的文件
  8. 10 Best TV Series Based On Hacking And Technology
  9. 关于在asp.net的web页面中的全局变量问题
  10. Swift字典