我希望有人能够帮助我,我还没有用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中编辑它,我很肯定我不应该这样做。
感谢您提供的任何帮助
答案 0 :(得分:2)
由于ExchangeService
是partial class,您可以创建另一个添加到ExchangeService
声明的文件,并为GetWebRequest声明一个合适的覆盖。这将允许您为ExchangeService
编写自定义功能,同时如果WSDL发生更改,它仍然可以更新。
有关详细信息,请参阅MSDN文章Partial Classes and Methods (C# Programming Guide)。
答案 1 :(得分:1)
子类吗?根类 WebClientProtocol 中的 GetWebRequest 为marked as virtual,因此您可以覆盖该方法。
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 。