WCF客户端缓存响应?

时间:2014-01-29 14:20:54

标签: c# wcf

我在控制台应用程序中配置了WCF服务。此应用程序允许我输入不同的URL并调用WCF客户端的方法。每次我输入URL时,我都会设置绑定,因为我使用的是带凭据的代理(用户名和密码)。当我输入正确的URL(用户名,密码,URL和正确的代理端口)时,我得到了满意的结果,但是当我输入错误的结果时,我得到的结果与以前相同。

似乎缓存了响应。我尝试了一切,但没有解决我的问题。

我的应用配置:

<defaultProxy useDefaultCredentials="true">
  <proxy usesystemdefault="True" autoDetect="True"/>
</defaultProxy>

<bindings>
  <basicHttpBinding>
    <binding name="Default" useDefaultWebProxy="true" messageEncoding="Text" >
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="Windows" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<client>
  <endpoint address="https://secured.planexware.com/wsdmsfe2/service.svc"
    binding="basicHttpBinding" bindingConfiguration="Default" contract="ServiceReference.IDMSService"
    name="Default" />
</client>

地址格式示例为“http:// user : pass @ proxyurl : port”,这是我的客户工厂类

    public static BasicHttpBinding DMSServiceClientFactory(string address)
    {
        try
        {
            return ConfigureClientWithProxy(address);    
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw ex;
        }
    }

    private static BasicHttpBinding ConfigureClientWithProxy(string address)
    {
        // Tomo la configuracion del binding en el App.config
        BasicHttpBinding binding = new BasicHttpBinding("Default");
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        if (address.Contains("@"))
        {
            binding.UseDefaultWebProxy = true;
            WebRequest.DefaultWebProxy = GetWebProxyWithCredentials(address);
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
        }
        return binding;
    }

    private static WebProxy GetWebProxyWithCredentials(string address)
    {
        var partes = address.Split('@');
        var userPass = partes[0].Replace("http://", string.Empty);
        var urlPort = partes[1];

        string user = string.Empty;
        string pass = string.Empty;
        string url = string.Empty;
        string port = string.Empty;

        if (userPass.Contains(":"))
        {
            user = userPass.Split(':')[0];
            pass = userPass.Split(':')[1];
        }

        if (urlPort.Contains(":"))
        {
            url = urlPort.Split(':')[0];
            port = urlPort.Split(':')[1];
        }

        // La URL y las credenciales las sacamos de la especificacion que hizo el usuario al configurar el proxy
        WebProxy proxyObject = new WebProxy(string.Format(@"http://{0}:{1}", url, port));
        proxyObject.Credentials = new NetworkCredential(user, pass);

        return proxyObject;
    }

0 个答案:

没有答案