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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

WCF服务端调用client.

發(fā)布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF服务端调用client. 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

wcf服務端

1,新建一個“windows窗口程序”名稱為WCFServer2。

?

2。然后加入一個“WCF服務”名稱為Service1。

詳細步驟為:解決方式試圖中,選中“WCFServer2”項目,右鍵,在彈出菜單中選擇“加入->新建項”。

3,雙擊主窗口,在它的Load事件中編寫代碼啟動wcf服務:BasicHttpBinding方式啟動wcf服務。此文件代碼例如以下:

using System.ServiceModel;

using System.ServiceModel.Description;

//…

ServiceHost m_ServiceHost;

//…

private void Form1_Load(object sender, EventArgse)

??????? {

??????????? //NetTcpBinding方式啟動wcf服務?

??????????? m_ServiceHost = new ServiceHost(typeof(Service1));//Service1wcf服務的類名稱?

??????????? NetTcpBindingbinding = new NetTcpBinding();

??????????? UribaseAddress = new Uri(string.Format("net.tcp://localhost:10086/WCFHostServer/Service1"));

??????????? m_ServiceHost.AddServiceEndpoint(typeof(IService1),binding, baseAddress);

???????????

??????????? m_ServiceHost.Open();

??????? }

?

4,在?IService1.cs中添加一個方法Init,同一時候添加ISvrToCliCallBack接口.

namespace WCFServer2

{

??? // 注意: 使用重構菜單上的重命名命令,能夠同一時候更改代碼和配置文件里的接口名“IService1”

??? [ServiceContract(CallbackContract= typeof(ISvrToCliCallBack))]

??? public interface IService1

??? {

??????? [OperationContract]

??????? voidDoWork();

?

??????? [OperationContract]

??????? voidInit();

??? }

?

??? public interface ISvrToCliCallBack

??? {

??????? [OperationContract(IsOneWay= true)]

??????? voidNotifyClientMsg(string devStateInfo);

??? }

}

?

5,?在Service1.cs中實現接口的方法Init初始化。

?? public static ISvrToCliCallBack userCallBack;

??????? //...

???????

??????? public void Init()

??????? {?

??????????? userCallBack = OperationContext.Current.GetCallbackChannel<ISvrToCliCallBack>();

??????? }

?

?

6,主窗體添加button。

private voidbutton1_Click(object sender, EventArgs e)

??????? {

??????????? Service1.userCallBack.NotifyClientMsg("服務端給client通知啦");?

??????? }

7。改動app.config,WCFServer2.Service1binding"wsHttpBinding"改成"wsDualHttpBinding"

?

?

?

?

客服端

1,新建一個“windows窗口程序”名稱為WCFClient2。

2,添加服務引用。

服務引用地址到服務端的app.config查看,就是baseAddress。

3.在FormClient中繼承接口IService1Callback,實現方法NotifyClientMsg

4。主窗體啟動是初始化。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

?

using WCFClient2.ServiceReference1;

using System.ServiceModel;

?

?

namespace WCFClient2

{

??? public partial class Form1 : Form, IService1Callback

??? {

??????? publicForm1()

??????? {

??????????? InitializeComponent();

??????? }

?

??????? public void NotifyClientMsg(stringdevStateInfo)

??????? {

??????????? MessageBox.Show(devStateInfo,"ddd");

??????? }

?

??????? IService1m_Innerclient;

?

??????? privatevoid Form1_Load(objectsender, EventArgs e)

??????? {

??? ????????InstanceContextm_CallBackContext;

??????????? m_CallBackContext = new InstanceContext(this);

?

??????????? DuplexChannelFactory<IService1> m_ChannelFactory;

??????????? NetTcpBindingbinding = new NetTcpBinding();

??????????? stringstrUrl = string.Format("net.tcp://{0}:{1}/WCFHostServer/Service1",

??????????????? "localhost",10085);

??????????? UribaseAddress = new Uri(strUrl);

??????????? m_ChannelFactory = new DuplexChannelFactory<IService1>(m_CallBackContext, binding, new EndpointAddress(baseAddress));

??????????? m_Innerclient =m_ChannelFactory.CreateChannel();

?

??????????? Service1Clienthost = new ServiceReference1.Service1Client(m_CallBackContext);

??????????? host.Init();//調用GetSvrTime獲取到wcfserver上的時間

?

??????? }

??? }

}


總結

以上是生活随笔為你收集整理的WCF服务端调用client.的全部內容,希望文章能夠幫你解決所遇到的問題。

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