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));//Service1是wcf服務的類名稱?
??????????? 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.Service1的binding由"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.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应用栈求解迷宫问题(C++实现)
- 下一篇: 设计模式[20]-Builder