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 ...
随机推荐
- 接口测试简单介绍、及jmeter的简单使用
1.接口测试简单介绍 接口测试其实就是功能测试,是从数据库查询到数据,返回查询结果 接口返回的数据都是json,json是一种通用的数据类型. 接口测试的优点:能在稍微偏底层的地方发现bug,越底层发 ...
- win7 64位环境下,为python2.7 安装pip
第一步: 安装python并配置好环境变量 参见:http://blog.csdn.net/donggege214/article/details/52062855 第二步: 下载setuptools ...
- Web 安全入门-书籍及建议
https://www.jianshu.com/p/6dcebd54fb24 (本文源于转载或摘抄整理) 2016-06-12 Fooying 优主张 最近比较忙,灵感稍微有点缺乏,本着宁缺毋滥的想法 ...
- Java create azure web app
create a certificate <java-install-dir>/bin/ keytool -genkey -alias <keystore-id> -keyst ...
- python Exception
1.except:用来捕捉异常,如果没有捕捉到,则向上层exception传递 2.finally:用来保证其代码一定会执行,可以做收尾工作,比如关闭文件等等. 3.在with as 中, 4.try ...
- Qt 学习之路 2(68):访问网络(4)
Home / Qt 学习之路 2 / Qt 学习之路 2(68):访问网络(4) Qt 学习之路 2(68):访问网络(4) 豆子 2013年11月7日 Qt 学习之路 2 19条评论 前面几章我们了 ...
- DP小小结
入门题 : [Luogu1441]砝码称重 , [NOIP2015]子串 [AHOI2009]中国象棋 , 详见代码 [HNOI2007]梦幻岛宝珠 , 详见代码 [NOIP2012]开车旅行 , 没 ...
- [BZOJ 1489][HNOI2009]双递增序
传送门 满满的负罪感,昨晚的刷题历程:写几道难题吧-->算了,还是只切道水题吧-->RNG赢了...... 背包一下就行了 #include <bits/stdc++.h> u ...
- Kibana源码分析--Hapijs路由设置理解笔记
[ES6解构赋值]:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_ ...
- bzoj2212 Tree Rotations 线段树合并+动态开点
题目传送门 思路: 区间合并线段树的题,第一次写,对于一颗子树,无论这个子树怎么交换,都不会对其他子树的逆序对造成影响,所以就直接算逆序对就好. 注意叶子节点是1到n的全排列,所以每个权值都只会出现1 ...