locked
Is there a way to conditionally Enable/Disable a WebService? RRS feed

  • Question

  • User-943250815 posted

    In my scenario, the process behind webservice needs to use a valid certificate hosted on server.
    So, if certificate has been expired, webservice returns a message instead of some data.

    I was thinking if there is a way to hide, my webservice when certificate does not exist or expired?
    By code or IIS config.

    Friday, February 23, 2018 4:50 PM

Answers

  • User753101303 posted

    Seems https://msdn.microsoft.com/en-us/library/system.web.services.webservice.context(v=vs.110).aspx allows access to the underlying Response (to set a status code).

    I would perhaps use 503 server unavailable rather than 404 which would rarther point at a client side error. This is really ASMX ? It's been a while since I used that.

    Edit: based on the default ASMX I tried something such as :

    if(System.DateTime.Now.Second % 2==0)
    {
        Context.Response.StatusCode = 503;
        Context.Response.End(); // or comment to still return a nil XML payload
        return null;
    }
    return "Hello World";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 28, 2018 6:54 PM

All replies

  • User516094431 posted

    You can use following code:

    public class SslCertificateExpirationChecker
    {
        public DateTime GetCertificateExpirationDate(string host, int port)
        {
            TcpClient client = new TcpClient(host, port);
    
            X509Certificate2 x509 = null;
            SslStream sslStream = new SslStream(client.GetStream(), false, 
                delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslError)
                {
                    x509 = new X509Certificate2(cert);
                    return true;
                });
            sslStream.AuthenticateAsClient(host);
            client.Close();
            return x509.NotAfter;
        }
    }
    if(checker.GetCertificateExpirationDate("www.mydomain.com", 443) < DateTime.Now)
    {
    // Call Web Service
    }
    else
    {
    // Message For Expire Certificate
    }

    Refer From:

    https://stackoverflow.com/questions/826175/determine-ssl-certificate-expiration-date-iis

    Monday, February 26, 2018 10:59 AM
  • User-943250815 posted

    Thank'a mshoaiblibra,

    Your sugestion can help on verify before call any sub or function.
    But webservice still available waiting for requests. I was thinking if there is any other option.
    Right now I can stop on IIS, or delete/rename .asmx file.

    Tuesday, February 27, 2018 12:02 PM
  • User753101303 posted

    Hi,

    The current status code you get is ? You want to return a 404 instead ? It won't be more confusing leaving consumers to wonder why exactly it doesn't  work any more ?

    It seems to me you should rather ensure the proper procedure is in place to renew certificates as needed...

    Tuesday, February 27, 2018 1:07 PM
  • User-943250815 posted

    PatriceSc,

    Yes 404 put my question in right direction.
    But do not focus on if Certificate is valid or not, I just use it as an possible scenario where I really want to my webservice not available. Where any request to webservice return nothing or 404.

    So if renew certificate procedure is forgotten, someone will alert that something goes wrong with webservice. due no answer.
    Consumer application expect an specific XML file, and there is no room for alerts. Is something like all or nothing.
    So your 404 mention seems best fit my needs


    Wednesday, February 28, 2018 12:58 AM
  • User753101303 posted

    Seems https://msdn.microsoft.com/en-us/library/system.web.services.webservice.context(v=vs.110).aspx allows access to the underlying Response (to set a status code).

    I would perhaps use 503 server unavailable rather than 404 which would rarther point at a client side error. This is really ASMX ? It's been a while since I used that.

    Edit: based on the default ASMX I tried something such as :

    if(System.DateTime.Now.Second % 2==0)
    {
        Context.Response.StatusCode = 503;
        Context.Response.End(); // or comment to still return a nil XML payload
        return null;
    }
    return "Hello World";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 28, 2018 6:54 PM
  • User-943250815 posted

    PatriceSc,

    Nice, thanks for the code, such code can help.
    But, let me try to explore options a little deeper.
    Thinkg on this, if webservice method is based on code, what about you have a choice to disable such method on code. I mean, you have a webservice with 3 methods, but you want to disable method 3 based on a condition, and left other 2 running. So to do that you have to edit your code and comment method you do not want be published.

    Of course I can add some check like you suggest (fine). But would be good to have a choice to disable a method based on a condition. Here I´m not concerned about users and objectives of webservice method, just think if this can be done on code (as an exercise)

    Sunday, March 4, 2018 6:38 PM
  • User753101303 posted

    That is you want to change the WSDL generation ? What about existing clients ?

    Try perhaps to have a look around https://msdn.microsoft.com/en-US/library/ms181843(v=vs.80).aspx

    ASP.NET uses often a "provider" approach so you might be able to alter WSDL generation or at least to maybe edit a WSDL file.

    Make sure thought to not overdesign:
    - if this is an "exercise" you should rather spend time on something you may need rather than on something you'll likely never need in your whole career
    - make sure you do get a real benefit for the additional work compared with just publishing the new version (possibly at a new location)
    - what happens for existing clients?

    The main point is that - as for a DLL - you do expose classes and methods (a "contract") and you are not supposed to change that without any prior notice, possibly breaking client code.

    Tuesday, March 6, 2018 8:55 AM