简体   繁体   English

如何将 SOAP 标头附加到我的 C# 客户端?

[英]How do I attach a SOAP Header to my C# client?

I have my web service set up to recieve a soap header of name "TestHeader" with param "Name"我已将 Web 服务设置为接收名称为"TestHeader"且参数为"Name"的soap标头

How do i create a soap header in my client AND send it to the service?如何在我的客户端中创建一个 soap 标头并将其发送到服务?

So far I have created it in my client.到目前为止,我已经在我的客户端中创建了它。

public class TestHeader : SoapHeader
{
    public String Name;
}

Initialised my service,初始化我的服务,

    Test.TestServicesClient SOAP = new Test.TestServicesClient();

Initialised my header.初始化我的标题。

  TestHeader header = new TestHeader();

set variable in header在标题中设置变量

header.Name = "BoB";

Now what?怎么办? Ive tried following MSDN, and other tutorials but not got anywhere.我试过遵循 MSDN 和其他教程,但一无所获。

TestService.cs测试服务.cs

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace Test
{

    // Define a SOAP header by deriving from the SoapHeader class.
    public class TestHeader : SoapHeader
    {
        public String Name;
    }

   public class TestService : ITestServices
    {      

        public TestHeader TestInHeader;


       [WebMethod]
        [SoapHeader("TestInHeader", Direction = SoapHeaderDirection.In)]
        public List<ServiceDetails> GetServiceDetails(Int32 CostCentreNo, Int32 ServiceCode, Boolean Recurring)
        {
            throw new System.Exception(TestInHeader.ToString());

        }
    }
}

I guess I'm a little late, but here's the answer:我想我有点晚了,但这是答案:

    Test.TestHeader header = new Test.TestHeader();
    header.Name = "BoB";
    Test.TestService SOAP = new Test.TestService();
    SOAP.TestHeaderValue = header;
    SOAP.GetServiceDetails(0,0,False);

Here's a LINK that clarifies the subject: "...Visual Studio will create a property in web service proxy called 'UserCredentialsValue' which will map the 'consumer' public property [which inherits from SoapHeader] in the web service."这是一个澄清主题的链接:“...Visual Studio 将在 Web 服务代理中创建一个名为“UserCredentialsValue”的属性,它将映射 Web 服务中的“消费者”公共属性 [从 SoapHeader 继承]。”

Since the information you provide is not detailed enough only some general pointers:由于您提供的信息不够详细,因此仅提供一些一般性提示:

The above links contains solutions including sample code for WCF (first link) and SOAP (second link)...以上链接包含解决方案,包括 WCF(第一个链接)和 SOAP(第二个链接)的示例代码...

EDIT - as per comments:编辑 - 根据评论:

This article (archived) gives you a complete walkthrough on implementing custom SOAP headers and setting them before calling the respective service. 这篇文章(已存档)为您提供了一个完整的演练,了解如何在调用相应的服务之前实现自定义 SOAP 标头并设置它们。

Bascially you need to extend the Test.TestServicesClient class with the TestHeader you defined and then you can just set its value before calling a method of the web service.基本上,您需要使用您定义的TestHeader扩展Test.TestServicesClient类,然后您可以在调用 Web 服务的方法之前设置其值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM