我現在有一對夫婦的項目的解決方案,其中之一是一個WCF服務訪問的共享庫的單一的app.config。我創造了另一個預測與靜態類,基本上提供了一個通往WCF客戶端的情況下,像這樣的:C#WCF:具有在提供服務
public static class WSGateway
{
public static DBInteractionGatewayClient MR_WebService
{
get
{
return new DBInteractionGatewayClient();
}
}
}
這是使(或因此我認爲)我可以用一個單一的app.config
文件,將只能在該庫中,然後其他項目纔可以引用它並從該屬性獲取對該客戶端的引用。
但問題是,當一個項目試圖訪問該屬性,拋出一個異常,告訴我,我需要app.config
應用程序,當我的app.config
了我的門戶庫複製到應用程序,它的工作原理。
有沒有一種方法,以避免在應用程序的多個app.config
文件,僅具有一個可能在單個庫?
[更新]解決方案:
繼Anderson Imes的建議,現在我決定硬編碼在類客戶參考配置,從而消除了多個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();
}
}
感謝您發佈您的解決方案......這將有助於其他人嘗試作出此決定。 – 2009-04-15 15:27:25
感謝您的提示。您的示例被截斷,所以我粘貼下面的解決方案(http://stackoverflow.com/questions/746107/c-wcf-having-a-single-app-config-in-a-shared-library-that-provides-訪問到/ 7652518#7652518) – 2011-10-04 18:40:25
不知道爲什麼代碼被截斷。無論如何,這是它應該看起來如何:http://pastebin.com/8jii24JV – 2011-10-05 14:25:02