在 WCF 中使用命名管道的 C# 异步调用

标签 c# wcf asynchronous pipe svcutil.exe

我有一个使用命名管道绑定(bind)创建的 WCF 主机:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
}

// Step 1: Create service class that implements the service contract.
public class CalculatorService : ICalculator
{
    // Step 2: Implement functionality for the service operations.
    public double Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        // Code added to write output to the console window.
        Console.WriteLine("Return: {0}", result);
        return result;
    }
}
class Program
{
    static void Main(string[] args)
    {

        // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
        Uri baseAddress = new Uri("net.pipe://localhost");

        // Step 2 of the hosting procedure: Create ServiceHost
        ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
        try
        {
            selfHost.AddServiceEndpoint(
                typeof(ICalculator),
                new NetNamedPipeBinding(),
                "Calc");
            // Step 4 of the hosting procedure: Enable metadata exchange.
            ServiceMetadataBehavior SMB = new ServiceMetadataBehavior();
            selfHost.Description.Behaviors.Add(SMB);
            selfHost.AddServiceEndpoint(typeof(IMetadataExchange),
                MetadataExchangeBindings.CreateMexNamedPipeBinding(),
                "mex");

            // Step 5 of the hosting procedure: Start (and then stop) the service.
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }
    }
  }
}

此主机启动正确。现在,我希望能够异步调用 Add 方法。因此我使用 svcutil:

svcutil.exe /config:app.config /out:generatedProxy.cs net.pipe://localhost /a /tcv:Version35

代理客户端生成,我使用这个实现:

namespace Microsoft.ServiceModel.Samples
{
class Program
{
    static void Main(string[] args)
    {
        CalculatorClient client = new CalculatorClient();
        Console.ReadLine();
        double value1 = 5;
        double value2 = 3;
        double result = client.Add(value1, value2);
        Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

        Console.WriteLine("Now async");

        value1 = 100.00D;
        value2 = 15.99D;
        client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
        client.AddAsync(value1, value2);
        Console.WriteLine("Add({0},{1})", value1, value2);
        client.Close();
    }
    // Asynchronous callbacks for displaying results.
    static void AddCallback(object sender, AddCompletedEventArgs e)
    {
        Console.WriteLine("Add Result async: {0}", e.Result);
    }
}
}

运行客户端时出现以下异常: System.ServiceModel.CommunicationException: 服务器没有提供有意义的回复 问题是,当将此示例配置为 http 绑定(bind)时,一切都很好,所以我想在使用异步命名管道方法调用时必须遵守某些规则?!

谢谢,于尔根

最佳答案

关于在 WCF 中使用命名管道的 C# 异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597108/

相关文章:

c# - Controller 生成很少的线程,尽管没有被询问

c# - RESTful WCF 服务中标志枚举的默认值

javascript - 使用 Javascript Fetch API 对异步获取的数据进行排序

javascript - 发生错误后,Axios “get”仍会转到 “then”

c# - 覆盖 ASP.net 中的母版页功能

c# - 使用其关系中的属性为子类建模的正确方法

值类型上的 WCF DataMember EmitDefaultValue? (但设置我自己的默认值)

ios - View 加载时自动异步运行 UITableView 刷新功能

c# - 如何在 LINQ 中通过索引连接两个集合

wcf - WCF ClaimsAuthenticationManager 中的依赖注入(inject)