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

时间:2018-10-29 14:34:18

标签: wcf asp.net-core net.tcp

我正在构建一个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.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 个答案:

答案 0 :(得分:0)

所以事实证明这很简单。我发现了这篇帖子How to programmatically connect a client to a wcf service

容易