在浏览器中查看vs2017发布的WCF

 

目录

IService.cs接口

Service.cs实现 

Service.svc

Web.config

运行

 

从开发的角度比较 ASP.NET Web 服务与 WCF


在Web项目中添加WCF新项

IService.cs接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
[ServiceContract]
public interface IService
{
    [OperationContract]
    void DoWork();
    
    //GET操作
    [OperationContract]
    [WebGet(UriTemplate = "")]
    List<Person> GetAllPerson();


    [OperationContract]
    [WebGet(UriTemplate = "{id}", ResponseFormat = WebMessageFormat.Json)]
    Person GetPerson(string id);
 
}
[DataContract]
public class Person
{
    [DataMember]
    public string ID;

    [DataMember]
    public string Name;

    [DataMember]
    public string Age;
}

Service.cs实现 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service”。


public class Service : IService
{
    public void DoWork()
    {
    }

    List<Person> persons = new List<Person>
    {
       new Person{ID = "1", Age = "20", Name = "张三"},
       new Person{ID = "2", Age = "23", Name = "李四"},
    };
    
    public List<Person> GetAllPerson()
    {
        return persons.ToList();
    }

    public Person GetPerson(string id)
    {
        return persons.FirstOrDefault(e => e.ID.Equals(id));
    }
}

Service.svc

添加Factory="System.ServiceModel.Activation.WebServiceHostFactory

我的理解就是创建实例,否则不能在浏览器中运行

官方解释:

https://docs.microsoft.com/zh-cn/dotnet/api/system.servicemodel.activation.webservicehostfactory?redirectedfrom=MSDN&view=netframework-4.8

<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

Web.config

添加

          <endpointBehaviors>
            <behavior>
              <webHttp helpEnabled="true"/>
            </behavior>
          </endpointBehaviors>

 将Web.config中的 <webHttp /> 修改为 <webHttp helpEnabled="true" />将可以在浏览器页面中列举出可用接口,并提供提交的数据样例。

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.6.1"/>
      <httpRuntime targetFramework="4.6.1"/>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
    </system.codedom>

    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>


          <endpointBehaviors>
            <behavior>
              <webHttp helpEnabled="true"/>
            </behavior>
          </endpointBehaviors>
          
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
</configuration>

运行

http://localhost:1979/Service.svc/?xsd=xsd0 

引用

在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API。在.net平台下,你有很多的选择来构建一个HTTP Services。我分享一下我对Web Service、WCF以及Web API的看法。

  Web Service

  1、它是基于SOAP协议的,数据格式是XML

  2、只支持HTTP协议

  3、它不是开源的,但可以被任意一个了解XML的人使用

  4、它只能部署在IIS上

 

  WCF

  1、这个也是基于SOAP的,数据格式是XML

  2、这个是Web Service(ASMX)的进化版,可以支持各种各样的协议,像TCP,HTTP,HTTPS,Named Pipes, MSMQ.

  3、WCF的主要问题是,它配置起来特别的繁琐

  4、它不是开源的,但可以被任意一个了解XML的人使用

  5、它可以部署应用程序中或者IIS上或者Windows服务中

 

  WCF Rest

  1、想使用WCF Rest service,你必须在WCF中使用webHttpBindings

  2、它分别用[WebGet]和[WebInvoke]属性,实现了HTTP的GET和POST动词

  3、要想使用其他的HTTP动词,你需要在IIS中做一些配置,使.svc文件可以接受这些动词的请求

  4、使用WebGet通过参数传输数据,也需要配置。而且必须指定UriTemplate

  5、它支持XML、JSON以及ATOM这些数据格式

 

  Web API

  1、这是一个简单的构建HTTP服务的新框架

  2、在.net平台上Web API 是一个开源的、理想的、构建REST-ful 服务的技术

  3、不像WCF REST Service.它可以使用HTTP的全部特点(比如URIs、request/response头,缓存,版本控制,多种内容格式)

  4、它也支持MVC的特征,像路由、控制器、action、filter、模型绑定、控制反转(IOC)或依赖注入(DI),单元测试。这些可以使程序更简单、更健壮

  5、它可以部署在应用程序和IIS上

  6、这是一个轻量级的框架,并且对限制带宽的设备,比如智能手机等支持的很好

  7、Response可以被Web API的MediaTypeFormatter转换成Json、XML 或者任何你想转换的格式。

  

  WCF和WEB API我该选择哪个?

  1、当你想创建一个支持消息、消息队列、双工通信的服务时,你应该选择WCF

  2、当你想创建一个服务,可以用更快速的传输通道时,像TCP、Named Pipes或者甚至是UDP(在WCF4.5中),在其他传输通道不可用的时候也可以支持HTTP。

  3、当你想创建一个基于HTTP的面向资源的服务并且可以使用HTTP的全部特征时(比如URIs、request/response头,缓存,版本控制,多种内容格式),你应该选择Web API

  4、当你想让你的服务用于浏览器、手机、iPhone和平板电脑时,你应该选择Web API

Fiddler4调试时  结构体  【1,2,3】这么传

从开发的角度比较 ASP.NET Web 服务与 WCF

官方文档:

https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/comparing-aspnet-web-services-to-wcf-based-on-development

猜你喜欢

转载自blog.csdn.net/qq_41664159/article/details/104755662