Skip to main content
 首页 » 编程设计

c#之引用的 WCF 服务不使用 app.config

2024年02月13日28开发

我编写了一个在 DLL 中引用的 WCF 服务。 在我添加该服务引用后,会自动生成一个包含所需数据的 app.config。

客户端正在使用 dll 与 wcf 服务通信...没什么特别的。 但是当我尝试创建服务引用的对象时......它崩溃了,说它无法找到端点地址。

我四处搜索并通过将绑定(bind)和地址传递给服务引用来修复它:

readonly BasicHttpBinding _binding = new BasicHttpBinding(); 
readonly EndpointAddress _address = new EndpointAddress("http://localhost:50309/CustomerService.svc"); 
 
using (CustomerServiceClient client = new CustomerServiceClient(_binding, _address)) 
{ 
    return client.GetActions(customerNumber); 
} 

我现在想知道,当这些数据已经在自动生成的 app.config 中时,为什么我必须传递这些参数。 我删除了 app.config 的内容...这些数据似乎没有在任何地方使用。

我做错了什么吗?

编辑:

dll项目中的app配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.serviceModel> 
    <bindings> 
        <basicHttpBinding> 
            <binding name="BasicHttpBinding_ICustomerService" closeTimeout="00:01:00" 
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
                useDefaultWebProxy="true"> 
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
                <security mode="None"> 
                    <transport clientCredentialType="None" proxyCredentialType="None" 
                        realm="" /> 
                    <message clientCredentialType="UserName" algorithmSuite="Default" /> 
                </security> 
            </binding> 
        </basicHttpBinding> 
    </bindings> 
    <client> 
        <endpoint address="http://localhost:50309/CustomerService.svc" 
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerService" 
            contract="ServiceReference.ICustomerService" name="BasicHttpBinding_ICustomerService" /> 
    </client> 
</system.serviceModel> 
</configuration>     

请您参考如下方法:

请注意以下几点:DLL 可以不有自己的 app.config!

如果您希望 DLL 使用配置值,通常使用 DLL 项目的属性创建它们,然后从 DLL 的 app.config复制设置部分到 EXE 的 app.config。此外,您需要复制相应的 sectionGroup 条目。

DLL 然后将使用应用程序的 exe.config 文件中的设置。

为应用程序和 DLL 提供设置的 EXE 项目的 app.config 示例:

<?xml version="1.0"?> 
<configuration> 
  <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
      <section name="ExeProject" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
      <section name="DllProject" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
  </configSections> 
 
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
  </startup> 
 
  <applicationSettings> 
    <ExeProject> 
      <setting name="..." serializeAs="String"> 
        <value>...</value> 
      </setting> 
    </ExeProject> 
 
    <DllProject> 
      <setting name="..." serializeAs="String"> 
        <value>...</value> 
      </setting> 
    </DllProject> 
 
  </applicationSettings> 
</configuration>