簡體   English   中英

C#在WCF中使用命名管道進行異步調用

[英]C# Asynchron call with Named Pipes in WCF

我有一個使用命名管道綁定創建的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綁定時一切都很好,所以我猜工作時必須遵守一些事情用異步命名管道方法調用?!

謝謝,Juergen

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM