【问题标题】:ConfigurationErrorsException using WCF serviceModel for Addin VS2008ConfigurationErrorsException 使用 WCF serviceModel 用于 Addin VS2008
【发布时间】:2010-12-07 15:27:08
【问题描述】:

我有一个调用外部 Web 服务的 DLL(vs2008 中的库项目)。项目具有对外部 Web 服务的服务引用

我在单元测试项目中有单元测试和app.config(带有servicemodel配置),一切正常。

现在,我使用 Addin VS 2008,并且没有像 Windows Forms 或 Asp.net 这样的配置文件。 插件是一个dll,它有配置文件。

如果我使用 WCF(使用我的项目 DLL),则找不到配置 system.servicemodel

我看过这个: http://vassiltonev.blogspot.com/2009/03/loading-custom-config-file-instead-of.html

但添加自定义 wcf 行为扩展会导致 ConfigurationErrorsException

无法加载为扩展“customTextMessageEncoding”注册的类型“Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, CalidadCodigo.Integracion.CustomTextEncoder”。 (E:\TFS\pro\AddIn\bin\Debug\MyAddIn.dll.config 第 123 行

我在我的扩展 WCF 中使用 Assembly QualifiedName 进行测试但错误。

还有其他建议或示例代码吗?

我的配置

<extensions>
  <bindingElementExtensions>
    <add name="customTextMessageEncoding"
         type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement,CalidadCodigo.Integracion.CustomTextEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </bindingElementExtensions>

</extensions>

代码

    internal static WebServicePortTypeClient CrearClienteWCF()
            {
                try
                {
                    return new WebServicePortTypeClient();
                }
                catch (Exception ex)
                {

                    //TODO: not found serviceModel config

 var addInConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);


                var endpointAddress = addInConfig.AppSettings.Settings[EasyVistaSvcEndPointAddress].Value;
                var endpoint = new System.ServiceModel.EndpointAddress(endpointAddress);

                return new WebServicePortTypeClient(EndPointConfigurationName, endpoint);

                // The type 'Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, CalidadCodigo.Integracion.CustomTextEncoder' registered for extension 'customTextMessageEncoding' could not be loaded. (E:\TFS\pro\AddIn\bin\Debug\MyAddIn.dll.config line 123)


                }
            }

【问题讨论】:

    标签: c# visual-studio-2008 wcf add-in configuration-files


    【解决方案1】:

    AFAIK 无法在 DLL 中使用 ConfigurationManager。我在为 VS2010 编写插件时遇到了同样的问题。

    我的解决方案是从文件中加载设置,然后自己在代码中创建端点和端点地址,如下所示:

    Uri myUri = loadUriFromFile();
    
    var endpoint = new EndpointAddress(myUri); 
    
    NetTcpBinding binding = GetNewTcpBindingFromFile(); 
    
    return new WebServicePortTypeClient(binding, endpoint);
    

    【讨论】:

    • 如何以编程方式创建 CustomBinding 和扩展??
    • 您可以查看 Visual Studio 在 app.config 中为 wcf 服务生成的 xml 代码。序列化的对象很容易看到你必须填写什么属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多