close

 

  最近公司有一門課教WCF,看似像個webservice

網路上也有查到一些看起來簡單的初步教學網站:

http://vmiv.blogspot.tw/p/wcf.html

 

 以公司範例:

1. 新增專案 >> WCF >> WCF服務程式庫(WCF Service Library) >> 取名為:Calculators

 

 IService1.cs:

namespace Calculators

 

{

 

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.

 

    [ServiceContract]

 

    public interface IComplexCalc

 

    {

 

        [OperationContract]

 

        int Add(int a, int b);

 

 

 

        [OperationContract]

 

        ComplexNumber ComplexAdd(ComplexNumber a, ComplexNumber b);

 

    }

 

    [DataContract]

 

    public class ComplexNumber

 

    {

 

        [DataMember]

 

        public int Real { get; set; }

 

        [DataMember]

 

        public int Imaginary { get; set; }

 

    }

 

}

 

 

Service1.cs:

 

 

namespace Calculators

 

{

 

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.

 

    public class ComplexCalc : IComplexCalc

 

    {

 

 

 

        #region IComplexCalc Members

 

 

 

        public int Add(int a, int b)

 

        {

 

            return a + b;

 

        }

 

 

 

        public ComplexNumber ComplexAdd(ComplexNumber a, ComplexNumber b)

 

        {

 

            return new ComplexNumber()

 

            {

 

                Real = a.Real + b.Real,

 

                Imaginary = a.Imaginary + b.Imaginary

 

            };

 

 

 

        }

 

 

 

        #endregion

 

    }

 

}

 

 

 

    Modify App.config:

 

 

 

<service name="Calculators.ComplexCalc">

 

        <host>

 

          <baseAddresses>

 

            <add baseAddress = "http://localhost:8080/" />

 

          </baseAddresses>

 

        </host>

 

        <!-- Service Endpoints -->

 

        <!-- Unless fully qualified, address is relative to base address supplied above -->

 

        <endpoint address ="" binding="wsHttpBinding" contract="Calculators.IComplexCalc">

 

 

 

 

OK後按F5 則就可以啟動服務,並試著輸入參數測試看看~~~

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()