WebRequest within Soap
本文关键字:Soap within WebRequest | 更新日期: 2023-09-27 18:36:55
我希望有人能帮助我,我已经有一段时间没有用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% 确定我无论如何都不应该这样做。
感谢您提供的任何帮助
由于ExchangeService
是分部类,因此您可以创建另一个文件,该文件添加到ExchangeService
声明中,并为 GetWebRequest 声明合适的覆盖。 这将允许您为 ExchangeService
编写自定义功能,同时在 WSDL 更改时仍然可以更新它。
有关详细信息,请参阅 MSDN 文章分部类和方法(C# 编程指南)。
subclass it?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。