Checks whether the WCF Service is in the second online version.

Source: Internet
Author: User

Checks whether the WCF Service is in the second online version.

The last version is http://www.cnblogs.com/kklldog/p/4878296.html here

The previous version solved the problem that the monitoring service does not need to manually add service references, but you still need to add the corresponding endpoint information in the configuration file, and the dll file that requires the real contract interface to implement reflection to generate the wcf channel.

This is actually quite tedious. Every time you add a Monitoring Service, you need to modify a bunch of configurations, and copy a bunch of dll. In the new version, you do not need to copy the dll file and endpoint configuration of the real contract.

In the previous version, I needed the contract dll to create a wcf call channel for reflection, and to call a real method. In fact, you can use a fake contract interface and a fake method to detect it.

Even if a false contract interface channel is used, a System. ServiceModel. ActionNotSupportedException exception is reported when a nonexistent method is called. This indicates that the service exists.

Then we only need a false contract interface:

  [ServiceContract]    public interface IFakeWcfInterface    {        [OperationContract]        string ThisIsATestMethod();    }

Then, use ChanelFactory to construct the channel. This time there is no reflection, which is simpler. To remove the endpoint configuration file, we directly instantiate a ServiceEndpoint using code. The binding of all services on my side is uniform, so you can write it to death. If the binding and other information of each service are different, you need to abstract it into the config file.

Test alive:

  private bool IsALive(string endpointName,string url)        {            try            {                FuncExtension.TryDo(() =>                {                    var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IFakeWcfInterface)), new NetTcpBinding(SecurityMode.None), new EndpointAddress(url));                    var channelFactory = new ChannelFactory<IFakeWcfInterface>(endpoint);                    var proxy = channelFactory.CreateChannel();                    try                    {                        proxy.ThisIsATestMethod();                    }                    catch (Exception exc)                    {                        Logger.Trace(                            string.Format("Try to connect wcf service error:{0}, ExceptionType:{1}", endpointName,                                exc.GetType()), GetType(), exc);                        throw;                    }                    finally                    {                        try                        {                            (proxy as ICommunicationObject).Close();                        }                        catch                        {                            (proxy as ICommunicationObject).Abort();                        }                    }                }, 3);                return true;            }            catch (Exception exc)            {                PrintWholeException(exc);                return !IsHttpOrSocketException(exc);            }        }

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.