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); } }