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 ...
随机推荐
- 使用itchat监控微信消息,从此不再为撤回烦恼
强大的Itchat itchat是一个开源的微信个人号接口,使用python封装接入微信网页版接口,通过调用itchat来登录微信网页版收发消息. 项目简介 - itchat 掌握itchat之后,只 ...
- kali linux之skipfish,arachni
c语言编写,实验性的主动web安全评估工具,递归爬网,基于字典的探测,速度较快--(多路单线程,启发式自动内容识别),误报率低 常用参数 -I 只检查包含/xx/的url -X 不检查包含/xx/的u ...
- k8s(未完待续)
K8s简介Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群间扩展. 使用Kubernetes可以 自动化容器的部署和复制 随时扩展或收缩容器规模 将容器 ...
- 【转】VS2010不能引用System.Data.OracleClient解决方法
源地址:http://blog.csdn.net/iloli/article/details/8484674
- Ubuntu 安装后的配置及美化(二)
Ubuntu安装后的配置与美化(二) 上篇文章讲了安装ubuntu后的一系列基础的配置,已经可以满足日常的使用了,这篇文章讲一下安装 IDE 及一些其他的配置. 1.安装 SSR 下载 SSR 客户端 ...
- ant实例
<?xml version="1.0" encoding="UTF-8" ?> <project name="javaTest&qu ...
- 【问题记录】element is not attached to the page document
遇到ui脚本报错:element is not attached to the page document 解决办法,再次定位即可
- js中自己实现bind函数的方式
最近由于工作比较忙,好久都没时间静下心来研究一些东西了.今天在研究 call 和 apply 的区别的时候,看到 github 上面的一篇文章,看完以后,感觉启发很大. 文章链接为 https://g ...
- sublime text3 添加新片段
1.tools->developer->new snippet 要介绍一下snippet四个组成部分: content:其中必须包含<![CDATA[…]]>,否则无法工作, ...
- HTML嵌套php
1. <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?> 2. <scri ...