lundi 29 juillet 2019

How to use a .net framework library (which consumes a wcf service) in a web api build in .net framework

        In my case I have 
        1. A library project built in .NET Framework 4.7.1 which consumes two WCF web services.
        2. An Web API built in .NET Framework 4.7.1 which consumes the above library project.


        In the above library project I create a system ServiceModel ChannelFactory using endpointConfiguration name and then consumes the WCF Web Service.

        The problem is I am not able to Create ChannelFactory instance and getting the exception "Attribute 'binding' is required on 'endpoint' element"

        I have copied the <system.serviceModel> configuration settings to the Web API Web.config. But no success. Looks like the system.serviceModel section is not getting loaded from web.config

Web.config

        <system.serviceModel>
            <bindings>
              <basicHttpsBinding>
                <binding name="HTTPMyService" maxReceivedMessageSize="2147483647" openTimeout="00:05:00" closeTimeout="00:05:00" sendTimeout="00:05:00"
                  receiveTimeout="00:05:00"/>
                <binding name="ServiceSoap">
                  <security mode="Transport"/>
                </binding>
                <binding name="ServiceSoap1"/>
              </basicHttpsBinding>
            </bindings>
            <client>
              <endpoint address="https://myservice.com:8444/MyService.svc/BasicHttpInt" binding="basicHttpsBinding"
                bindingConfiguration="HTTPSMyService" contract="MyService.MyService" name="CustomBinding_MyService1"/>
              <endpoint address="https://MobileAuthenticateService.com/MobileAuthenticate/Service.asmx" binding="basicHttpsBinding" bindingConfiguration="ServiceSoap"
                contract="MobileAuthenticationService.ServiceSoap" name="ServiceSoap"/>
            </client>
          </system.serviceModel>

WCF Client library

    namespace MyDemoServices
    {    
          public delegate void UseMobileAuthenticationServiceDelegate<T>(T proxy);

            public static class MobileAuthenticationServiceBase<T>
            {
                public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>(ResolveBindingByName("ServiceSoap"));

                public static void Use(UseMobileAuthenticationServiceDelegate<T> codeBlock)
                {
                    IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
                    bool success = false;

                    try
                    {
                        codeBlock((T)proxy);
                        proxy.Close();
                        success = true;
                    }
                    finally
                    {
                    }
                }
        }
   }

Expectation is that WCF web service should be invoked successfully. I have investigated and found the the ConfigurationManager is not loading the from Web.config, but other sections like unity, logging , appsettings, and connection strings are getting loaded successfully.

Another point is when I explicitly load the Web.config using OpenMappedExeConfiguration then all settings and sections are getting loaded . Not sure what is going wrong here




Aucun commentaire:

Enregistrer un commentaire