我目前有一个包含几个项目的解决方案,其中之一是 WCF 服务。我创建了另一个带有静态类的项目,该静态类基本上提供了通往 WCF 客户端实例的网关,如下所示:
public static class WSGateway
{
public static DBInteractionGatewayClient MR_WebService
{
get
{
return new DBInteractionGatewayClient();
}
}
}
这样(或者我认为)我可以使用一个app.config
文件仅位于该库中,然后其他项目可以仅引用它并从该属性获取对该客户端的引用。
但问题是,当项目尝试访问该属性时,会抛出异常,告诉我需要app.config
在应用程序中,当我复制app.config
我的应用程序网关库,它可以工作。
有没有办法避免出现多个app.config
应用程序中的文件并且可能在单个库中只有一个?
[更新]解决方案:
下列的安德森·艾姆斯'建议,现在我决定在类中对客户端参考配置进行硬编码,从而消除了对多个app.config
s.
因此,我从此翻译了我的配置(app.config
):
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IDBInteractionGateway" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="6000000"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<security mode="None"/>
<readerQuotas maxDepth="6000000" maxStringContentLength="6000000" maxArrayLength="6000000"
maxBytesPerRead="6000000" maxNameTableCharCount="6000000" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://agnt666laptop:28666/DBInteractionGateway.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDBInteractionGateway"
contract="DBInteraction_Service.IDBInteractionGateway" name="WSHttpBinding_IDBInteractionGateway">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
对此(一static class
):
public static class WSGateway
{
private static WSHttpBinding binding;
private static EndpointAddress endpointAddress;
static WSGateway()
{
var readerQuotas = new XmlDictionaryReaderQuotas()
{
MaxDepth = 6000000,
MaxStringContentLength = 6000000,
MaxArrayLength = 6000000,
MaxBytesPerRead = 6000000,
MaxNameTableCharCount = 6000000
};
binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};
endpointAddress = new EndpointAddress("http://agnt666laptop:28666/DBInteractionGateway.svc");
}
public static DBInteractionGatewayClient MR_WebService
{
get
{
return new DBInteractionGatewayClient(binding, endpointAddress);
}
}
public static void ExecuteCommand(Action<DBInteractionGatewayClient> command)
{
var ws = MR_WebService;
command.Invoke(ws);
ws.Close();
}
}