本文參考:http://log.medcl.net/item/2010/03/wcf-extensions-under-iis7-configuration-issues/
在學習IIS服務寄宿時參考了文章:如何在IIS中承載WCF服務http://msdn.microsoft.com/zh-cn/library/ms733766.aspx,並按照上面的教程進行配置,
PS:2011-5-28
不按上面的教程進行配置也是可以的,下面我講一下我的配置過程。
步驟1:首先建立一個解決方案叫做IISHostWCF。
步驟2:在這個解決方案中建立一個”控制台應用程式“Client來測試我們通過IIS寄宿的服務,然後在建立一個CalcService的“WCF服務庫“
步驟3:在建立好WCF服務庫以後他會自動協助我們建立契約IService1.cs(介面)以及契約實現Service1.cs
我們在WCF服務哭下建立一個檔案夾叫做App_Code,然後將IService1.cs和Service1.cs都放到這個App_Code檔案夾中去。
步驟4:將自動產生的設定檔App.config改名為Web.config(這一步很重要,不然會報錯說找不到ServiceHost指定的路徑)
步驟5:其他內容可以參照前面。
最後通過http://localhost/IISHostedCalc/Service.svc訪問時報錯,錯誤內容為:“由於擴充配置問題而無法提供您請求的頁面。如果該頁面是指令碼,請添加處理常式。如果應下載檔案,請添加 MIME 對應。”
解決方案:
以管理員運行命令:
C:/Windows/Microsoft.NET/Framework/v3.0/Windows Communication Foundation/ServiceModelReg.exe -i
運行結果如所示:
再次登入 http://localhost/IISHostedCalc/Service.svc,運行經過如所示
表明服務沒有發布成功,按照提示進行修改,修改以後的Web.config為:
<?xml version="1.0" encoding="utf-8" ?><br /><configuration><br /> <system.serviceModel><br /> <behaviors><br /> <serviceBehaviors><br /> <behavior name="MyServiceTypeBehaviors" ><br /> <serviceMetadata httpGetEnabled="true" /><br /> </behavior><br /> </serviceBehaviors><br /> </behaviors><br /> <services><br /> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="MyServiceTypeBehaviors" ><br /> <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc --><br /> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /><br /> </service><br /> </services><br /> </system.serviceModel><br /></configuration>
再次登入http://localhost/IISHostedCalc/Service.svc,會出現如下介面
SvcUtil.exe應用
可以參考ServiceModel 中繼資料工具 + 生產力 (Svcutil.exe)
我們可以在C:/Program Files/Microsoft SDKs/Windows/v6.0A/bin目錄下找到SvcUtil.exe,將這個程式拷貝到D盤根目錄下,然後開啟cmd,切換到d盤根目錄下,輸入命令:
svcutil.exe http://xuwei/IISHostedCalc/service.svc?wsdl
運行完以後,在d盤根目錄下就會自動產生CalculatorService.cs和output.config檔案。
在CalculatorService.cs中定義了CalculatorClient這個類,我們在用戶端應用程式CalculatorServiceClient中就可以直接通過Calculator這個類建立調用服務的用戶端對象。
CalculatorService.cs:
//------------------------------------------------------------------------------<br />// <auto-generated><br />// 此代碼由工具產生。<br />// 運行時版本:2.0.50727.4952<br />//<br />// 對此檔案的更改可能會導致不正確的行為,並且如果<br />// 重建代碼,這些更改將會丟失。<br />// </auto-generated><br />//------------------------------------------------------------------------------</p><p>[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]<br />[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ICalculator")]<br />public interface ICalculator<br />{</p><p> [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Add", ReplyAction="http://tempuri.org/ICalculator/AddResponse")]<br /> double Add(double n1, double n2);</p><p> [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Subtract", ReplyAction="http://tempuri.org/ICalculator/SubtractResponse")]<br /> double Subtract(double n1, double n2);</p><p> [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Multiply", ReplyAction="http://tempuri.org/ICalculator/MultiplyResponse")]<br /> double Multiply(double n1, double n2);</p><p> [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculator/Divide", ReplyAction="http://tempuri.org/ICalculator/DivideResponse")]<br /> double Divide(double n1, double n2);<br />}<br />[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]<br />public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel<br />{<br />}<br />[System.Diagnostics.DebuggerStepThroughAttribute()]<br />[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]<br />public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator<br />{</p><p> public CalculatorClient()<br /> {<br /> }</p><p> public CalculatorClient(string endpointConfigurationName) :<br /> base(endpointConfigurationName)<br /> {<br /> }</p><p> public CalculatorClient(string endpointConfigurationName, string remoteAddress) :<br /> base(endpointConfigurationName, remoteAddress)<br /> {<br /> }</p><p> public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :<br /> base(endpointConfigurationName, remoteAddress)<br /> {<br /> }</p><p> public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :<br /> base(binding, remoteAddress)<br /> {<br /> }</p><p> public double Add(double n1, double n2)<br /> {<br /> return base.Channel.Add(n1, n2);<br /> }</p><p> public double Subtract(double n1, double n2)<br /> {<br /> return base.Channel.Subtract(n1, n2);<br /> }</p><p> public double Multiply(double n1, double n2)<br /> {<br /> return base.Channel.Multiply(n1, n2);<br /> }</p><p> public double Divide(double n1, double n2)<br /> {<br /> return base.Channel.Divide(n1, n2);<br /> }<br />}<br />
out.config
<?xml version="1.0" encoding="utf-8"?><br /><configuration><br /> <system.serviceModel><br /> <bindings><br /> <wsHttpBinding><br /> <binding name="WSHttpBinding_ICalculator" closeTimeout="00:01:00"<br /> openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"<br /> bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"<br /> maxBufferPoolSize="524288" maxReceivedMessageSize="65536"<br /> messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"<br /> allowCookies="false"><br /> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"<br /> maxBytesPerRead="4096" maxNameTableCharCount="16384" /><br /> <reliableSession ordered="true" inactivityTimeout="00:10:00"<br /> enabled="false" /><br /> <security mode="Message"><br /> <transport clientCredentialType="Windows" proxyCredentialType="None"<br /> realm=""><br /> <extendedProtectionPolicy policyEnforcement="Never" /><br /> </transport><br /> <message clientCredentialType="Windows" negotiateServiceCredential="true"<br /> algorithmSuite="Default" establishSecurityContext="true" /><br /> </security><br /> </binding><br /> </wsHttpBinding><br /> </bindings><br /> <client><br /> <endpoint address="http://xuwei/IISHostedCalc/service.svc" binding="wsHttpBinding"<br /> bindingConfiguration="WSHttpBinding_ICalculator" contract="ICalculator"<br /> name="WSHttpBinding_ICalculator"><br /> <identity><br /> <servicePrincipalName value="host/xuwei" /><br /> </identity><br /> </endpoint><br /> </client><br /> </system.serviceModel><br /></configuration>
建立測試用戶端
將CalculatorService.cs和out.config拷貝到CalculatorServiceClient項目下。然後添加現有項CalculatorService,添加完以後再添加應用程式設定檔App.config,將out.config中的內容拷貝到App.config中去,Program.cs代碼如下:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />namespace CalculatorServiceClient<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> CalculatorClient client = new CalculatorClient();<br /> // 使用 "client" 變數在服務上叫用作業。<br /> Console.WriteLine("x+y={2} when x={0} and y={1}", 1, 2, client.Add(1, 2));<br /> Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, client.Subtract(1, 2));<br /> Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, client.Multiply(1, 2));<br /> Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, client.Divide(1, 2));<br /> Console.Read();<br /> // 始終關閉用戶端。<br /> client.Close();<br /> }<br /> }<br />}<br />
運行這個CalculatorServiceClient控制台應用程式,運行結果如下: