WCF系列之承載(IIS-HTTP)

來源:互聯網
上載者:User

標籤:

IIS承載

部署到IIS的話,需要啟用WCF Http Activation功能組件。

下面我們來看一個簡單的例子,我們先來看一下項目的目錄結構

 

該例子實現傳入兩個整型的參數,返回它們的和。(是Surface pro4截出來的,可能字型有點大)

定義契約:

實現服務:

 

 再來看一下服務的配置:

到目前為止,那麼服務的定義已經實現了,下面再來看一下怎麼在IIS中承載。

首先,我們把項目編譯,拷貝bin的目錄和svc和設定檔到一個檔案夾下。

 

在IIS中建立一個網站,目錄映射到上面的目錄中,啟動網站。

 

下面驗證一下,是否發布成功,在瀏覽器輸入該網站地址,我們可以看到,列出的目錄有一個svc檔案,我們點擊開啟這個檔案。

 

開啟之後,得到了下面的介面,那麼說明我們服務的承載已經成功。

 

 服務已經發布了,那麼怎麼使用呢,我們繼續下面的操作。

用管理員的方式開啟VS提供的命令列工具,用svcutil.exe產生調用的用戶端代理類和配置。

現在我們來建立一個控制台應用程式,把產生的兩個拷貝到項目下,得到如下的目錄結構。

 

好,項目已經完成,先來看一下產生的程式碼

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")][System.ServiceModel.ServiceContractAttribute(ConfigurationName="ICalculate")]public interface ICalculate{        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculate/Add", ReplyAction="http://tempuri.org/ICalculate/AddResponse")]    int Add(int a, int b);        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculate/Add", ReplyAction="http://tempuri.org/ICalculate/AddResponse")]    System.Threading.Tasks.Task<int> AddAsync(int a, int b);}[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]public interface ICalculateChannel : ICalculate, System.ServiceModel.IClientChannel{}[System.Diagnostics.DebuggerStepThroughAttribute()][System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]public partial class CalculateClient : System.ServiceModel.ClientBase<ICalculate>, ICalculate{        public CalculateClient()    {    }        public CalculateClient(string endpointConfigurationName) :             base(endpointConfigurationName)    {    }        public CalculateClient(string endpointConfigurationName, string remoteAddress) :             base(endpointConfigurationName, remoteAddress)    {    }        public CalculateClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :             base(endpointConfigurationName, remoteAddress)    {    }        public CalculateClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :             base(binding, remoteAddress)    {    }        public int Add(int a, int b)    {        return base.Channel.Add(a, b);    }        public System.Threading.Tasks.Task<int> AddAsync(int a, int b)    {        return base.Channel.AddAsync(a, b);    }}

我們可以看到,同時產生了同步和非同步兩個方法。

 CalculateClient : System.ServiceModel.ClientBase<ICalculate>, ICalculate
服務調用,用了架構的通道調用了遠端服務。

下面再來看一下用戶端的配置

 

我們來關注一下這兩個終結點的配置

<!--提供服務的終結點-->
<endpoint address="WcfService" binding="wsHttpBinding" contract="WcfService.ICalculate" />
<!--提供中繼資料的終結點-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

第一個終結點用於服務提供,第二個用於終結點中繼資料發佈使用,那麼通個這個地址,可以產生用戶端的代理代碼,我們來看一下,把第一個注釋掉看會出現什麼情況。

然後再來看一下,去掉第二個終結點,依然可以正常訪問中繼資料,用了預設提供的終結點。

那麼把serverMetadata的httpGetEnabled設定為false或者直接刪除,那麼會出現這種情況,

 那麼,去掉這個配置,把第二個終結點配置上,可以正常訪問該中繼資料。

但是中繼資料的地址就是這樣的了。

,mex並不是固定的,可以任意命名

 

那麼對應的地址就是:

 

 用戶端的調用與本地代碼調用的方式一致。

 

運行得到的結果:

好到現在就完成了WCF服務在IIS的承載。

WCF系列之承載(IIS-HTTP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.