一.新建wcf

如下图:wcf可以简历俩种形式

1.库文件,就是一个类库文件,可以用windows服务或控制台开启。

2.服务应用程序,可以直接IIS上面发布。

二.库文件自动生成的类

接口类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization; using System.Text; namespace WCFLib
{
using System.ServiceModel; /*
WCF服务的特点:
* 1、在接口上必须贴上 [ServiceContract]标签来标记这个接口是一个wcf的服务
* 2、在这个接口中的方法上贴上 [OperationContract] 标记这个方法是一个wcf的服务方法,可以被外界调用
*/
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value); }
}

实现类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WCFLib
{
/// <summary>
/// WCF服务的实现类,它是用来处理wcf服务方法的逻辑代码的
/// </summary>
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("你输入了: {0}", value);
}
}
}

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
<system.serviceModel>
<!-- services节点负责存放所有的WCF服务的-->
<services>
<!-- name 一定是配置为当前服务实现类的全名称 契约 (合同)-->
<service name="WCFLib.Service1">
<host>
<baseAddresses>
<!-- 表示当前Service1这个服务的地址,被服务器来监听-->
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFLib/Service1/" />
</baseAddresses>
</host>
<!-- 终结点,也是供服务器来监听,
address="" :表示当前监听的地址使用上面host配置节点中的baseAddress中的地址
如果这个地址不为空,优先使用这个地址
binding="basicHttpBinding" :表示客户端与wcf服务的通讯方式基于基本的http模式
basicHttpsBinding: 表示表示客户端与wcf服务的通讯方式基于基本的https模式, 数据传输更加安全,但是速度要慢一些
contract="WCFLib.IService1" :表示当前wcf服务Service1所对应的节点是谁
-->
<endpoint address="" binding="basicHttpBinding" contract="WCFLib.IService1">
</endpoint> <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。
类似于Webservices的元数据,客户端将来生成的代理类就是按照这个元数据中的描述信息来生成的-->
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service> </services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

控制台托管的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WCF服务的自托管
{
using System.ServiceModel;
using WCFLib; class Program
{
static void Main(string[] args)
{
//开启托管MenusMgr这个wcf的服务
//如果ServiceHost() 的第二个参数不填写的话,自动去查找当前运行项目中的配置文件
//所以要记得在当前项目的app.config中配置好wcf服务库的节点
using (ServiceHost host = new ServiceHost(typeof(MenusMgr)))
{
//开启监听,等待客户端的连接
host.Open(); Console.WriteLine("服务已经开启");
Console.ReadKey();
}
}
}
}

三.客户端调用的方式

引用--添加服务引用--输入地址OK

四.引用好服务调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace Client
{
using Client.WcfServer;
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
MyServerClient my = new MyServerClient();
string str = my.GetPerson("张三");
this.TextBox1.Text = str;
}
}
}

最新文章

  1. java工厂模式
  2. sharepoint2013的审核日志的时间区域设置
  3. Linux下PHP+MYSQL+APACHE配置方法
  4. SQLSERVER分页查询
  5. 一直都在说反射很有用 谈谈大型.NET ERP系统有哪些地方用到了反射
  6. firefox怎么修改tls协议号
  7. Btn要记得对状态进行设置
  8. codeforces 598B Queries on a String
  9. Android_开发人员经常使用的颜色
  10. LogMiner的使用
  11. ListView与RecyclerView对比浅析——缓存机制
  12. Linux(CentOS)用户修改密码有效期(chage命令)
  13. 进程间通信IPC-消息队列
  14. 【深度学习】吴恩达网易公开课练习(class1 week2)
  15. atom编辑器社区插件推荐
  16. CSS笔记——属性选择器
  17. CentOS中安装JAVA环境
  18. Eclipse的快捷键使用总结
  19. ipconfig出现window IP configuration 。。
  20. Day27-28 基础加强

热门文章

  1. iOS UITableView制作类似QQ好友列表视图
  2. spoj14846 Bribe the Prisoners
  3. 安装Maven及Eclipse中配置Maven
  4. python---pyspider,报错?
  5. Day Day Up—— ——fseek()函数的用法
  6. Java Annotation详解
  7. (数组)对数组中的数字加1(plus one)
  8. 《Head First Servlets & JSP》-6-会话管理-listener etc. demo
  9. C#获取WINDOWS系统信息
  10. winform GDI基础(四)简单截屏