我的要求是我想访问我开发的服务器之外的wcf服务。我需要做自设,我们没有IIS,所以我需要做tcp托管。 Windows服务主机创建了一些问题。我想从另一台机器访问它。 请分享任何链接或演示应用程序将是伟大的。
答案 0 :(得分:0)
我创建了一个帮助方法来托管我的WCF服务,将URI作为参数,这是方法:
public ServiceHost CreateService(Uri baseAddress)
{
// create the net.tcp binding for the service endpoint
NetTcpBinding ntcBinding = new NetTcpBinding();
ntcBinding.Security.Mode = SecurityMode.None;
System.ServiceModel.Channels.Binding tcpBinding = ntcBinding;
// create the service host and add the endpoint
ServiceHost host = new ServiceHost(typeof(RMS.Gateway.Services.RiskLinkService), baseAddress);
host.Opening += new EventHandler(host_Opening);
host.Opened += new EventHandler(host_Opened);
host.Closing += new EventHandler(host_Closing);
host.Closed += new EventHandler(host_Closed);
host.Faulted += new EventHandler(host_Faulted);
host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
//smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Add MEX endpoint
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexTcpBinding(),
"mex"
);
host.AddServiceEndpoint(typeof(RAE.Entities.ServiceInterfaces.IRiskLinkService), ntcBinding, string.Empty);
host.Open();
return host;
}
然后从测试控制台应用程序我使用此方法启动托管,它可以在Windows服务内部以类似的方式完成,在生产中我们现在将进行简单调试我们在控制台应用程序中托管:)
Uri baseAddress = new Uri(ConfigurationManager.AppSettings["serviceURL"]);
// Create the ServiceHost.
using (ServiceHost host = serviceFactory.CreateService(baseAddress))
{
System.Console.WriteLine("The service is ready at {0}", baseAddress);
System.Console.WriteLine("Press <Enter> to stop the service.");
System.Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
正如您所见,NetTcpBinding是在运行时从代码创建的,它可以工作,我们可以访问和使用网络中其他机器的WCF端点。
答案 1 :(得分:0)
希望Microsoft的step by step指南可以帮助您托管wcf服务并使用该服务。