日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UI层调用WCF服务实例(源码)

發布時間:2025/3/13 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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服务实例(源码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。