我编写了在DLL中引用的WCF服务的代码。
在添加该服务参考之后,会自动使用所需数据生成一个app.config。
客户端使用dll与wcf服务进行通信...没什么特别的。
但是,当我尝试创建服务引用..对象崩溃时,它说它无法找到端点地址。
我通过将绑定和地址传递给服务引用来搜索并修复它:
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项目中的应用程序配置:
<?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
文件中的设置。
EXE项目的app.config
示例,该项目为应用程序和DLL提供设置:
<?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>
关于c# - 引用的WCF服务不使用app.config,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15677829/