wcf的简单服务建立
来源:互联网 发布:北京大学大数据学院 编辑:程序博客网 时间:2024/04/29 00:06
这是我第一次在博客写作,主要是为了记录一下自己在工作或者生活中的一些所得,希望能够用作一个备忘录吧。
首先,在VS上面建立一个项目,不用特定的建立wcf项目,直接建立一个空项目即可,废话不多说直接上代码。
这个是服务端的入口程序,因为服务比较简单,不需要手动配置,直接用代码的方式添加配置。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Configuration;using System.ServiceModel.Description;namespace OrderMenu.Wcf{ public class Program { static void Main(string [] args) { try { Uri baseAddress=new Uri("http://localhost:8001/Service"); using(ServiceHost host = new ServiceHost(typeof(Service1), baseAddress)) { WebHttpBinding binding = new WebHttpBinding(); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1),binding,baseAddress); WebHttpBehavior behavior = new WebHttpBehavior(); endpoint.Behaviors.Add(behavior); host.Opened += delegate { Console.WriteLine("服务启动"); }; host.Open(); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex); } } }}其中Service1和IService1是服务和接口,因此还要完成这两个类的编写.
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using System.ServiceModel.Web;using System.IO;namespace OrderMenu.Wcf{ [ServiceContract] public interface IService1 { [OperationContract] [WebGet( UriTemplate = "GetTest/{username}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string GetTest(string username); <pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using System.IO;namespace OrderMenu.Wcf{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。 public class Service1 : IService1 { public string GetTest(string username) { return "你好" + username; } }}
将相关的dll引进去了之后接下来就可以运行了。此处的app.config是不需要配置的。
效果大致如下:
0 0
- wcf的简单服务建立
- WCF--建立简单的WCF服务
- wcf学习--建立最简单的WCF服务
- wcf学习--建立最简单的WCF服务
- wcf学习--建立最简单的WCF服务
- 简单的wcf服务
- 建立WCF服务
- 续:建立WCF服务
- 创建一个简单的WCF服务
- 用VS2010创建简单的WCF服务
- 建立net.tcp协议的WCF服务并调用初试
- 网络编程之WCF编程:WCF服务和客户端的建立,回调
- 网络编程之WCF编程:WCF服务和客户端的建立,回调
- 网络编程之WCF编程:WCF服务和客户端的建立,回调
- 建立WCF的客户端
- 简单的WCF发布-订阅(Pub/Sub)服务
- Restfull分格的 WCF服务简单构建流程
- [WCF REST] 一个简单的REST服务实例
- Android Action总结
- PDF怎么转换成word
- 第三周 课后实践:项目五——数组作数据成员(1)
- pomelo Channel
- 算法竞赛入门经典训练指南——例18 max(a[i]-a[j])且(i<j)
- wcf的简单服务建立
- 玩转百度即用API(2)——身份证查询
- Sql Server 删除所有表
- 第四周 项目一-三角形类的构造函数(四)
- 为chromium增加底部工具栏
- Java实现uint8_t/uint16_t/uint32_t
- 【Node.js基础篇】(四)Node.js实现文件路由功能
- 站长排名限制在50内,是何节奏?
- 倾城花