使用WCF中的命名管道进行C#异步调用
我使用命名管道绑定创建了一个WCF主机:使用WCF中的命名管道进行C#异步调用,c#,wcf,asynchronous,pipe,svcutil.exe,C#,Wcf,Asynchronous,Pipe,Svcutil.exe,我使用命名管道绑定创建了一个WCF主机: using System; using System.ServiceModel; using System.ServiceModel.Description; namespace Microsoft.ServiceModel.Samples { // Define a service contract. [ServiceContract] public interface ICalculator { [OperationContract]
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();
}
}
}
}
代理客户端已生成,我使用以下实现:
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);
}
}
}
名称空间Microsoft.ServiceModel.Samples
{
班级计划
{
静态void Main(字符串[]参数)
{
CalculatorClient=新CalculatorClient();
Console.ReadLine();
双值1=5;
双值2=3;
双重结果=client.Add(value1,value2);
WriteLine(“Add({0},{1})={2}”,value1,value2,result);
WriteLine(“现在是异步的”);
值1=100.00D;
值2=15.99D;
client.AddCompleted+=新事件处理程序(AddCallback);
AddAsync(value1,value2);
WriteLine(“Add({0},{1})”,value1,value2;
client.Close();
}
//用于显示结果的异步回调。
静态void AddCallback(对象发送方,AddCompletedEventArgs e)
{
WriteLine(“addresult异步:{0}”,e.Result);
}
}
}
运行客户端时,我会遇到以下异常:
System.ServiceModel.CommunicationException:服务器未提供有意义的答复
问题是,当将这个示例配置为http绑定时,一切都很好,所以我猜在使用异步命名管道方法调用时,必须遵守一些规定
谢谢,尤尔根,通读一遍
这个答案可能会有帮助。阅读文章@HungryMind谢谢底部的评论,我认为问题在于在返回结果之前调用了client.Close()!我希望这是根本原因,至少它是有意义的:-)谢谢!
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);
}
}
}