【发布时间】:2020-12-06 05:55:48
【问题描述】:
免责声明:我从未使用过 SOAP Web 服务。完全没有。一点儿都没有。所以通道和WCF脚手架的概念让我有点困惑,所以我在这里。
我被要求集成到使用基本身份验证的 SOAP XML Web 服务。 (例如,授权标头,基本 xxxxxxxxxxxxxxxxxxxxxxxxxxxx
我使用 Visual Studio WCF 连接的服务发现来生成脚手架代码,这对我实例化所需的对象等非常有用,但是我的问题是我被要求使用基本身份验证,我不知道在哪里将此代码注入已生成的脚手架中。我以前使用过基本身份验证,所以我了解“如何”做它,例如 REST API 等。只需用户名:密码,base64 编码并添加到授权标头。但是,我不确定如何为这个脚手架的 SOAP Web 服务执行此操作。
我认为可以注入到每个请求中以添加您的自定义标头的代码是:
using (OperationContextScope scope = new OperationContextScope(IContextChannel or OperationContext)
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = new HttpRequestMessageProperty()
{
Headers =
{
{ "MyCustomHeader", Environment.UserName },
{ HttpRequestHeader.UserAgent, "My Custom Agent"}
}
};
// perform proxy operations...
}
OperationContextScope 需要 IContextChannel 或 OperationContext。我不知道要在这里添加什么。如果我查看我的脚手架代码,我可以在这里找到 Web 服务的“客户端”:
public partial class POSUserInformationManagerV1_2Client : System.ServiceModel.ClientBase<McaEndpointPosUserInformation.POSUserInformationManagerV1_2>, McaEndpointPosUserInformation.POSUserInformationManagerV1_2
我可以在这里找到“通道”,但它只是另一个接口,没有指定任何合同?
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
public interface POSUserInformationManagerV1_2Channel : McaEndpointPosUserInformation.POSUserInformationManagerV1_2, System.ServiceModel.IClientChannel
{
}
我查看了 ChannelBase,它似乎应该接受实现一个或另一个通道接口的各种对象(包括 IClientChannel,脚手架 POSUserInformationManagerV1_2Channel 使用)
protected class ChannelBase<T> : IDisposable, IChannel, ICommunicationObject, IOutputChannel, IRequestChannel, IClientChannel, IContextChannel, IExtensibleObject<IContextChannel> where T : class
{
protected ChannelBase(ClientBase<T> client);
[SecuritySafeCritical]
protected IAsyncResult BeginInvoke(string methodName, object[] args, AsyncCallback callback, object state);
[SecuritySafeCritical]
protected object EndInvoke(string methodName, object[] args, IAsyncResult result);
但我仍然坚持我可以放入 OperationContextScope 以将其适当地连接到“通道”。我已经尝试过 POSUserInformationManagerV1_2Client 和相关的 Channel 接口,但都不会转换为 IContextChannel。有没有人有任何想法/想法?
编辑:这是我尝试注入代码以添加 Auth HTTP 标头的地方:
public System.Threading.Tasks.Task<McaEndpointPosUserInformation.requestUserInformationResponse> requestUserInformationAsync(McaEndpointPosUserInformation.requestUserInformation request)
{
using (OperationContextScope scope = new OperationContextScope(request)
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = new HttpRequestMessageProperty()
{
Headers =
{
{ "MyCustomHeader", Environment.UserName },
{ HttpRequestHeader.UserAgent, "My Custom Agent"}
}
};
// perform proxy operations...
}
return base.Channel.requestUserInformationAsync(request);
}
【问题讨论】:
-
您有一条带有标头和 xml 正文的 HTTP 消息。因此,您正在做两件事 1) 添加带有凭据(用户名/正文)和 UserAgent(可接受的浏览器)的自定义标头。请参阅:developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent 2)您正在将类序列化为 xml,这是正文的上下文。
-
谢谢jdweng(我觉得你以前回答过我的问题?:P)..但我不确定你提供的信息如何(虽然我确信它很有用) 解决所问问题的具体情况。能稍微详细点吗?
-
最好的方法是为 header 和 body 创建 c# 类。然后将肥皂的上下文作为类。查看带有标题和正文的 xml 肥皂示例:w3schools.com/xml/xml_soap.asp
标签: c# soap channel soapheader