1.新建一个MVC web项目。

2.点击项目,【右键】→【添加】→【新建项】

3.点击【Web】→【Web服务】

4.恭喜,Web Service已经新建成功,里面的方法就可以参考着根据自己的需要进行修改了,是不是很简单。

5.Web Serice建成之后当然是开始调用了。在【引用】上【右键】,添加【服务引用】

6.开始引用。

7.恭喜服务已经引用成功。

再看配置文件,会多出一些代码,这些都是自动生成的,可以看看理解理解。

8.开始在程序中调用方法了

9.到此为止Web Service的建立到调用已经全部完成。

10.新建WCF与Web Service基本上是一样的,下面直图解不在介绍。

点击项目,【右键】→【添加】→【新建项】→【Web】→【WCF服务】

11.WCF建成之后当然是开始调用了。在【引用】上【右键】,添加【服务引用】,以后过程即使一模一样的了。

12.WCF和Web Service已经建立成功,那么如何供外部访问呢,当时是发布了,发布在IIS就可以,和发布网站是一样的。

WCF控制台应用程序例子

上面这是个简单的架构图。Iservice大家都有的功能的接口(契约),IUserService针对不同的类定义不同的接口,

AbstractService实现IService的公共方法,Uservice实现自己的方法,也可以重写集成的方法。

下面是分层,Web层,大家可以当做不存在,哈哈。

下面是IService层

using System;
using System.Collections.Generic;
using System.ServiceModel; namespace IServiceClassLibrary
{
[ServiceContract]
public interface IService<TModel> : IDisposable where TModel : new()
{
[OperationContract]
string Add(TModel tModel); [OperationContract]
string Delete(int id); [OperationContract]
string Edit(TModel tModel); [OperationContract]
TModel GetModel(int id); [OperationContract]
IEnumerable<TModel> GetAll();
}
}
using System;
using System.Runtime.Serialization; namespace IServiceClassLibrary
{
[DataContract]
public class UserData
{
[DataMember]
public int UserID { get; set; } [DataMember]
public string UserName { get; set; } [DataMember]
public string Password { get; set; } [DataMember]
public string Discribe { get; set; } [DataMember]
public DateTime SubmitTime { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ServiceModel; namespace IServiceClassLibrary
{
[ServiceContract]
public interface IUserService : IService<UserData>
{
[OperationContract]
IDictionary<int, string> GetUserDict();
}
}

下面是Service层

using IServiceClassLibrary;
using System;
using System.Collections.Generic; namespace ServiceClassLibrary
{
public abstract class AbstractService<TModel> : IService<TModel> where TModel : new()
{
public virtual string Add(TModel tModel)
{
//throw new NotImplementedException();
return "Add";
} public virtual string Delete(int id)
{
//throw new NotImplementedException();
return "Delete";
} public virtual string Edit(TModel tModel)
{
//throw new NotImplementedException();
return "Edit";
} public virtual TModel GetModel(int id)
{
TModel tModel = new TModel();
return tModel;
} public virtual IEnumerable<TModel> GetAll()
{
IList<TModel> tModels = new List<TModel>();
return tModels;
} public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool dispose)
{
if (dispose)
{
//CleanUp managed objects by calling thier
}
}
}
}
using IServiceClassLibrary;
using System.Collections.Generic; namespace ServiceClassLibrary
{
public class UserService : AbstractService<UserData>, IUserService
{
public IDictionary<int, string> GetUserDict()
{
IDictionary<int, string> keyValue = new Dictionary<int, string>();
keyValue.Add(, "test");
return keyValue;
} protected override void Dispose(bool dispose)
{
if (dispose)
{
//CleanUp managed objects by calling thier
}
}
}
}

下面是Server层,这层需要配置了,【新建】→【控制台应用程序】建好之后,【添加】→【新建项】→【WCF 服务】,建号之后,生成的两个文件可以删去了,

查看配置信息,对配置进行修改就可以了,懒人模式,高效准确。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ServiceClassLibrary.UserService">
<endpoint address="" binding="basicHttpBinding" contract="IServiceClassLibrary.IUserService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
using IServiceClassLibrary;
using ServiceClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WCFServer
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(UserService)))
{
host.AddServiceEndpoint(typeof(IUserService),
new BasicHttpBinding(),
new Uri("http://localhost:8733/ServiceClassLibrary/UserService/"));
if (host.State != CommunicationState.Opening)
{
host.Open();
}
Console.WriteLine("服务已经启动!");
Console.ReadLine();
}
}
}
}

下面是客服端

using IServiceClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WCFClient
{
class Program
{
static void Main(string[] args)
{
EndpointAddress ea = new EndpointAddress("http://localhost:8733/ServiceClassLibrary/UserService/");
IUserService proxy = ChannelFactory<IUserService>.CreateChannel(new BasicHttpBinding(), ea);
Console.WriteLine(proxy.Delete());
Console.WriteLine(proxy.GetUserDict().First());
Console.ReadLine();
}
}
}

OK,WCF程序已经建立完成,先启动Server,再启动Client,程序正常运行。

如果遇到下面的问题,请以管理员身份运行就可以了。

其他的WCF文章

1. WCF入门

2. 我的第一个WCF程序

3. Web Service 和WCF的比较

4. 基于.NET的WebService的实现和WCF的实现

基于.NET的WebService的实现和WCF的实现的更多相关文章

  1. 基于soapUI构建WebService测试框架

    基于soapUI构建WebService测试框架 http://www.docin.com/p-775523285.html

  2. 基于Jws的WebService项目

    基于Jws的WebService项目   1.服务器端建立 1.1.创建接口 [java] view plaincopy @WebService  public interface IWebServi ...

  3. 【转】构建基于CXF的WebService服务

    构建基于CXF的WebService服务 Apache CXF = Celtix+ XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.C ...

  4. python发布及调用基于SOAP的webservice

    现如今面向服务(SOA)的架构设计已经成为主流,把公用的服务打包成一个个webservice供各方调用是一种非常常用的做法,而应用最广泛的则是基于SOAP协议和wsdl的webservice.本文讲解 ...

  5. 基于JAX-WS的webService开发实例

    最近因为工作原因接触到webService,所以记录下开发中碰到的问题,方便自己以后复习,顺便发扬一下开源精神.刚刚接触webServie如果有什么错误欢迎大家指正. 本地环境:myEclipse10 ...

  6. 基于PI的Webservice发布实例

    [转自http://blog.csdn.net/yin_chuan_lang/article/details/6706816] 最近的项目中,接口较多,而Webservice技术是主要实现方式之一.下 ...

  7. Myeclipse8.5上基于JAX-WS开发WebService

    1.JAX-WS介绍 JAX-WS规范是一组XML web services的JAVA API. 2.开发步骤 基于JAX-WS的WebService开发步骤如下: 2.1 新建一个Web Servi ...

  8. 使用sproxy.exe访问基于soap的webservice

    使用vc访问基于soap的webservice有多种方法,其中有一种是使用atlsoap,关于这个可以搜索sproxy.exe文章,不在这介绍(主要是我的写作能力太差).我写这个日记主要是项记录访问w ...

  9. PHP基于SOAP实现webservice

    简单对象访问协议(SOAP)是一种轻量的.简单的.基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息. SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( H ...

随机推荐

  1. 解决删除/升级Python导致Ubuuntu无法进入桌面的问题

    找到问题的原因后于是换个思路,想大概修复了python,Ubuntu进入桌面应该也就没啥问题了.于是重新安装Python发现还是无济于事.也通过/usr/bin/python: can't find ...

  2. 【练习】增加日志组数至4组,且每组日志成员大小为50M,每组2个成员。

    1.查看日志组成员路径及日志组大小.状态 SQL> select group#,member from v$logfile; GROUP# MEMBER ---------- --------- ...

  3. 【测试】通过RMAN联机全库备份,包括控制文件,归档日志文件,备份成功后,删除已备份的归档日志。

    RMAN是一个很方便很好用的备份,恢复,还原的一个工具,做这个小测试其实只有一个RMAN语句就完全解决了这么大的需求: RMAN> backup as backupset full databa ...

  4. html中button的type属性

         接触web开发不久,今天遇到了一个问题,点击button按钮,浏览器没有反应,尝试了自己可以想到的所有办法,还是无果.只得请教他人,才发现是button的type属性搞得怪,原来:     ...

  5. linx 实用操作命令一

    如果apache安装成为linux的服务的话,可以用以下命令操作:service httpd start 启动service httpd restart 重新启动service httpd stop ...

  6. java基础回顾(二)——内部类

    一.常规内部类 public class Outer { int count; void say(){ count++; System.out.println("我是外部类"); ...

  7. MFC六大核心机制之一:MFC程序的初始化

    很多做软件开发的人都有一种对事情刨根问底的精神,例如我们一直在用的MFC,很方便,不用学太多原理性的知识就可以做出各种窗口程序,但喜欢钻研的朋友肯定想知道,到底微软帮我们做了些什么,让我们在它的框架下 ...

  8. Python 编程规范-----转载

    Python编程规范及性能优化 Ptyhon编程规范 编码 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- .设置编辑器,默认保存为 utf-8 格式. ...

  9. CSS3 圆形时钟式网页进度条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. grub2

    手工启动 set  root(hd0,msdos7) linux  /boot/vmlinuz-3.9.8-300.fc19.i686.PAE  root=/dev/sda7 initrd  /boo ...