mercredi 28 août 2019

How to insert a database record if no matching Web Method is called

I am trying to creating a simple SOAP Web Service and I would like it to log (by adding a SQL database record) when no matching WebMethod is called, and include what the failed method/action is.

The Web Service contains two WebMethod's; MethodA and MethodB.

If MethodA or MethodB is called, it adds an entry into the SQL database using my custom class "Common.LogRequestResponse" which takes a SQL connection string, and the name of the called method. This works perfectly.

However, if neither MethodA or MethodB is called, I'd like the web service to add a record specifying what the failed method was. For example, if a SOAP request trying to call "MethodC" is sent to the web service, it will obviously fail but the web service should catch this and use "Common.LogRequestResponse" noting "MethodC".

This is the Web Service code;

namespace SOAPWebService
{
    [WebService(Namespace = "http://test.com")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class SOAPService : System.Web.Services.WebService
    {

        [webMethod]
        public string MethodA()
        {
            string strConnect = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            Common.LogRequestResponse(strConnect, "MethodA");

            return "this is a Method A";
        }

        [webMethod]
        public string MethodB()
        {
            string strConnect = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            Common.LogRequestResponse(strConnect, "MethodB");

            return "this is a Method B";
        }

        // some kind of Catch code here if neither methods above called
        Catch()
        {
            string MethodCalled = xxx // the name of the method being called
            string strConnect = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
            Common.LogRequestResponse(strConnect, MethodCalled);

            return "Failed to find method"+ MethodCalled;
        }

    }
}

This is a SOAP Request XML that correctly calls MethodA;

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <MethodA xmlns="http://test.com" />
    </soap:Body>
</soap:Envelope>

And this is a SOAP Request XML that calls a method I have not defined, and would like the Web Service to record "MethodC";

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <MethodC xmlns="http://test.com" />
    </soap:Body>
</soap:Envelope>




Aucun commentaire:

Enregistrer un commentaire