WCF开发入门

.net 的 wcf 作为web服务特别适合给第三方使用
wcf 创建的接口同时支持Post和webservice两种访问方式, 同时支持json和xml两个数据格式
而且直接生成了接口的帮助文档,特别适合给第三方提供接口

1,用Microsoft Visual Studio创建wcf服务项目

2,添加一个Global.asax类

包含两个文件:
Global.asax:

<%@ Application Codebehind="Global.asax.cs" Inherits="WcfGetAndPost.Global" Language="C#" %>

Global.asax.cs

using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;

namespace WcfGetAndPost
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }
    }
}

可以右键添加,可以手动添加(新建上面两个文件,包含到项目里面就可以)

3,修改web.config以便显示帮助文档

在 <system.serviceModel> 节点下面添加节点如下:

<standardEndpoints>
 <webHttpEndpoint>
   <!-- 
         Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
         via the attributes on the <standardEndpoint> element below
     -->
   <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
 </webHttpEndpoint>
</standardEndpoints>

运行项目, 打开帮助文档 http://localhost:54407/Service1/help 就能够看到接口说明了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值