Ham*_*ari 5 c# wcf windows-services named-pipes
我收到以下错误:
无法侦听管道名称“net.pipe://localhost/ServiceModelSamples/service/”,因为另一个管道端点已在侦听该名称
我正在从另一个 WCF 调用中调用 Windows 服务内的 WCF 自托管服务,如下所示。
$
ServiceHost _host;
var guidid = Guid.NewGuid().ToString() ;
_host = new ServiceHost(typeof(CalculatorService), new Uri[] { new Uri("net.pipe://localhost/" + guidid)});
_host.AddServiceEndpoint(typeof(ICalculator),
new NetNamedPipeBinding(),
"");
_host.Open();
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(
new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),
new EndpointAddress("net.pipe://localhost/" + guidid));
ICalculator proxy = factory.CreateChannel();
proxy.SendPin(pin);
((IClientChannel)proxy).Close();
factory.Close();
Run Code Online (Sandbox Code Playgroud)
自托管 WCF 服务
$
namespace PINSender
{
// Define a service contract.
public interface ICalculator
{
[OperationContract]
void SendPin(string pin);
}
// Implement the ICalculator service contract in a service class.
public class CalculatorService : ICalculator
{
// Implement the ICalculator methods.
public void SendPin(string pin)
{
}
}
public class CalculatorWindowsService : ServiceBase
{
public ServiceHost serviceHost = null;
public CalculatorWindowsService()
{
// Name the Windows Service
ServiceName = "PINSenderService";
}
public static void Main()
{
ServiceBase.Run(new CalculatorWindowsService());
}
// Start the Windows service.
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(CalculatorService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
// Provide the ProjectInstaller class which allows
// the service to be installed by the Installutil.exe tool
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "PINSenderService";
Installers.Add(process);
Installers.Add(service);
}
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序配置
$
ServiceHost _host;
var guidid = Guid.NewGuid().ToString() ;
_host = new ServiceHost(typeof(CalculatorService), new Uri[] { new Uri("net.pipe://localhost/" + guidid)});
_host.AddServiceEndpoint(typeof(ICalculator),
new NetNamedPipeBinding(),
"");
_host.Open();
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>(
new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),
new EndpointAddress("net.pipe://localhost/" + guidid));
ICalculator proxy = factory.CreateChannel();
proxy.SendPin(pin);
((IClientChannel)proxy).Close();
factory.Close();
Run Code Online (Sandbox Code Playgroud)