Visual Studio2012中搭建WCF项目
分布式系统:指在系统与系统之间进行通信,系统不再是孤立的,例如:淘宝查看物流信息,或是hao123的天气预报,这些可能都是用的别的系统的web方法。
1.创建空的解决方案
2.新建项目-WCF服务库项目,项目名称:XfrogWCFService
3.在XfrogWCFService项目中添加[System.ServiceModel]的引用
4.上图是该项目的结构打开IFirstServices.cs这个是接口也是一个服务契约,客户端调用方法都必须遵守,代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace XfrogWCFService
{
/// <summary>
/// 注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务
/// </summary>
[ServiceContract]
public interface IFirstService
{
/// <summary>
/// 表示该方法是IFirstService的一个服务方法,客户端可远程调用该方法。
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[OperationContract]
String GetData(int a,int b);
}
}
5.再打开FiretService.cs这个文件对接口进行实现,也就是Web方法所要实现的功能,为了学习,做了一个两数相加的操作,代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace XfrogWCFService
{
public class FirstService : IFirstService
{
string IFirstService.GetData(int a,int b)
{
return String.Format("{0}+{1}={2}",a,b,(a+b));
}
}
}
6.下面我们在解决方案中重新建立一个【客户端控制台应用程序】的项目,名称叫做Host,这是web服务的主人[宿主],意思是该服务的启动或者关闭都归它来控制。
7.我们的解决方案成了上面的结构,也同样要添加[System.ServiceModel]的引用,还有对XfrogWCFService的引用,然后打开Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using XfrogWCFService;
namespace Host
{
class Program
{
static void Main(string[] args)
{ //这段代码说创建一个新的服务宿主,这个服务是FirstService
//也就是实现IFirstService接口的类
using (ServiceHost host = new ServiceHost(typeof(FirstService)))
{
host.Open();
Console.WriteLine("服务已启动,按任意键中止...");
Console.ReadKey(true);
host.Close();
}
}
}
}
8.接下来打开Host项目中的App.config文件,应该是会添加ServiceModel节点,如下面:
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <system.serviceModel>
<services>
<service name="XfrogWCFService.FirstService" behaviorConfiguration="behaviorConfiguration">
<host>
<baseAddresses>
<add baseAddress="http://192.168.10.58:8100/"/>
</baseAddresses>
</host>
<endpoint address="http://192.168.10.58:8100/" binding="basicHttpBinding"
contract="XfrogWCFService.IFirstService"
name="BasicHttpBinding_IFirstService"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>
9.现在服务这边应该就是搭建好了,可以测试下,运行Host项目:如果是这样代表服务已经搭建成功了!
------------------------------------下面是客户端的哦-----------------------------------------
1.重新在别的地方建立一个解决方案,然后创建一个类库名称是Clinent1。
2.添加webService引用,步骤是:
2.1右击【引用】点击【添加服务引用】然后如下图:
地址是我们在wcf服务器端App.config页面写的地址将ip换成localhost
点击转到会查看到这个地址的服务
命名空间可以修改,这个是我们调用服务的命名空间,然后点击【确定】
3.给Clinent1添加ServiceModel引用。
4.在Clinent1中新建一个类Program.cs代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Clinent1
{
public class Program
{
/// <summary>
/// WCF调用方法
/// </summary>
/// <param name="par"></param>
/// <returns></returns>
public string getwcfstr(int a,int b)
{
ServiceReference1.FirstServiceClient aa = new ServiceReference1.FirstServiceClient();
return aa.GetData(a, b);//调用服务服务器接口的方法。
}
}
}
5.在该解决方案中创建一个新的网站项目,添加ServiceModel和Clinent1的引用。
6.Clinent1的App.config如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFirstService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.10.58:8100/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IFirstService" contract="ServiceReference1.IFirstService"
name="BasicHttpBinding_IFirstService" />
</client>
</system.serviceModel>
</configuration>
7.web.Config在configuration下添加,代码如下:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFirstService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.10.58:8100/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IFirstService" contract="ServiceReference1.IFirstService"
name="BasicHttpBinding_IFirstService" />
</client>
</system.serviceModel>
8.添加一个aspx页面,前台写个div后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
Program p=new Program();
//设置div的内容调用服务传递参数
div_wcf.InnerText = p.getwcfstr(10, 5);
}
9.客户端的结构是:
10.运行WebForm1.aspx的效果:为了测试,做的功能很简单啊。
Visual Studio2012中搭建WCF项目的更多相关文章
- (15)Visual Studio中使用PCL项目加入WCF WebService参考
原文 Visual Studio中使用PCL项目加入WCF WebService参考 Visual Studio中使用PCL项目加入WCF WebService参考 作者:Steven Chang 2 ...
- [转]初试visual studio2012的新型数据库LocalDB 及 在visual studio2012中如何使用localDB具体讲解
本文转自:http://www.cnblogs.com/zhangran/archive/2012/08/26/2657864.html 初试visual studio2012的新型数据库LocalD ...
- [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考、NuGet组件参考
[Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考.NuGet组件参考 解决方案 目前Visual Studio中最新版本的Xamarin.iO ...
- Web 应用程序项目与 Visual Studio 中的网站项目的异同
要查看英语原文,请勾选“英语”复选框.也可将鼠标指针移到文本上,在弹出窗口中显示英语原文. 翻译 英语 本文档已存档,并且将不进行维护. Web 应用程序项目与 Visual Studio 中的网站项 ...
- 在Visual Studio中新增生成项目
在Visual Studio中新增生成项目 选择适配器类型 选择WCF-SQL适配器 创建连接选项 选择相应的存储过程 生成相应的消息架构
- 在visual studio2012中如何使用localDB具体讲解
http://www.cnblogs.com/zhangran/archive/2012/08/26/2657864.html 说明: 经过一段时间的小捉摸终于基本掌握在vs2012中如何使用loca ...
- 久违的问候-----eclipse中搭建maven项目2016年
好久没有写过博客了,可是一直向别人推荐自己的博客,深感惭愧!今天再次在寒冷之夜继续code,config,write. 接下来,我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一 ...
- 我们的相识,总是那么巧。-------eclipse中搭建maven项目
一.我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一个简单的例子,但是过程是很艰辛的. 首先我们看一下eclipse的封面,下面就是刚打开的华丽封面哦 其次我安装了eclips ...
- HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本 ,请添加处理程序。如果下载文件,请添加 MIME 映射。 IIS站点中添加WCF项目后浏览网站报错解决方法。
vs2013 wcf服务 windows10 家庭中文版 上图中的红色没有出现就按照下面的方法做: 按照上图所示勾选. 刷新上图中你的网站,查看上图右边的内容是否出现,如果出现,再次重新浏览网站,看一 ...
随机推荐
- Java 动态生成 复杂 .doc文件
阅读目录 1.word 里面调整好排版,包括你想生成的动态部分,还有一些不用生成的规则性的文字 2. 将 word 文档保存为 xml 3.用 Firstobject free XML edito 打 ...
- 文件正在上传的转圈圈gif图片引出的fixed定位和absolute定位
文件正在上传的转圈圈gif图片 一.文件上传时,未上传返回成功状态之前给个gif动态图片显示在页面,改善用户体验. <!--S 遮罩层 --> <div id="mas ...
- Android - TextView Ellipsize属性
Android - TextView Ellipsize属性 本文地址: http://blog.csdn.net/caroline_wendy android:ellipsize属性: If set ...
- Unityclient通信測试问题处理(二)
Unityclient通信測试问题处理(二) 在client的通信測试过程中.场景载入的问题给自己带来了不小的麻烦.由于消息的解析方法在单独的监听线程中调用,这也就意味着无法在消息的解析方法中调用Un ...
- sql语法:inner join on, left join on, right join on具体用法
inner join(等值连接) 仅仅返回两个表中联结字段相等的行 left join(左联接) 返回包含左表中的全部记录和右表中联结字段相等的记录 right join(右联接) 返回包含右表中的全 ...
- 标准I/O库之标准I/O的效率
程序清单5-1 用getc和putc将标准输入复制到标准输出 #include "apue.h" int main( void ) { int c; while(( c = get ...
- 【转】GitHub平台最火的iOS开源项目——2013-08-25 17
http://www.cnblogs.com/lhming/category/391396.html 今天,我们将介绍20个在GitHub上非常受开发者欢迎的iOS开源项目,你准备好了吗? 1. AF ...
- 认识CoreData-基础使用
第一篇文章中并没有讲CoreData的具体用法,只是对CoreData做了一个详细的介绍,算是一个开始和总结吧. 这篇文章中会主要讲CoreData的基础使用,以及在使用中需要注意的一些细节.因为文章 ...
- 在windows C++中编译并使用Lua脚本
早前就用过LUA ,只是局部的小项目使用,突然兴起想要写一些关于LUA 的 文章,记录曾经学习过的点点滴滴. 这里我使用的是LUA5.2作为 案例 lua做为轻量级脚本语言已经被广泛应用到应用软件以 ...
- [置顶] 读取pdf并且在web页面中显示
读取pdf并且在web页面中显示 if (System.IO.File.Exists(f)) { Response.ContentType = "applicationpdf"; ...