[ServiceContract] public interface IPeopleInfo { [OperationContract] int GetAge(string name); }
數據契約:瀏覽器
[DataContract] public class People { public string Name; public int Age; }
服務實現:服務器
public class PeopleInfo : IPeopleInfo { public int GetAge(string name) { List<People> peopleList = DataFactory.GetPeopleList(); return peopleList.Find(t =>t.Name==name).Age ; } }
輔助類:tcp
public class DataFactory { public static List<People> GetPeopleList() { List<People> peopleList = new List<People>(); peopleList.AddRange(new People[] { new People{Name="tjm",Age=18}, new People{Name="lw",Age=20}, new People{Name="tj",Age=22}, }); return peopleList; } }
WCF部署IIS出現「因爲擴展配置問題而沒法提供您請求的頁面。若是該頁面是腳本,請添加處理程序。若是應下載文件,請添加 MIME 映射」的解決辦法
管理員身份運行C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i工具
HTTP 錯誤 404.3 - Not Found,
使用管理員註冊:C:\Windows\system32>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\ServiceModelReg.exe" -r
Microsoft(R) WCF/WF 註冊工具版本 4.5.0.0學習
版權全部(C) Microsoft Corporation。保留全部權利。網站
用於管理一臺計算機上 WCF 和 WF 組件的安裝和卸載的管理實用工具。this
[錯誤]此 Windows 版本不支持此工具。管理員應改成使用「打開或關閉 Windows 功能」對話框或 dism 命令行工具來安裝/卸載 Windows Communication Foundation 功能。spa
把我畫紅線的框內的複選框所有勾選,點擊肯定,而後再在iis中再進行瀏覽就可以找到發佈後的WCF服務了,緣由應該是我沒有安裝WCF服務的組件而致使的吧。在瀏覽器中瀏覽服務以下。命令行
在window功能中卸載iis和WCF服務,而後再從新安裝配置。
首先創建一個Winform項目,界面以下。設計
注意:在部署以前須要把第一步中編寫服務的dll引用進來。
ServiceHost host; private void btnStart_Click(object sender, EventArgs e) { //使用代碼綁定 Uri tcpa = new Uri("net.tcp://172.21.212.54:8090/peopleinfo"); host = new ServiceHost(typeof(FirstWCFService.PeopleInfo), tcpa); ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); NetTcpBinding tcpb = new NetTcpBinding(); host.Description.Behaviors.Add(mBehave); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex"); host.AddServiceEndpoint(typeof(FirstWCFService.IPeopleInfo), tcpb, tcpa); host.Open();
this.btnStart.Enabled = false;
this.label1.Text = "Service Running"; } private void btnStop_Click(object sender, EventArgs e) { if (host != null) { host.Close(); this.label1.Text = "Service Closed"; this.btnStart.Enabled = true; } }
2)使用配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="FirstWCFService.PeopleInfo"> <!--客戶端用來解釋服務,這個端點若是缺乏服務會發布失敗--> <endpoint contract="IMetadataExchange" binding="mexTcpBinding" address ="net.tcp://172.21.212.54:8090/peoleinfo/mex"></endpoint> <!--提供服務的端點--> <endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding" contract="FirstWCFService.IPeopleInfo"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
雖然是用來配置文件,可是開啓服務的代碼仍是須要寫的。代碼以下。
ServiceHost host; private void btnStart_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(FirstWCFService.PeopleInfo)); host.Open(); this.btnStart.Enabled = false; this.label1.Text = "Service Running"; } private void btnStop_Click(object sender, EventArgs e) { if (host != null) { host.Close(); this.label1.Text = "Service Closed"; this.btnStart.Enabled = true; } }
運行程序,點擊Start按鈕,到此使用Winform做爲宿主來發布WCF服務已經成功了。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IPeopleInfo" /> </basicHttpBinding> <netTcpBinding> <binding name="NetTcpBinding_IPeopleInfo"> </binding> </netTcpBinding> </bindings> <client> <endpoint address="http://172.21.212.54/PeopleInfo.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPeopleInfo" contract="HttpService.IPeopleInfo" name="BasicHttpBinding_IPeopleInfo" /> <endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IPeopleInfo" contract="TcpService.IPeopleInfo" name="NetTcpBinding_IPeopleInfo" /> </client> </system.serviceModel> </configuration>
調用服務操做的代碼很是簡單,就相似於操做本地的類同樣,代碼以下:
private void bntGet_Click(object sender, EventArgs e) { string name = this.txtName.Text; int age; if (rdbHttp.Checked) { HttpService.PeopleInfoClient httpClient = new HttpService.PeopleInfoClient(); age = httpClient.GetAge(name); } else { TcpService.PeopleInfoClient tcpClient = new TcpService.PeopleInfoClient(); age = tcpClient.GetAge(name); } this.txtAge.Text = age.ToString(); }
到此,wcf的客戶端編寫完成,結果以下。
System.ServiceModel.Security.SecurityNegotiationException: 服務器已拒絕客戶端憑據。 --->
System.Security.Authentication.InvalidCredentialException: 服務器已拒絕客戶端憑據。 --->
System.ComponentModel.Win32Exception: 登陸沒有成功
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="FirstWCFService.PeopleInfo"> <!--客戶端用來解釋服務,這個端點若是缺乏服務會發布失敗--> <endpoint contract="IMetadataExchange" binding="mexTcpBinding" address ="net.tcp://172.21.212.54:8090/peoleinfo/mex"></endpoint> <!--提供服務的端點--> <endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding" contract="FirstWCFService.IPeopleInfo" bindingConfiguration="NoSecurity"> </endpoint> </service> </services> <bindings> <netTcpBinding> <binding name="NoSecurity"> <security mode="None"/> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
設置完成以後,從新開啓服務。而後在客戶端刪除winform宿主發佈的服務,而且再從新添加,以便在配置文件中從新生成訪問tcp方式的服務端點的配置。更新後的配置文件以下,紅色加粗部分爲更新的。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IPeopleInfo" /> </basicHttpBinding> <netTcpBinding> <binding name="NetTcpBinding_IPeopleInfo"> <security mode="None" /> </binding> </netTcpBinding> </bindings> <client> <endpoint address="http://172.21.212.54/PeopleInfo.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPeopleInfo" contract="HttpService.IPeopleInfo" name="BasicHttpBinding_IPeopleInfo" /> <endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IPeopleInfo" contract="TcpService.IPeopleInfo" name="NetTcpBinding_IPeopleInfo" /> </client> </system.serviceModel> </configuration>