我在我的app.config此配置:
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
我想从我的桌面应用程序公开此服务:
我定义了主机实例:
ServiceHost host = new ServiceHost(typeof(MyType), new Uri("http://" + hostName + ":" + port + "/MyName"));
然后,添加端点与它的结合:
var binding = new BasicHttpBinding("myBinding");
host.AddServiceEndpoint(typeof(IMyInterface), binding, "MyName");
现在,我想用一些代码,读取配置文件命名myBehavior行为来代替如下因素代码,而不是硬编码的行为的选项。
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };
host.Description.Behaviors.Add(smb);
// Enable exeption details
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;
然后,我可以打开主机。
host.Open();
*编辑*
配置服务使用配置文件
你不应该需要这样,你应该让主机需要它的配置自动地从配置文件,而不是手动给他们,请阅读这篇文章(配置使用配置文件服务) ,它会帮助你,我主持我的服务在C#和配置很少的人一行。
这是关于(配置WCF服务的代码)第二条 ,我的错在于我试图两种方式混用!
我将其添加为一个答案。
首先,你需要或者使用打开配置文件
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
要么
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "app.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
然后你读的行为:
var bindings = BindingsSection.GetSection(config);
var group = ServiceModelSectionGroup.GetSectionGroup(config);
foreach (var behavior in group.Behaviors.ServiceBehaviors)
{
Console.WriteLine ("BEHAVIOR: {0}", behavior);
}
请注意,这些类型的System.ServiceModel.Configuration.ServiceBehaviorElement
,所以不是你所需的东西呢。
如果你不介意使用私有API,您可以调用保护法BehaviorExtensionElement.CreateBehavior()
通过反射:
foreach (BehaviorExtensionElement bxe in behavior)
{
var createBeh = typeof(BehaviorExtensionElement).GetMethod(
"CreateBehavior", BindingFlags.Instance | BindingFlags.NonPublic);
IServiceBehavior b = (IServiceBehavior)createBeh.Invoke(bxe, new object[0]);
Console.WriteLine("BEHAVIOR: {0}", b);
host.Description.Behaviors.Add (b);
}
配置服务使用配置文件
你不应该需要这样,你应该让主机需要它的配置自动地从配置文件,而不是手动给他们,请阅读这篇文章(配置使用配置文件服务) ,它会帮助你,我主持我的服务在C#和配置很少的人一行。
这是关于(配置WCF服务的代码)第二条 ,我的错在于我试图两种方式混用!
尽管这是一个老问题,我有问题,找到我所需要的其他地方,所以以供将来参考,这里是我的解决方案。
var behaviorSection = ConfigurationManager.GetSection("system.serviceModel/behaviors") as BehaviorsSection;
if (behaviorSection != null)
{
// for each behavior, check for client and server certificates
foreach (EndpointBehaviorElement behavior in behaviorSection.EndpointBehaviors)
{
foreach (PropertyInformation pi in behavior.ElementInformation.Properties)
{
if (pi.Type == typeof(ClientCredentialsElement))
{
var clientCredentials = pi.Value as ClientCredentialsElement;
var clientCert = clientCredentials.ClientCertificate;
// TODO: Add code...
}
}
}
}
ConfigurationManager.Open...Configuration()
不与Web项目很好地工作,所以手动取得部和铸造更简单。
如果你是在让真正坚持System.ServiceModel
做配置阅读你就可以做这样的事情:
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "web.config"); // the root web.config
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var group = ServiceModelSectionGroup.GetSectionGroup(config);
foreach (EndpointBehaviorElement item in group.Behaviors.EndpointBehaviors)
{
// TODO: add code...
}
第二个方法是使用HttpContext.Current
,而我们都知道HttpContext.Current
做单元测试的时候是可怕的。
文章来源: Reading WCF behavior elements from config file programmticlally when exposing self-hosted service