分布式系统:指在系统与系统之间进行通信,系统不再是孤立的,例如:淘宝查看物流信息,或是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项目的更多相关文章

  1. (15)Visual Studio中使用PCL项目加入WCF WebService参考

    原文 Visual Studio中使用PCL项目加入WCF WebService参考 Visual Studio中使用PCL项目加入WCF WebService参考 作者:Steven Chang 2 ...

  2. [转]初试visual studio2012的新型数据库LocalDB 及 在visual studio2012中如何使用localDB具体讲解

    本文转自:http://www.cnblogs.com/zhangran/archive/2012/08/26/2657864.html 初试visual studio2012的新型数据库LocalD ...

  3. [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考、NuGet组件参考

    [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考.NuGet组件参考 解决方案 目前Visual Studio中最新版本的Xamarin.iO ...

  4. Web 应用程序项目与 Visual Studio 中的网站项目的异同

    要查看英语原文,请勾选“英语”复选框.也可将鼠标指针移到文本上,在弹出窗口中显示英语原文. 翻译 英语 本文档已存档,并且将不进行维护. Web 应用程序项目与 Visual Studio 中的网站项 ...

  5. 在Visual Studio中新增生成项目

    在Visual Studio中新增生成项目 选择适配器类型 选择WCF-SQL适配器 创建连接选项 选择相应的存储过程 生成相应的消息架构

  6. 在visual studio2012中如何使用localDB具体讲解

    http://www.cnblogs.com/zhangran/archive/2012/08/26/2657864.html 说明: 经过一段时间的小捉摸终于基本掌握在vs2012中如何使用loca ...

  7. 久违的问候-----eclipse中搭建maven项目2016年

    好久没有写过博客了,可是一直向别人推荐自己的博客,深感惭愧!今天再次在寒冷之夜继续code,config,write. 接下来,我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一 ...

  8. 我们的相识,总是那么巧。-------eclipse中搭建maven项目

    一.我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一个简单的例子,但是过程是很艰辛的. 首先我们看一下eclipse的封面,下面就是刚打开的华丽封面哦 其次我安装了eclips ...

  9. HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本 ,请添加处理程序。如果下载文件,请添加 MIME 映射。 IIS站点中添加WCF项目后浏览网站报错解决方法。

    vs2013 wcf服务 windows10 家庭中文版 上图中的红色没有出现就按照下面的方法做: 按照上图所示勾选. 刷新上图中你的网站,查看上图右边的内容是否出现,如果出现,再次重新浏览网站,看一 ...

随机推荐

  1. Centos环境下Tomcat启动缓慢

    最近项目上线部署的时候,发现一个问题.Tomcat在启动过程中耗费了很长的时间.查看日志,发现耗时最长的地方是:INFO [localhost-startStop-1] org.apache.cata ...

  2. 关于JAVA中URL传递中文参数的问题

    今天在调用一个接口时,需要传递一个中文参数,结果获取不到数据,原因就在于中文传参的编码问题. 问题来源:URL url= new URL("http://XXX?OrganName=司法厅& ...

  3. NOI 1998 免费馅饼

    附题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4901 时间限制(普通/Java ...

  4. OcciWrapper使用指南(高性能Oracle访问组件)

    occiwrapper使用指南 occiwrapper是一个开源的.跨平台的Oracle访问组件, 方便C++开发者们灵活地操作oracle数据库.为了方便使用,组件中的接口形式参考的POCO库的使用 ...

  5. freemarker 数字,字符的操作

    1. 数据类型转换: ${xx?string} //字符串 ${xx?number}//整数 ${xx?currency}//小数 ${xx?percent}//百分比 2. 截取字符串长度 有的时候 ...

  6. springmvc + spring + mybatis + maven整合配置文件

    源码下载地址:http://download.csdn.net/detail/a757956132/9353345 src/main/java sy controller dao model serv ...

  7. 杭电ACM1218——Blurred Vision

    题目有点长,并且比較难懂.看了非常久.也看的不是非常懂,仅仅知道输入输出的格式. 直到看了最后的一句话,也就是output那里的最后一句话,题目的意思就非常明白了,就是输出的每个点的像素是原始的四个像 ...

  8. Delphi 读取CPU Id

    在网上找了很久,终于找了一段能读出正确CPU ID 的代码,以下代码经过Delphi7测试 procedure TForm1.Button1Click(Sender: TObject); var _e ...

  9. 设置tomcat内存

    设置Tomcat启动的初始内存其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4. 可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置 三.实例,以下给 ...

  10. web app 相关记录

    今天在手机浏览器上运行cocos2d-html5的sample, crystalcraze运行起来只有10~20帧, moonwarrior只有20~30帧,很不理想的数据: 记录下几个web app ...