System.ServiceModel.Web .NET Core

5

我正在将一个.NET Framework 应用程序移植到.NET Core。我通过NuGet添加了 System.ServiceModel.Web 但似乎没有起作用。我需要寻找 "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);

}

What I have to do?


你能给我们提供一个 [mcve] 吗?一个简单的控制器类,其中包含一个你尝试过但不起作用的 GET 方法。 - pcdev
这是一个接口。我为您提供了更多的代码。 - Gicminos
2个回答

5
如@Thomas所说,WebGet已经被更好的框架取代,用于创建REST API。如果您还没有,请在VS2015 / VS2017中创建一个新的.Net Core Web Api项目,运行它,然后查看它与旧的WCF方法的区别。你会注意到需要的样板代码和装饰要少得多。 这里是一些WCF和ASP.NET Web API之间差异的概述,而.Net Core实际上只是这个的下一代。
以下是来自工作控制器类的一些代码的更全面的示例。如果需要,您可以将其抽象成接口,但可能没有必要。还请注意,除其他外,缺少[ServiceContract]和[OperationContract]装饰。只需使用[HttpGet(...)]等指定[Route(...)](可选-如果控制器不符合默认路由)的方法和Uri路径即可。
此代码还假定某些依赖项已在DI容器中注册(ILogger和ICustomerRepository)。请注意,.Net Core内置了依赖项注入,这是一个很好的功能(快速概述)。
最后,我还建议您使用Swagger(如果您还没有使用)。我在这方面很迟,但最近一直在使用它,这对于API开发是一个福音(下面的广泛评论有助于使Swagger更有用):
    [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}"));
            }
        }
    }

ResponseFormat = WebMessageFormat.Json 呢? - Gicminos
1
如果您在所有地方都使用JSON,则不需要在控制器中进行设置。请参阅此问题的答案,以在Chrome浏览时显式返回json,否则只需确保在请求中设置正确的“Content-Type”标头即可。 - pcdev
还要确保您看到我关于不需要创建接口的编辑。这并不是Web Api的设计方式。 - pcdev
1
学习ASP.NET。在将某些东西放入网络之前,有数百个细节需要考虑,超出了这个示例中的三到四个功能。 - Thomas

3

System.ServiceModel.Web在.NET Standard或.NET Core上不受支持。这对于一般已弃用的WCF技术栈尤其如此。

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

使用ASP.NET Core创建相同的接口。


1
你能给我一个例子吗? - Gicminos
我认为每个关于ASP.NET Core Web API使用的入门教程都可以满足您的需求。使用dotnet new示例项目即可。 - Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接