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