国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SOADemo

2019-11-14 16:24:22
字體:
來源:轉載
供稿:網友

使用SOA來實現兩個數字的相加,不包含驗證,僅供練習使用。

 PDF文檔下載地址:http://files.VEVb.com/chenyongblog/SOA_Demo.pdf

 源碼下載:http://files.VEVb.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();        }    }}

程序運行:

 

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孟津县| 武功县| 清苑县| 苍南县| 余江县| 辽宁省| 黔西县| 临西县| 台北县| 南京市| 安仁县| 甘德县| 阿坝县| 乐至县| 九台市| 阳城县| 康乐县| 东方市| 浮梁县| 阿荣旗| 师宗县| 邻水| 安徽省| 城市| 衡阳市| 东丽区| 萍乡市| 纳雍县| 合作市| 阿勒泰市| 长岛县| 丰顺县| 镇远县| 洞头县| 许昌市| 凭祥市| 忻城县| 多伦县| 徐水县| 宁都县| 六枝特区|