UI层调用WCF服务实例(源码)
生活随笔
收集整理的這篇文章主要介紹了
UI层调用WCF服务实例(源码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
WCF原理性的東西,暫時還沒有深入研究,只是在公司的項目中使用到了,會調用,然后再多做了一些了解,現在將它抽出來了一個小實例,寫了一個WCF的demo。
我寫的這個WCF.Demo主要包括數據契約和服務契約,UI客戶端層和Host宿主層,基于http和net.tcp兩種協議通信。
不多說,直接貼一張層次圖片先,最后提供源碼下載。
?
數據契約層(DataContracts)代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks;namespace WCF.DataContracts {[DataContract()]public class CustomerData{[DataMember()]public string Name { set; get; }[DataMember()]public string Sex { set; get; }} }
?
服務契約接口層(WCF.ServiceContracts)
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks;namespace WCF.ServiceContracts {[ServiceContract()]public interface ICustomerService{[OperationContract()]string ShowInfo();} }服務契約實現層(WCF.Services)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WCF.DataContracts; using WCF.ServiceContracts;namespace WCF.Services {public class CustomerService : ICustomerService{public string ShowInfo(){CustomerData data = new CustomerData(){Name = "admin",Sex = "男"};return "姓名是:" + data.Name + ",性別是:" + data.Sex;}} }宿主層(兩個文件)
Program
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using WCF.Services;namespace WCF.Hosting {class Program{static void Main(string[] args){ServiceHost hostForHello = new ServiceHost(typeof(CustomerService));hostForHello.Open();Console.WriteLine("WCF服務啟動成功!");Console.ReadLine();}} }app.config
<?xml version="1.0"?><configuration><system.serviceModel><services><service name="WCF.Services.CustomerService" behaviorConfiguration="mex"><host><baseAddresses><add baseAddress="http://192.168.1.100:64566/CustomerService"/><add baseAddress="net.tcp://192.168.1.100:64567/CustomerService"/></baseAddresses></host><endpoint binding="wsDualHttpBinding" contract="WCF.ServiceContracts.ICustomerService" address="mex" /> <endpoint address="net.tcp://192.168.1.100:64567/CustomerService" binding="netTcpBinding"bindingConfiguration="tcpWindowsSecurity" name="helloEndPoint"contract="WCF.ServiceContracts.ICustomerService"/><endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /></service></services><bindings><netTcpBinding><binding name="tcpWindowsSecurity"></binding></netTcpBinding></bindings><behaviors><serviceBehaviors><behavior name="mex"><serviceMetadata /></behavior></serviceBehaviors></behaviors></system.serviceModel><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>客戶端調用層(WCF.Client)
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using WCF.ServiceContracts;namespace WCF.Client {public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){using (ChannelFactory<ICustomerService> channelFactory = new ChannelFactory<ICustomerService>("helloEndPoint")){ICustomerService helloService = channelFactory.CreateChannel();using (helloService as IDisposable){TextBox1.Text = helloService.ShowInfo();}}}} }客戶端配置文件
<?xml version="1.0" encoding="utf-8"?><!--有關如何配置 ASP.NET 應用程序的詳細信息,請訪問http://go.microsoft.com/fwlink/?LinkId=169433--><configuration><system.serviceModel><bindings><netTcpBinding><binding name="tcpWindowsSecurity"></binding></netTcpBinding></bindings><client><endpoint name="helloEndPoint" address="net.tcp://192.168.1.100:64567/CustomerService"binding="netTcpBinding" bindingConfiguration="tcpWindowsSecurity"contract="WCF.ServiceContracts.ICustomerService" /></client></system.serviceModel><system.web><compilation debug="true" targetFramework="4.5" /><httpRuntime targetFramework="4.5" /></system.web></configuration>源碼下載
轉載于:https://www.cnblogs.com/renzaijianghu/p/3493264.html
總結
以上是生活随笔為你收集整理的UI层调用WCF服务实例(源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocos2dx打飞机项目笔记五:CCS
- 下一篇: Flash的层叠顺序问题(z-index