做代码统计,方便以后使用:

app.config配置文件设置:

<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webBinding" maxBufferPoolSize="" maxBufferSize="" maxReceivedMessageSize="">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength="" maxBytesPerRead="" maxNameTableCharCount=""/>
</binding>
</webHttpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="mySerBeh">
<serviceMetadata httpGetEnabled="true"/>
<!--httpGetUrl="mex"-->
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息-->
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpendBehavior">
<webHttp></webHttp>
</behavior>
</endpointBehaviors>
</behaviors> <!-- 看到services节,就表明这是在定义服务相关的内容 -->
<services>
<!-- 定义一个服务,name是契约实现类的全名 -->
<service behaviorConfiguration="mySerBeh" name="WCFExample.WCF.UserService">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:21467/"/>
</baseAddresses>
</host>
<!-- 定义一下终节点,address一般为空,如果不为空,最终服务地址就是在baseAddress的基础上加上这个address,binding指定为basicHttpBinding,
这是最基础的基于http的绑定方式,contract标明这是为哪个契约服务 -->
<endpoint address="wcfs" behaviorConfiguration="webHttpendBehavior"
binding="webHttpBinding" bindingConfiguration="webBinding" contract="WCFExample.WCF.IService"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>

基本内容可以直接创建一个wpf服务会生成基本内容,服务分成两个,一个去做接口,一个去实现接口:

接口类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web; namespace WCFExample.WCF
{ //需要引用类库 System.ServiceModel 和 System.ServiceModel.Web
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetCon?op={name}", ResponseFormat = WebMessageFormat.Json)]
string GetCon(string name);
}
}

实现方法类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WCFExample.WCF
{
/// <summary>
/// 用ServiceBehavior为契约实现类标定行为属性,此处指定并发模型为ConcurrencyMode.Multiple,即并发访问
/// </summary>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class UserService : IService
{
public string GetCon(string name)
{
return name;
}
}
}

启动WCF类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading; namespace WCFExample.WCF
{
public class WCFService
{ public static void Begion()
{
//无配置文件App.config的情况下手动绑定
//Uri HttpUri = new Uri("Http://localhost:21467/wcf");
//Type Servicetype = typeof(WCFExample.WCF.UserService);
//using (ServiceHost Chost=new ServiceHost (Servicetype,new Uri[]{HttpUri}))
//{
// Binding basicHttpBinding = new BasicHttpBinding();
// string address = "";
//Chost.AddServiceEndpoint(typeof(WCFExample.WCF.UserService), basicHttpBinding, address); // Chost.Open();
// Console.WriteLine("Service Running...");
// Console.ReadKey(true);
// Chost.Close();
//} //定义一个ServiceHost,注意参数中要使用契约实现类而不是接口
ServiceHost host = new ServiceHost(typeof(WCFExample.WCF.UserService));
host.Open();
while (true)
Thread.Sleep();
}
}
}

服务启动后,即可正常使用WCF服务

C# 创建一个WCF服务的更多相关文章

  1. WCF学习系列一_创建第一个WCF服务

    原创作者:灰灰虫的家http://hi.baidu.com/grayworm WCF开发实战系列一:创建第一个WCF服务 在这个实战中我们将使用DataContract,ServiceContract ...

  2. WCF开发实战系列一:创建第一个WCF服务

    WCF开发实战系列一:创建第一个WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 在这个实战中我们将使用DataContract,ServiceContract ...

  3. WCF开发实战系列一:创建第一个WCF服务 转

    转 http://www.cnblogs.com/poissonnotes/archive/2010/08/28/1811064.html 在这个实战中我们将使用DataContract,Servic ...

  4. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  5. 【LINUX】——linux如何使用Python创建一个web服务

    问:linux如何使用Python创建一个web服务? 答:一句话,Python! 一句代码: /usr/local/bin/python -m SimpleHTTPServer 8686 > ...

  6. ng 通过factory方法来创建一个心跳服务

    <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <met ...

  7. 使用PHP创建一个socket服务端

    与常规web开发不同,使用socket开发可以摆脱http的限制.可自定义协议,使用长连接.PHP代码常驻内存等.学习资料来源于workerman官方视频与文档. 通常创建一个socket服务包括这几 ...

  8. 20190710用控制台启动一个wcf服务

    快速阅读 如何用控制台启动一个wcf服务,已经wcf的配置和在类库中如何实现 . wcf类库 用vs新建一个类库,引用system.ServiceModel 定义接口实现服务契约和操作契约 [Serv ...

  9. 创建第一个WCF服务

    创建WCF服务 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序. 2.建立完成后如下图所示: 3.删除系统生成的两个文件IService1.cs与Service1.s ...

随机推荐

  1. innerText、innerHTML

    innerTest修改的是标签的文本内容,如果修改的字符串具有标签的格式,不会把标签展示到页面中 innerHtml打印标签中所有子标签 以字符串的形式,如果修改的字符串有标签的格式,则直接展示到页面 ...

  2. spring 学习(五):spring 事务

    spring 学习(五):spring 事务 事务概要 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同 ...

  3. TypeScript入门-类

    ▓▓▓▓▓▓ 大致介绍 在ECMASript6中引入了类这一概念,通过class声明一个类.对于学习过C和C++的人应该不会陌生 ▓▓▓▓▓▓ 类 看一个简单的类: class Greeter { g ...

  4. GitHub CEO:GitHub 十年,感谢有你

    简评:不知为何,总感觉 GitHub 成立不止 10 年了,你们有这种错觉么? 本文是 GitHub 联合创始人兼 CEO:Chris Wanstrath 在计算机世界杂志写的文章. 当我们回顾 Gi ...

  5. Azure KUDU工具

    Azure网站提供了一个比较不错可以用来对我们的网站进行分析的工具------KUDU,下面我们就来看看这个工具主要能为我们做些啥,啥时候使用它. 如何打开KUDU KUDU所展现的强大功能 如何打开 ...

  6. 【重要的css属性学习】看了乙醇的文章,统计了几个高star前端框架下,Css属性出现最多的,这里学习记录一下

    color background-color display margin-left border-color padding max-width margin-bottom width flex 待 ...

  7. php 常用字符集

    ASCII 字符集 单字节编码,7位(bits)表示一个字符,共128字符 包含内容 控制字符:回车键.退格.换行键等. 可显示字符:英文大小写字符.阿拉伯数字和西文符号 ANSI 码  ANSI编码 ...

  8. CodeForces - 1110C-Meaningless Operation(打表找规律)

    Can the greatest common divisor and bitwise operations have anything in common? It is time to answer ...

  9. python-is,==

    在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识).python type()(数据类型)和value(值).is和==都是对对象进行比较判断 ...

  10. HDU - 4686 函数积的前缀和

    题意:求\(\sum_{i=0}^{n-1}a_ib_i\) 其中,\(a_i=A_xa_{i-1}+A_y,b_i=B_xb_{i-1}+B_y\) 构造矩阵分别维护\(a_ib_i,a_i,b_i ...