2012-06-13 165 views
0

我希望有人能够帮助我,我没有在C#编程一段时间。我相信答案很简单,但我无法理解它。肥皂内WebRequest

我有以下,我打电话System.Web.Services.Protocols.SoapHttpClientProtocol。我只包含了我所拥有的众多电话中的一个。

public partial class ExchangeService : System.Web.Services.Protocols.SoapHttpClientProtocol { 

//Lots of code using Soap 

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("getDetailsLite", RequestNamespace="http://www.MySite.com/ExchangeService/", ResponseNamespace=" http://www.MySite.com/ExchangeService/", ", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
    [return: System.Xml.Serialization.XmlElementAttribute("Result", IsNullable=true)] 
    public GetDetailsLiteResp getDetailsLite(getDetailsLiteReq request) { 
     object[] results = this.Invoke("getDetailsLite", new object[] { 
        request}); 
     return ((getDetailsLiteResp)(results[0])); 
    } 

    public void getDetailsLiteAsync(getDetailsLiteReq request) { 
     this. getDetailsLiteAsync(request, null); 
    } 

    public void getDetailsLiteAsync (getDetailsLiteReq request, object userState) { 
     if ((this.getDetailsLiteOperationCompleted == null)) { 
      this.getDetailsLiteOperationCompleted = new System.Threading.SendOrPostCallback(this.OngetDetailsLiteOperationCompleted); 
     } 
     this.InvokeAsync("getDetailsLite", new object[] { 
        request}, this. getDetailsLiteOperationCompleted, userState); 
    } 
} 

我想覆盖SoapHttpClientProtocol调用的WebRequest。 的SoapHttpClientProtocol看起来像这样(我相信从System.Web.Services.dll调用)

namespace System.Web.Services.Protocols { 
    public class SoapHttpClientProtocol : HttpWebClientProtocol { 
    public SoapHttpClientProtocol(); 
    public SoapProtocolVersion SoapVersion { get; set; } 
    protected IAsyncResult BeginInvoke(string methodName, object[] parameters, AsyncCallback callback, object asyncState); 
    public void Discover(); 
    protected object[] EndInvoke(IAsyncResult asyncResult); 
    protected virtual XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize); 

    //This line is the one i am talking about 
    protected override WebRequest GetWebRequest(Uri uri); 

    protected virtual XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize); 
    protected object[] Invoke(string methodName, object[] parameters); 
    protected void InvokeAsync(string methodName, object[] parameters, SendOrPostCallback callback); 
    protected void InvokeAsync(string methodName, object[] parameters, SendOrPostCallback callback, object userState); 
    } 
} 

我需要保护的覆盖的WebRequest GetWebRequest(URI URI)看起来像这样:

protected override WebRequest GetWebRequest(Uri uri) { 
    HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri); 
    webRequest.KeepAlive = false; 
    webRequest.ProtocolVersion=HttpVersion.Version10; 
    return webRequest; 
} 

有谁知道我会怎么做?我可以直接在SoapHttpClientProtocol里编辑它,我肯定我不应该这样做。

感谢您可能能够提供

回答

1

子类呢? GetWebRequestmarked as virtual的根类WebClientProtocol,所以你可以重写该方法。根据需要您的服务类中

public class MyHttpProtocol : SoapHttpClientProtocol 
{ 
    public override WebRequest GetWebRequest(Uri uri) 
    { 
     // Base request: 
     WebRequest request = base.GetWebRequest(uri); 

     // You code goes here... 

     // Return... 
     return request; 
    } 
} 

然后使用MyHttpProtocol

+0

谢谢,这看起来像可能工作。我会给它一个旋风。 – Glen

+1

OK,得到了以下错误(不能更改访问修饰符当重写“保护”继承成员),所以我不得不将其更改为 公众新的WebRequest GetWebRequest(URI URI) {// 我的代码 } – Glen

+0

@Glen你'对,我修复了代码片段 – MAXE

2

由于ExchangeService任何帮助是partial class,您可以创建另一个文件,增加了的ExchangeService声明还宣称对GetWebRequest一个合适的替代。这将允许您为ExchangeService编写自定义功能,但如果WSDL更改,仍然可以更新它。

欲了解更多信息,请参阅MSDN文章Partial Classes and Methods (C# Programming Guide)