0

我希望有人能够帮助我,我已经有一段时间没有用 C# 编程了。我相信答案会很简单,但我无法理解它。

我有以下内容,我称之为 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 内部编辑它,我 99% 肯定我不应该这样做。

感谢您提供的任何帮助

4

2 回答 2

2

由于ExchangeService是一个部分类,您可以创建另一个文件,添加到ExchangeServiceGetWebRequest 的声明并声明一个合适的覆盖。这将允许您为 编写自定义功能ExchangeService,同时在 WSDL 更改时仍然可以更新它。

有关详细信息,请参阅 MSDN 文章部分类和方法(C# 编程指南)

于 2012-06-13T02:24:19.723 回答
1

子类吗?GetWebRequest在根类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 。

于 2012-06-13T02:28:06.080 回答