使用纯文本文件打造WCF服务

简介:

这篇文章将教你如何不使用Visual Studio,使用纯文本文件创建一个WCF服务。

     一、编写代码:

    首先,在你的C盘创建两个txt文件。一个文件名为:ServerProgram.txt,一个文件名为:ClientProgram.txt。

    在ServerProgram.txt中输入下面代码,实现一个加法的WCF服务,代码很简单:

复制代码
1  using  System;
2    using  System.ServiceModel;
3 
4  namespace  SimpleWcfService
5  {
6  class  ServerProgram
7  {
8  static void  Main( string [] args)
9  {
10 
11  BasicHttpBinding binding  = new  BasicHttpBinding();
12  Uri serviceUri  = new  Uri( " http://localhost:8001 " );
13  ServiceHost host  = new  ServiceHost( typeof (SimpleWcfService), serviceUri);
14  host.AddServiceEndpoint( typeof (ISimpleWcfService), binding,  " OperationService " );
15  host.Open();
16  Console.WriteLine( " 服务启动 " );
17  Console.ReadLine();
18  host.Close();
19 
20  }
21  }
22 
23  [ServiceContract]
24  public interface  ISimpleWcfService
25  {
26  [OperationContract]
27  int  Add( int  a,  int  b);
28  }
29 
30  public class  SimpleWcfService : ISimpleWcfService
31  {
32  public int  Add( int  a, int  b)
33  {
34  return  a  +  b;
35  }
36  }
37  }
复制代码

在ClientProgram.txt中输入下面代码来调用上面的WCF服务:

复制代码
1  using  System;
2  using  System.ServiceModel;
3  using  System.Windows.Forms;
4 
5  namespace  SimpleWcfServiceClient
6  {
7  class  ClientProgram
8  {
9  static void  Main( string [] args)
10  {
11  string  input;
12  int  a, b;
13  Console.WriteLine( " 请输入两个整数,以逗号隔开! " );
14  input  = Console.ReadLine();
15  = int .Parse(input.Split( ' , ' )[ 0 ]);
16  = int .Parse(input.Split( ' , ' )[ 1 ]);
17  BasicHttpBinding binding  = new  BasicHttpBinding();
18  ChannelFactory < ISimpleWcfService >  factory  = new  ChannelFactory < ISimpleWcfService > (binding,  new  EndpointAddress( " http://localhost:8001/OperationService " ));
19  ISimpleWcfService proxy  =  factory.CreateChannel();
20  int  result  =  proxy.Add(a,b);
21  Console.WriteLine( string .Format( " 经过WCF服务计算,{0}加{1}原来等于{2}. " ,a,b, result));
22  Console.ReadLine();
23 
24 
25  }
26 
27  [ServiceContract]
28  public interface  ISimpleWcfService
29  {
30  [OperationContract]
31  int  Add( int  a, int  b);
32  }
33  }
34  }
复制代码

 

二、编译代码:

打开Command prompt 。通过cmd命令,转到C盘下面。
1、输入下面命令编译服务端代码:
csc /r:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll" ServerProgram.txt
2、输入下面命令编译客户端代码:
csc /r:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll" ClientProgram.txt
3、/r:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll"用于引入System.ServiceModel.dll

三、运行程序:
1、服务端截图


2、客户端截图:


 

总结:本文使用文本文件创建一个WCF服务。这个是我以前在服务器上测试WCF的方法,由于服务器上没安装VS,只有.net framwork。此法只适用于学习和研究,不建议在实际中使用。





本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2010/09/21/use-notebook-create-a-wcf-service.html,如需转载请自行联系原作者

相关文章
|
10月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
9月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
39 0
|
9月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
66 0
|
10月前
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
105 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
858 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1050 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
999 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
981 0