For full functionality of this site it is necessary to enable JavaScript.
Here are the instructions how to enable JavaScript in your web browser.

Knowledge base

Find the answer to your question

Advanced Search PView

Search terms
Search Type
Product
Category
Language
Format
SDK
Sort by
Direction

Creating a notification listener using .NET framework

  • Answer ID 2112
  • Published 06/11/2020 11:26 AM
  • Updated 06/11/2020 11:26 AM
  • Permalink https://ebaydts.com/eBayKBDetails?KBid=2112
Product

How to create a notification listener using .NET framework


Listener Part:

  1. Add a new web service project and add reference to the eBay.Service.dll
  2. Use the visual studio development server instead of IIS and set it to run in specific port 8080 in the project properties
  3. Create a web service and name it as eBayNotification.asmx and inherit it from System.Web.Services.WebService.
  4. Implement the required web methods based on the notificaiton events and the respective response type.
  5. For example, consider the FixedPriceTransaction event that sends a GetItemTransactions SOAP payload from eBay.

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').

  1. Add a new web application project and name it as TestNottifcation and add a web page called Default.aspx.
  2. Add a submit button and a textarea into the Default.aspx page.
  1. In the TestNotification application's root directory you will notice a GetItemTransactions payload txt file
  2. When you run the test application the payload content is loaded into the textarea and when you click the 'Submit' button, using the http object this GetItemTransactions payload, is streamed into the http object and delivered to the listener. The listener will then process the payload and writes the same into its 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.

File Attachments

How well did this answer your question?

Answers others found helpful

Print Email This Page Delicious Digg Facebook Reddit StumbleUpon Twitter