1

I'm inexperienced with Web Services and I'm trying my best to get this working based on readings on MSDN and here in stackoverflow. Obviously, I'm still not getting it since I still cannot get it working so I'm seeking help.

Here's what I've been doing and what I need to do:

I have a sample request and a proxy class and with these I need to send a request to a WS using C#

    <soap:Header>
    <AppDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.example.com/websvc">
      <AppName>Application1</AppName>
    </AppDetails>
    <userDetails xmlns='http://schemas.example.com/mysoap'>
      <USER xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://schemas.example.com/user_schema'>
        <userid>12345</userid>
        <cid>100</cid>
      </USER>
    </userDetails>
  </soap:Header>
  <soap:Body>
    <myRequest_getReport xmlns="http://schemas.example.com/websvc" docID="9999"/>
  </soap:Body>

I've been having great success in creating the request via Fiddler because it's straighforward and I just need to fill in the values but I've been struggling for a week now in getting this to work in my code.

Here's the proxy class code:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/mysoap")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.example.com/mysoap", IsNullable = false)]
    public partial class userDetails : System.Web.Services.Protocols.SoapHeader
    {

        private System.Xml.XmlElement any_u;

        /// <remarks/>
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement Any
        {
            get
            {
                return this.any_u;
            }
            set
            {
                this.any_u = value;
            }
        }
    }


    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/infrastructure/user_schema")]
    public partial class USER
    {

        private uint userid_u;

        private uint cid_u;

        /// <remarks/>
        public uint userid
        {
            get
            {
                return this.userid_u;
            }
            set
            {
                this.userid_u = value;
            }
        }

        /// <remarks/>
        public uint cid
        {
            get
            {
                return this.cid_u;
            }
            set
            {
                this.cid_u = value;
            }
        }
    }



    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.example.com/websvc")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.example.com/websvc", IsNullable = false)]
    public partial class AppDetails : System.Web.Services.Protocols.SoapHeader
    {

        private string appName_u;

        /// <remarks/>
        public string AppName
        {
            get
            {
                return this.appName_u;
            }
            set
            {
                this.appName_u = value;
            }
        }


    }

And here's my code:

        static void Main(string[] args)
        {
            myRequest req_server = new myRequest("myremoteserver", null); //connects to the server

            // User
            XmlElement UserIdentity = UserDetailsHeader();

            req_server.userIdentityValue = new userDetails();
            req_server.userIdentityValue.Any = UserIdentity;

            // Application
            //XmlElement appDataVal = AppDataHeader();
            req_server.AppDataValue = new AppDetails();
            req_server.AppDataValue.AppName = "Application1";

            // Construct the request
            myRequest_getReport docRequest = new myRequest_getReport();

        }

        private static XmlElement UserDetailsHeader()
        {
            USER myUser = new USER();
            XmlSerializer xs = new XmlSerializer(typeof(USER));
            MemoryStream ms = new MemoryStream(1024);

            myUser.userid = 12345;
            myUser.cid = 100;
            xs.Serialize(ms, myUser);
            ms.Position = 0;
            XmlDocument xd = new XmlDocument();
            xd.Load(ms);

            return xd.DocumentElement;
        }


        private static XmlElement AppDataHeader()
        {
            AppDetails myApp = new AppDetails();
            XmlSerializer xs = new XmlSerializer(typeof(AppDetails));
            MemoryStream ms = new MemoryStream(1024);

            myApp.AppName = "Application1";
            xs.Serialize(ms, myApp);
            ms.Position = 0;

            XmlDocument xd = new XmlDocument();
            xd.Load(ms);
            return xd.DocumentElement;
        }

As you can see in UserDetailsHeader(), I was able to set the values for userid and cid in USER. I also created AppDataHeader() but wasn't able to use it later since I later figure out I didn't have to but I included it here anyway in case there is a need.

I set the UserDetailsHeader() in main() as UserIdentity but when I execute the code, an exception is displayed saying "UserDetails is not defined." Obviously I'm missing something very important here and I want to know what it is.

Could you please point me to the right direction? I am very confused right now and I'm not even sure if my code makes sense. The code in UserDetailsHeader is based on my readings on how to create a SOAP header request in C#, I got this from an MSDN article which I cannot find right now.

Ramone
  • 11
  • 3

0 Answers0