WCF只有服务端响应
来源:互联网 发布:编程和软件开发的区别 编辑:程序博客网 时间:2024/04/28 18:44
wcf服务端
1,新建一个“windows窗体程序”名称为WCFServer1。
2,然后添加一个“WCF服务”名称为Service1。具体步骤为:解决方案试图中,选中“WCFServer1”项目,右键,在弹出菜单中选择“添加->新建项”。
3,双击主窗体,在它的Load事件中编写代码启动wcf服务:BasicHttpBinding方式启动wcf服务。此文件代码如下:
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 System.ServiceModel;
using System.ServiceModel.Description;
namespace WCFServer1
{
public partial class Form1 : Form
{
ServiceHost m_ServiceHost;
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(objectsender,EventArgs e)
{
m_ServiceHost = new ServiceHost(typeof(Service1));//Service1是wcf服务的类名称
//BasicHttpBinding方式启动wcf服务
ServiceMetadataBehaviormetadataBehavior;
metadataBehavior =m_ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
if(metadataBehavior ==null)
{
metadataBehavior = newServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled= true;
metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1"));
m_ServiceHost.Description.Behaviors.Add(metadataBehavior);
}
else
{
metadataBehavior.HttpGetEnabled= true;
metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1"));
}
m_ServiceHost.Open();
}
privatevoid FormService_FormClosing(object sender,FormClosingEventArgse)
{
if(m_ServiceHost != null)
{
m_ServiceHost.Close();
m_ServiceHost = null;
}
}
}
}
4,在 IService1.cs中增加一个方法GetSvrTime
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFServer1
{
// 注意:使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
voidDoWork();
[OperationContract]
stringGetSvrTime();
}
}
5, 在Service1.cs中实现接口的方法GetSvrTime,获取服务器时间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFServer1
{
// 注意:使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public void DoWork()
{
}
public string GetSvrTime()
{
returnDateTime.Now.ToString();
}
}
}
第二步:WCF客户端
1,新建一个“windows窗体程序”命名为WCFClient1。
2,在Debug中运行WCFHostServer窗体(这一步很重要),然后在项目WCFHostClient中右键--》“添加服务引用”,在地址填入以BasicHttpBinding绑定方式启动wcf服务的地址http://localhost:10085/WCFHostServer/Service1,点击“前往”,然后连接上后点击“确定”,在WCFHostClient项目中就会增加一个Service References(ServiceReference1)
3,主窗体上增加一个按钮,双击此按钮添加响应事件。
using WCFClient1.ServiceReference1;
using System.ServiceModel;
//...
private voidbutton1_Click(object sender, EventArgs e)
{
//basicHttpBinding绑定方式的通讯单工
Service1Clienthost =new ServiceReference1.Service1Client();
stringhtime = host.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间
MessageBox.Show("basicHttpBinding" +"@" + htime);
//NetTcpBinding绑定方式的通讯双工
IService1m_Innerclient;
ChannelFactory<IService1> m_ChannelFactory;
NetTcpBindingbinding =new NetTcpBinding();
UribaseAddress = new Uri("net.tcp://localhost:10085/WCFHostServer/Service1");
m_ChannelFactory = newChannelFactory<IService1>(binding,newEndpointAddress(baseAddress));
m_Innerclient =m_ChannelFactory.CreateChannel();
stringhtime1 = m_Innerclient.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间
MessageBox.Show("NetTcpBinding" +"@" + htime1);
}
- WCF只有服务端响应
- wcf找不到服务端问题解决
- WCF服务端与客户端
- WCF服务端调用客户端.
- WCF 服务端的异步
- WCF 响应超时问题
- WCF安全操练(2)--WCF服务端
- 登录 之 服务端响应
- WCF客户端和服务端配置
- 操作Wcf服务端Config文件
- WCF----双工模式服务端回调客户端方法时无响应,“发送到 http://***/User.svc 的请求操作在配置的超时内未收到回复”
- 利用WCF共享ASP.NET session实现WCF服务端验证
- WCF,传一个基类到服务端,服务端转为子类
- 使用WCF-Client如何调用服务端
- Android访问WCF服务(上篇)-服务端开发
- WCF服务端与客户端代码示例
- WCF服务端运行时架构体系详解
- 服务端分页 - WCF RIA Services 代码
- oracle查所有字段
- 【C++】类的静态成员函数和静态成员变量的使用
- Java Map 如何实现Key 的唯一性?
- 从零学编程1-写一封情书
- UVA 409 Excuses, Excuses!
- WCF只有服务端响应
- ActionBar --3.0
- 【OI康复训练】POJ2187旋转卡壳
- 关于coco2d-X Android 打包问题
- SpringMVC return:redirect 中文乱码
- 低版本中使用ActionBar
- 安装Vagrant中
- 批处理文件路径问题
- Andrid Stuido SVN配置在Mac系统下的步骤及注意事项(上)