2010-11-19 48 views
2

我写了一个负责调用第三方WCF服务的类,它工作正常。我们的应用程序可以使用本地网络的代理设置进行配置,因此只需在拨打电话时使用这些设置(如果它们已设置)即可。如何使我的WCF服务调用通过已配置的本地代理服务器?

我看了一下,看不清楚如何做到这一点。我发现我使用的BasicHttpBinding对象具有ProxyAddress属性,但没有任何东西可以让我定义网络凭据或用户名和密码。

这可能就在我面前,所以我认为我只需要指出正确的方向。至少我希望这很简单!我需要做的就是告诉我的服务调用本地代理服务器以及使用什么凭证。

这是我实例化服务类的代码。我省略了构建请求类的下一部分,调用服务方法并处理结果。

// Create the service instance. 
var binding = new BasicHttpBinding(); 
var endPoint = new EndpointAddress(new Uri(_servicesBaseUri + "MyServiceName")); 
var service = new WSHsgCreateSchemeRepairClient(binding, endPoint); 

// Add the MessageInspector to the contract behaviours list. This will inject the SecurityHeader XML and the SOAP action. 
var soapAction = _servicesBaseUri + "MyServiceName/MyServiceMethod"; 
service.Endpoint.Contract.Behaviors.Add(new MessageInspector(_securityHeaderUsername, _securityHeaderPassword, soapAction)); 

我们有一些其他的Web服务调用(使用旧的Web服务方法)以及它们产生System.Net.WebProxy的新实例,并将其设置对服务类代理财产,但很明显,它在不同的WCF。

感谢。

回答

2

我想我已经完成了。可以设置和使用默认的系统代理。 This is a very useful article如果其他人违反相同的要求。

的想法是,你必须先设置System.Net.HttpWebRequest.DefaultWebProxy.Credentials(大概System.Net.CredentialCache.DefaultCredentials) - 这就是“默认代理”参数是从当您设置绑定的UseDefaultWebProxytrue拍摄。

0

您可以通过特定于您的服务的web.config设置来执行此操作。 在绑定配置,设置proxyAddress = “HTTP:// MYPROXY:8080”,并设置useDefaultWebProxy = “假”

<bindings> 
    <basicHttpBinding> 
    <binding name="SubscriberFulfilmentServiceSOAP12Binding" closeTimeout="00:01:00" 
    openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00" 
    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
    maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" 
    textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="false" 
    proxyAddress="http://myproxy:8080" 
    messageEncoding="Text"> 
     <readerQuotas maxDepth="32" maxStringContentLength="2147483647" 
     maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

设置代理的所有服务:

<system.net> 
    <defaultProxy> 
    <proxy usesystemdefault="True" proxyaddress="http://myproxy:8080" bypassonlocal="True" /> 
    </defaultProxy> 
</system.net> 
相关问题