C# 创建一个WCF服务
做代码统计,方便以后使用:
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服务的更多相关文章
- WCF学习系列一_创建第一个WCF服务
原创作者:灰灰虫的家http://hi.baidu.com/grayworm WCF开发实战系列一:创建第一个WCF服务 在这个实战中我们将使用DataContract,ServiceContract ...
- WCF开发实战系列一:创建第一个WCF服务
WCF开发实战系列一:创建第一个WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 在这个实战中我们将使用DataContract,ServiceContract ...
- WCF开发实战系列一:创建第一个WCF服务 转
转 http://www.cnblogs.com/poissonnotes/archive/2010/08/28/1811064.html 在这个实战中我们将使用DataContract,Servic ...
- 为MongoDB创建一个Windows服务
一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...
- 【LINUX】——linux如何使用Python创建一个web服务
问:linux如何使用Python创建一个web服务? 答:一句话,Python! 一句代码: /usr/local/bin/python -m SimpleHTTPServer 8686 > ...
- ng 通过factory方法来创建一个心跳服务
<!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <met ...
- 使用PHP创建一个socket服务端
与常规web开发不同,使用socket开发可以摆脱http的限制.可自定义协议,使用长连接.PHP代码常驻内存等.学习资料来源于workerman官方视频与文档. 通常创建一个socket服务包括这几 ...
- 20190710用控制台启动一个wcf服务
快速阅读 如何用控制台启动一个wcf服务,已经wcf的配置和在类库中如何实现 . wcf类库 用vs新建一个类库,引用system.ServiceModel 定义接口实现服务契约和操作契约 [Serv ...
- 创建第一个WCF服务
创建WCF服务 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序. 2.建立完成后如下图所示: 3.删除系统生成的两个文件IService1.cs与Service1.s ...
随机推荐
- dota有哪些经典的典故或笑话?
----------------------------------------------------dota有哪些经典的典故或笑话?虽然现在玩游戏也没什么热情了, 但是看到这些还是笑尿,笑点低 = ...
- IIS发布ASP程序问题汇总
看异常位置,因为域的问题
- UML之用例图详解
原文链接:https://blog.csdn.net/mj_ww/article/details/53020080 UML,即Unified Model Language,统一建模语言.百度百科对他的 ...
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...
- 6、C++共用体
6.共用体 共用体(union)是一种数据格式,他能够存储不同的数据类型,但只能同时存储其中的一种类型.也就是说,结构可以同时存储int.long和double,共用体只能存储ing.long.dou ...
- js 三大事件(鼠标.键盘.浏览器)
鼠标事件: click:单击 dblclick:双击 mousedown:鼠标按下 mouseup:鼠标抬起 mouseover:鼠标悬浮(进入) mouseout:鼠标离开(离开) mousemov ...
- bootstrap的使用2
表单控件: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- centos用ifconfig不显示ip地址的解决方法
解决办法: 第一步: 输入ip addr 发现ens33中并不包含IP内容 第二步: 输入cd /etc/sysconfig/network-scripts/ 回车 然后输入 ls 回车 第三步:选 ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
- Codeforces - 912B 位运算
求[1,n]内k的值的异或和使其值最大 k=1是肯定是n k>1,设pos是n的最高位,那答案就是(1ll<<(pos+1))-1 这里用到一个性质是当S=2^i-1时,a xor ...