Home
Find the answer to your question
How to create a notification listener using .NET framework
Listener Part:
The following is the code snippet that represents a web service that accepts a GetItemTransactions SOAP Payload.
Code in the eBayNotification.asmx.cs file |
using System; using System.Collections;using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; using eBay.Service.Core.Soap; namespace eBayNotification { /// <summary> /// Summary description for eBayNotification. /// </summary> [WebService(Namespace = "urn:ebay:apis:eBLBaseComponents")] public class eBayNotification : System.Web.Services.WebService { private CustomSecurityHeaderType mRequesterCredentials; public eBayNotification() { //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent(); } [WebMethod()] [System.Web.Services.Protocols.SoapHeaderAttribute("RequesterCredentials", Direction = System.Web.Services.Protocols.SoapHeaderDirection.In)] [System.Web.Services.Protocols.SoapDocumentMethodAttribute(Action = "https://developer.ebay.com/notification/FixedPriceTransaction", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)] public void FixedPriceTransaction(GetItemTransactionsResponseType GetItemTransactionsResponse) { LogRequest(Server.MapPath("files/FixedPriceTransaction_" + GetItemTransactionsResponse.Item.ItemID + "_" + GetItemTransactionsResponse.CorrelationID + ".xml")); } } } |
The web config file will be as follows. |
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <webServices> <conformanceWarnings> <remove name='BasicProfile1_1'/> </conformanceWarnings> </webServices> </system.web> </configuration> |
Testing the Listener:
This test application contains a web page where you have a textarea that contains a GetItemTransactions SOAP payload. On clicking the submit button the test application streams the payload to the listener and the listener writes the payload to local system under a user specific folder(in this sample the listener writes the payload as a text file under the applications root directory in a folder called 'files').
The code part in the default.aspx.cs is as follows. |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.IO; using System.Net; using System.Text; using System.Web.Services.Protocols; using System.Web.Services; namespace TestNotification { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { if (IsPostBack == false) { string fileName = AppDomain.CurrentDomain.BaseDirectory + "payload.txt"; StreamReader reader = new StreamReader(fileName); string strPayload = reader.ReadToEnd(); txt_Payload.Text = strPayload; } } catch (Exception ex) { throw ex; } } protected void Btn_Submit_Click(object sender, EventArgs e) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://localhost:8080/eBayNotification.asmx"); HttpWebResponse resp = null; //Add the request headers req.Headers.Add("SOAPAction", "https://developer.ebay.com/notification/FixedPriceTransaction"); req.Host = "localhost"; req.ContentType = "text/xml; charset=utf-8"; req.Method = "POST"; var payload1 = txt_Payload.Text.Trim().Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n", ""); //Converting string to a byte array byte[] postDataBytes1 = System.Text.Encoding.ASCII.GetBytes(payload1); int len = postDataBytes1.Length; req.ContentLength = len; //Post the request to eBay Stream requestStream = req.GetRequestStream(); requestStream.Write(postDataBytes1, 0, postDataBytes1.Length); requestStream.Close(); // get response and write to console resp = (HttpWebResponse)req.GetResponse(); Response.Write("Status: " + resp.StatusCode.ToString()); resp.Close(); } catch (Exception ex) { Response.Write(ex.Message); } } } } |
Note:
Please see the attachment for the complete code sample for both the listener and the test application.