我创建了一个简单的代码,允许使用WCF和NamedPipes进行跨域管理。我在Windows 8.1上测试代码,导致EndpointNotFoundException。
这是我的代码:
服务合同
[ServiceContract(Namespace = "http://PoC.AppDomainWCF")]
public interface ICrossAppDomainSvc
{
[OperationContract]
bool HasPermission(String User, String Permission);
}
Program.cs(WinForms)
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread thread = new Thread(new ThreadStart(StartService));
thread.Start();
Application.Run(new Form1());
}
static void StartService()
{
using (ServiceHost host = new ServiceHost(typeof(CrossAppDomainSvc), new Uri[] {
new Uri("http://localhost:12000/AppDomainWCF/"),
new Uri("net.pipe://localhost/")
}))
{
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
host.AddServiceEndpoint(typeof(ICrossAppDomainSvc), binding, "CrossAppDomainSvc");
// Add a MEX endpoint
//ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
//metadataBehavior.HttpGetEnabled = true;
//metadataBehavior.HttpGetUrl = new Uri("http://localhost:12001/AppDomainWCF");
//host.Description.Behaviors.Add(metadataBehavior);
host.Open();
}
}
}
客户端代码
NetNamedPipeBinding binding = new NetNamedPipeBinding();
ChannelFactory<ICrossAppDomainSvc> channelFactory = new ChannelFactory<ICrossAppDomainSvc>(binding);
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/CrossAppDomainSvc");
ICrossAppDomainSvc service = channelFactory.CreateChannel(endpointAddress);
MessageBox.Show(service.HasPermission("Juliano", "XPTO").ToString());
在service.HasPermission调用时抛出异常。
我的代码出了什么问题?
由于问题已得到解答并且我的概念验证工作正常,我已在GitHub上创建了一个存储库,以帮助任何需要允许跨appdomain通信的人。
答案 0 :(得分:0)
您打开ServiceHost并立即关闭它。 serviceHost.Open()方法不会阻塞。因此,您的端点不存在,因为在连接时主机已经关闭