在我的应用程序中,我正在调用 Web 服务,通过使用 SoapExtension 和 SoapExtensionAttribute,我可以拦截传入和传出的 SOAP 消息以进行日志记录。我使用了 http://msdn.microsoft.com/en-us/magazine/cc164007.aspx 中的示例作为输入。 但现在我想更进一步。我有一个 Windows 客户端调用我的类(在一个单独的项目中),然后类调用 Web 服务。我现在可以截获 SOAP 消息,但我不想将它们直接记录到文件中,而是想将这些消息传递回调用 Web 服务的类以及调用类的客户端。这些是我到目前为止所做的代码更改:
private String ExtractFromStream(Stream target)
{
if (target != null)
return (new StreamReader(target)).ReadToEnd();
return "";
}
public void WriteOutput(SoapMessage message)
{
newStream.Position = 0;
string soapOutput = ExtractFromStream(newStream);
newStream.Position = 0;
Copy(newStream, oldStream);
}
public void WriteInput(SoapMessage message)
{
Copy(oldStream, newStream);
newStream.Position = 0;
string soapInput= ExtractFromStream(newStream);
newStream.Position = 0;
}
我现在想将 soapInput 和 soapOutput 传递回包含应用此属性的方法的类。关于我应该如何做的任何线索?
最佳答案
对于任何碰巧路过的人,这里是解决方案:
SoapMessage 对象不包含有关我的客户端的任何信息。但是,我可以将此对象转换为 SoapClientMessage 对象,然后我就可以访问我的 Web 服务。如果我现在向此 Web 服务添加一个方法(通过创建一个新的公共(public)部分类),我可以像这样访问它的属性和方法(纯粹是一个例子!):
private String ExtractFromStream(Stream target)
{
if (target != null)
return (new StreamReader(target)).ReadToEnd();
return "";
}
public void WriteOutput(SoapMessage message)
{
newStream.Position = 0;
string soapOutput = ExtractFromStream(newStream);
SoapClientMessage soapClient = (SoapClientMessage)message;
WebServiceClass webservice = (WebServiceClass)soapClient.Client;
webservice.MyMethod(soapOutput); //Use your own method here!
newStream.Position = 0;
Copy(newStream, oldStream);
}
public void WriteInput(SoapMessage message)
{
Copy(oldStream, newStream);
newStream.Position = 0;
string soapInput= ExtractFromStream(newStream);
SoapClientMessage soapClient = (SoapClientMessage)message;
WebServiceClass webservice = (WebServiceClass)soapClient.Client;
webservice.MyMethod(soapInput);
newStream.Position = 0;
}
您可以通过创建一个新的公共(public)分部类并向其中添加方法、属性和任何您喜欢的内容来向您的 WebServiceClass 添加方法(如本例中的 MyMethod)。
关于c# - 将信息传递给应用了 SoapExtensionAttribute 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094702/