System.ServiceModel.Web .NET 核心 [英] 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(...)]
(可选 - 如果控制器不符合默认路由),以及使用 [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).
最后,如果您还没有使用 Swagger,我还建议您使用.我迟到了,但最近一直在使用它,这对 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 核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!