WCF和它的配置有时会让我自杀。
我有一个带插件的Windows服务,其中一个插件是WCF net.tcp双工服务(带回调)。它运作良好,直到我试图把它变成"插件",所以所有的配置都很酷。现在我无法正确托管它(我不能从我的外部项目添加服务引用)。首先,我无法将配置提供给此服务,因为config不在app.config或类似的东西中,而是在带有WCF服务dll的文件夹中的.config文件中。我发现了一些代码,如:
protected override void ApplyConfiguration()
{
string configFilename = System.IO.Path.Combine(AssemblyDirectory, CONFIG_FOLDER_NAME,
String.Format("{0}.config", this.Description.Name));
if (string.IsNullOrEmpty(configFilename) || !System.IO.File.Exists(configFilename))
base.ApplyConfiguration();
else
LoadConfigFromCustomLocation(configFilename);
}
private void LoadConfigFromCustomLocation(string configFilename)
{
var filemap = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = configFilename };
System.Configuration.Configuration config =
System.Configuration.ConfigurationManager.OpenMappedExeConfiguration
(filemap,
System.Configuration.ConfigurationUserLevel.None);
var serviceModel = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
bool loaded = false;
foreach (System.ServiceModel.Configuration.ServiceElement se in serviceModel.Services.Services)
{
if (!loaded)
if (se.Name == this.Description.ConfigurationName)
{
base.LoadConfigurationSection(se);
loaded = true;
}
}
if (!loaded)
throw new ArgumentException("ServiceElement doesn't exist");
}
但他们不能工作。所以我尝试以编程方式定义设置,如下所示:
var baseUri = new Uri("net.tcp://localhost:1488/");
var serviceUrl = "ClientService";
var serviceUri = new Uri(baseUri, serviceUrl);
//var binding = new NetTcpBinding();
BindingElement bindingElement = new TcpTransportBindingElement();
var binding = new CustomBinding(bindingElement);
using (var host = new ServiceHost(typeof(ClientService), serviceUri /*Specify full URL here*/))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = false /*Do not specify URL at all*/});
host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
host.AddServiceEndpoint(typeof(IClientService), binding, "net.tcp://localhost:228/LinProgWebServer/ClientService"
var metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange), binding, "MEX");
Console.WriteLine("Started service on cotnract {0}, ready for anything", typeof(IClientService).FullName);
host.Open();
_ResetEvent.WaitOne();
}
嗯,netstat说有些东西会监听我的端口,但我仍然无法添加服务引用。我的思绪彻底打击了。你能帮我吗?
我会感激任何帮助