2017-06-15 201 views
2

我将.NET Framework应用程序移植到.NET Core中。我已经通过NuGet System.ServiceModel.Web添加,但它似乎不起作用。我需要一个替代“WebGet”:System.ServiceModel.Web .NET核心

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

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

} 

我该怎么办?

+0

你能不能给我们一个[MCVE ]?带有一个GET方法的简单控制器类,您尝试过的方法无效。 – pcdev

+0

这是一个界面。我已经提供了更多的代码。 – Gicminos

回答

3

正如@Thomas指出的那样,WebGet早已被用于创建REST API的更好的框架所取代。如果还没有,请在VS2015/VS2017中创建一个新的.Net Core Web Api项目,运行它,然后查看它与旧的WCF方法的不同之处。您会注意到需要更少的样板代码和装饰。 Here's WCF和ASP.NET Web API之间的一些差异的概要,而.Net Core实际上就是下一代。

下面是来自工作控制器类的一些代码的更全面的例子。如果需要,可以将此抽象为一个接口,但there's probably no point。另外请注意缺少[ServiceContract][OperationContract]装饰品等等。只需指定[Route(...)](可选 - 如果控制器不符合默认路由)以及使用[HttpGet(...)]等的方法和Uri路径。

此代码还假定一些内容,例如向DI注册的相关性容器(​​和ICustomerRepository)。请注意.Net Core内置了依赖注入,这是一个很好的功能(Quick rundown)。

最后,我还建议如果您还没有使用Swagger。我来晚了就这一个党,但近来使用它了,它是API开发的福音(广泛的评论如下助攻使扬鞭更有用):

[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}")); 
      } 
     } 
    } 
+0

和'ResponseFormat = WebMessageFormat.Json'? – Gicminos

+1

如果您在任何地方使用JSON,则在控制器中不需要。查看[这个问题]的答案(https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using- Chrome/26068063#26068063)在使用Chrome浏览器时显式返回json,否则确保在请求中设置正确的“Content-Type”标头。 – pcdev

+0

另外请确保您看到我所做的关于不需要创建界面的编辑。这并不是Web Api设计的方式。 – pcdev

0

.NET Standard或.NET Core不支持System.ServiceModel.Web。一般而言,对于不推荐使用的WCF技术堆栈尤其如此。

幸运的是,WCF的WebGet概念是尝试通过WCF堆栈来覆盖HTTP REST接口。但是最终被ASP.NET WebApi和后来的ASP.NET Core所取代。

使用ASP.NET Core创建相同的界面。

+0

你能举个例子吗? – Gicminos

+0

我认为ASP.NET API的每个介绍教程都可以满足您的需求。 Dotnet新的示例项目将会执行。 – Thomas