开发者

asp.net web services soap header appearing as parameter in client service call

I'm simply attempting to use a soap header for authentication purposes.

After adding a service reference to a client console application, the header shows as the first parameter in list instead of as a member on the client object.

Anyone have any idea what I'm doing wrong?

WebService:

public class Service1 : System.Web.Services.WebService
{
    public CustomSoapHeader MyHeader;

    [WebMethod]
    [SoapHeader("MyHeader")]
    public string HelloWorld()
    {
        return "Hello World";
    }

    public class CustomSoapHeader : SoapHeader
    {
        public string SomeProperty { get; set; }
 开发者_StackOverflow社区   }
}

Client:

class Program
{
    static void Main(string[] args)
    {
        Service1SoapClient client = new Service1SoapClient();
        client.HelloWorld(new CustomSoapHeader());
    }
}


If by "Service Reference" you mean a WCF client, then the problem is that the server isn't a WCF server. If you add the reference as a "Web Reference" then the header should appear as a member of the client proxy class.


using System;
using System.Windows.Forms;

namespace WebClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // For Web Reference:
            //ServiceReference1.HelloWorldRequest = new WebClient.ServiceReference1.HelloWorldRequest();
            //label1.Text = webService.GetClientTime(5).ToString(); 

            string baseURL = "http://localhost:11674/Service1.asmx";

            // Create the SystemService Client
            // Looking to the ap.config for "Service1Soap" binding string.
            ServiceReference1.Service1SoapClient systemService
                = new ServiceReference1.Service1SoapClient("Service1Soap", baseURL);

            label1.Text = systemService.HelloWorld();

            WebClient.ServiceReference1.Auth myAuf = new WebClient.ServiceReference1.Auth();

            myAuf.password = "test";
            myAuf.user = "test";

            try
            {
                label2.Text = systemService.GetClientTime(myAuf, 0).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }
    }
}`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜