Windsor WCF客户端 - "找不到默认端点元素"在配置中不存在时

时间:2014-08-22 14:02:35

标签: c# wcf castle-windsor windsor-facilities

我无法让Windsor WCF工具生成客户端端点而不在Web / App.config文件中明确定义它们。

我的合同,客户端和回调是作为服务引用生成的,我想以编程方式注册所有内容而不使用配置文件。但是,当App / Web.config中没有端点信息时,我收到此错误:

  

错误:找不到引用的默认端点元素   合同' ServiceReference1.IWcfContract'在ServiceModel客户端中   配置部分。这可能是因为没有配置文件   找到您的应用程序,或者因为没有端点元素匹配   这个合同可以在客户元素中找到。

注册

Component.For<IWcfContract>()
    .ImplementedBy<WcfContractClient>()
    .AsWcfClient(new DefaultClientModel(
        WcfEndpoint
            .ForContract<IWcfContract>()
            .BoundTo(MyConfig.NetTcpBinding)
            .At(MyConfig.WcfHostAddressAndPort)),
    .LifestyleTransient());
Component.For<IWcfContractClientFactory>()
    .AsFactory(new WcfContractClientFactorySelector())

键入工厂

IWcfContractCreate(WcfContractClientCallback callback);
void Release(IWcfContractinstance);

工厂选择器

public class WcfContractClientFactorySelector : DefaultTypedFactoryComponentSelector
{
    protected override IDictionary GetArguments(MethodInfo method, object[] arguments)
    {
        Arguments args = new Arguments();
        args.Add("callbackInstance", new InstanceContext(arguments[0]));
        return args;
    }
}

解析客户

IWcfContractClientFactory factory = container.Resolve<IWcfContractClientFactory>();
IWcfContract client = factory.Create(new WcfContractClientCallback());

此时,如果我在Web / App.config文件中有端点信息,那么一切正常。如果我把它拿出来,我会收到上面提到的错误。谢谢!

1 个答案:

答案 0 :(得分:0)

此问题是由自动生成的服务引用类中的客户端构造函数引起的。客户端有五个构造函数,其中四个正在查找配置文件。出于某种原因,Windsor的WCF工厂正在使用这些构造函数。此外,这些构造函数位于标记为DebuggerStepThrough的类中,因此异常从调试器中隐藏。

决议是让Windsor使用最后一个构造函数,它不需要配置文件,但我必须删除.AsWcfClient。如果有人知道如何使用.AsWcfClient而没有遇到此构造函数问题,请发布另一个答案,我会接受它。感谢。

服务参考构造函数

    public SearchServiceContextClient(InstanceContext callbackInstance)
    public SearchServiceContextClient(InstanceContext callbackInstance, string endpointConfigurationName)
    public SearchServiceContextClient(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress)
    public SearchServiceContextClient(InstanceContext callbackInstance, string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
    public SearchServiceContextClient(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress)

<强>注册

Component.For<IWcfContract>()
    .ImplementedBy<WcfContractClient>()
    .DependsOn(new
    {
        binding = MyConfig.NetTcpBinding,
        remoteAddress = new EndpointAddress(MyConfig.WcfHostAddressAndPort)
    })
.LifestyleTransient()),
Component.For<IWcfContractClientFactory>()
    .AsFactory(new WcfContractClientFactorySelector())