如何在 asp.net core 2.1 中使用 net.tcp 服务

Posted

技术标签:

【中文标题】如何在 asp.net core 2.1 中使用 net.tcp 服务【英文标题】:How do I consume a net.tcp service in asp.net core 2.1 【发布时间】:2019-04-02 12:26:44 【问题描述】:

我正在构建一个 asp.net core 2.1 Web 应用程序,我需要调用一个旧的 net.tcp 端点来获取有关预订的详细信息。端点在另一个旧应用程序的 web.config 中配置如下:

<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="Behaviors.EndpointBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <netTcpBinding>
    <binding name="unsecuredLargeTcpBuffer" maxBufferSize="20000000" maxReceivedMessageSize="20000000">
      <readerQuotas maxArrayLength="20000000" maxBytesPerRead="20000000" maxStringContentLength="10000000" />
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>

<client>      
  <endpoint address="net.tcp://us-ap-contoso1:12345/Bookings" binding="netTcpBinding" bindingConfiguration="unsecuredLargeTcpBuffer" behaviorConfiguration="Behaviors.EndpointBehavior" contract="Contoso.Contracts.IBookingService" />     
</client>

我相信在 aspnet 核心中不能使用这个基于文件的配置,但需要以编程方式进行。我尝试使用 svcutil 生成服务引用,但这不适用于 net.tcp 服务。

我尝试使用上述配置调用 setup a web.config 文件并像这样调用端点

public Booking FindBooking(string name, string number)
    
        var proxy = new BookingSearchServiceProxy();

        try
        
            var query = new BookingFieldQuery
            
                BookingNumber = number,
                LastName = name
            ;

            Booking booking = proxy.FindSingle(query);

            return booking;
        
        catch (Exception ex)
        
            //log here
        
        finally
        
            proxy.CloseSafely();
        

        return null;
    

但是会抛出异常

System.PlatformNotSupportedException: Configuration files are not supported.

我该怎么做?

如果您能提供任何建议,我将不胜感激。

【问题讨论】:

【参考方案1】:

所以结果很简单。我找到了这个帖子How to programmatically connect a client to a wcf service

简单。

【讨论】:

以上是关于如何在 asp.net core 2.1 中使用 net.tcp 服务的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP.NET Core 2.1 中使用自动完成输入引发模式

如何在 ASP.NET Core 2.1 中获取客户端 IP 地址

如何在 ASP .NET Core 2.1 中将登录页面设为默认路由?

如何在 ASP.NET Core 2.1 中的计时器上运行 BackgroundService

ASP.NET Core 2.1 Jwt 设置自定义声明

Asp.net core 2.1 - 如何服务多个角度应用程序?