6

我的 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 服务)的第二篇文章,我的错是我试图混合两种方式!

我将添加此作为答案。

4

3 回答 3

7

首先,您需要使用以下任一方法打开配置文件

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);
}

请注意,这些是 type 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);
}
于 2012-12-25T22:26:04.350 回答
6

使用配置文件配置服务

您不需要这种方式,您应该让主机自动从配置文件中获取其配置,而不是手动提供它们,阅读这篇文章(使用配置文件配置服务),它会帮助你,我已经将我的服务托管在C# 中的一行,配置中的几行。

这是关于(在代码中配置 WCF 服务)的第二篇文章,我的错是我试图混合两种方式!

于 2012-12-29T16:43:45.647 回答
1

虽然这是一个老问题,但我在其他地方找不到我需要的东西时遇到了问题,所以为了将来参考这里是我的解决方案。

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,在进行单元测试时很糟糕。

于 2015-09-22T12:50:33.193 回答