How can I generate OperationContract(Action, ReplyAction) in ChannelFactory automatically for a WCF service in C#?

76 views Asked by At

I am working on a dashboard monitoring application. It's a REST API application using .Net Core 6.0 (C#) As part of this project, I need to monitor the activity of several web applications. Most of these applications are WCF services.

Requirements:

  • I need to ping the application to check if it responds and retrieve the version (here is a HelloWorld() and GetVersion() method on all services)
  • create a connection to the application 100% automatically: the only thing I have is the application name and the WSDL address (exemple :* http://namespace/WCFServiceName.svc*)

To create this connection I use ChannelFactory which establishes a connection to the application, but I can't manage to generate [OperationContract(Action="...", ReplyAction="...")] in an automatic way. To make it work, I hard-coded this information.

        public async Task<string> GetStatus(string serviceAddress)
        {
            BasicHttpBinding myBinding = new BasicHttpBinding();

            myBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

            myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Ntlm;

            EndpointAddress myEndpoint = new EndpointAddress(serviceAddress);

            ChannelFactory<IWcfService> myChannelFactory = new ChannelFactory<IWcfService>(myBinding, myEndpoint);

            // Create a channel
            IWcfService wcfClient = myChannelFactory.CreateChannel();
            string status = await wcfClient.HelloWorld();
            ((IClientChannel)wcfClient).Close();
            return status;
        }

And this is my interface IWcfService:

    [ServiceContract]
    public interface IWcfService
    {
        [OperationContract(Action = "http://tempuri.org/WcfServiceName/GetVersion", ReplyAction = "http://tempuri.org/WcfServiceName/GetVersionResponse")]
        Task<string> GetVersion();

        [OperationContract(Action = "http://tempuri.org/WcfServiceName/HelloWorld", ReplyAction = "http://tempuri.org/WcfServiceName/HelloWorldResponse")]
        Task<string> HelloWorld();
    }

My goal is to get rid of this information displayed in OperationContract and generate it automatically, for example :

[OperationContract(Action = "http://tempuri.org/" + **serviceName **+ "/HelloWorld", 
                   ReplyAction = "http://tempuri.org/" + **serviceName **+ "/HelloWorldResponse")]

In this case I will pass the serviceName and connect directly to any service.

0

There are 0 answers