System.ServiceModel.Web .NET Core [英] System.ServiceModel.Web .NET Core

查看:347
本文介绍了System.ServiceModel.Web .NET Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将.NET Framework应用程序移植到.NET Core中。我是通过NuGet System.ServiceModel.Web添加的,但似乎无法正常工作。我需要 WebGet的替代方法:

I'm porting a .NET Framework application into .NET Core. I've added via NuGet System.ServiceModel.Web but it seem not work. I need an alternativ to "WebGet":

[ServiceContract]
public interface IChannelsApi
{
    [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json), OperationContract]
    List<Channel> GetChannels();

    [WebGet(UriTemplate = "{name}", ResponseFormat = WebMessageFormat.Json), OperationContract]
    Channel GetChannel(string name);

}

我该怎么办?

推荐答案

正如@Thomas所指出的那样,WebGet长期以来被创建REST API的更好框架所取代。如果还没有,请在VS2015 / VS2017中创建一个新的.Net Core Web Api项目,运行它,然后查看它与旧的WCF方法有何不同。您会注意到,所需的样板代码和装饰要少得多。 这里简要介绍了WCF和ASP.NET Web API之间的一些差异,而.Net Core实际上只是下一代的

As @Thomas noted, WebGet has been long superseded with much better frameworks for creating REST APIs. If you haven't already, go and create a new .Net Core Web Api project in VS2015 / VS2017, run it, then see how it differs to the old WCF method. You'll notice that a lot less boilerplate code and decorating is required. Here's a rundown of some differences between WCF and ASP.NET Web API, and .Net Core is really just the next generation of this.

下面是一个工作控制器类代码的更全面示例。如果需要,您可以将其抽象到一个界面中,但是可能没有意义。还请注意,缺少 [ServiceContract] [OperationContract] 装饰等等。只需指定 [Route(...)] (可选-如果控制器不符合默认路由),并指定使用<$ c的方法和Uri路径$ c> [HttpGet(...)] ,等等。

Below is a more comprehensive example of some code from a working controller class. If required, you can abstract this into an interface, but there's probably no point. Also notice the lack of [ServiceContract] and [OperationContract] decorations, among other things. Just specify the [Route(...)] (optional - if the controller doesn't conform to the default route), and the method and Uri path using [HttpGet(...)], etc.

此代码还假设了一些东西,例如向DI容器( ILogger ICustomerRepository )。请注意.Net Core内置了依赖项注入功能,这是一个很好的功能(快速运行)。

This code also assumes a few things such as dependencies being registered with the DI container (ILogger and ICustomerRepository). Note that .Net Core has dependency injection built in, which is a nice feature (Quick rundown).

最后,我还建议使用摇摇晃晃(如果您还没有的话)。我参加这个聚会很晚,但是最近一直在使用它,这对API开发是一个福音(下面的大量评论有助于使Swagger更加有用):

Finally, I also recommend using Swagger if you are not already. I'm late to the party on this one but have been using it lately and it is a boon for API development (the extensive commenting below assists in making Swagger more useful):

    [Route("api/[controller]")]
    public class CustomersController : Controller
    {
        ILogger<CustomersController> log;
        ICustomerRepository customerRepository;

        public CustomersController(ILogger<CustomersController> log, ICustomerRepository customerRepository)
        {
            this.log = log;
            this.customerRepository = customerRepository;
        }

        /// <summary>
        /// Get a specific customer 
        /// </summary>
        /// <param name="customerId">The id of the Customer to get</param>
        /// <returns>A customer  with id matching the customerId param</returns>
        /// <response code="200">Returns the customer </response>
        /// <response code="404">If a customer  could not be found that matches the provided id</response>
        [HttpGet("{customerId:int}")]
        [ProducesResponseType(typeof(ApiResult<Customer>), 200)]
        [ProducesResponseType(typeof(ApiResult), 404)]
        public async Task<IActionResult> GetCustomer([FromRoute] int customerId)
        {
            try
            {
                return Ok(new ApiResult<Customer>(await customerRepository.GetCustomerAsync(customerId)));
            }
            catch (ResourceNotFoundException)
            {
                return NotFound(new ApiResult($"No record found matching id {customerId}"));
            }
        }
    }

这篇关于System.ServiceModel.Web .NET Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆