none
WCF consume by dotnet winfom client and config entry RRS feed

  • Question



  • i am new in WCF but i am bit familiar with web service (ASMX file)
    i have couple of question on wcf client config entry

    when we create any web service (ASMX) proxy then nothing add in config file like below entry but in case of WCF the below entry adds. i just need to know the significant of the below entry.

    1) if i delete these below entry then what will happen....can't i call the service from the client side?

    2) just tell me when we call web service from client side then how do i say that which endpoint address my service will use to call service if there are more than one endpoint address added in client side ?

    3) how do i explicitly mention web service url from cient side when i will make a service call?

    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_ICommService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:00:05"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost/CommService/"
                binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_ICommService"
                contract="Services.ICommService" name="WSDualHttpBinding_ICommService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
    Thursday, February 7, 2013 2:56 PM

Answers

  • 1) If you delete those entries you will get an Exception that looks like: InvalidOperationException: Could not find default endpoint element that references the contract Services.ICommService in the ServiceModel client configuration section.

    You will get the above error if you are using the default constructor for the CommServiceClient class that was generated by the Add Service Reference Wizard.

    You can still call your service by using another constructor

    public CommServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    The constructor mentioned above accepts a binding and an endpoint address as parameters, which basically allows you to provide the information that was in the configuration file using code. It's up to you how you will initializes those values. You can read more about the Binding class and EndpointAddress class in WCF documentation on MSDN.

    2) If there are two endpoints implementing the contract "Services.ICommService" and you are using the default constructor you will get the following exception:
    An endpoint configuration section for contract 'Services.ICommService' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

    To solve this problem you can use another constructor that specifies the endpoint name you want to use. 

    public CommServiceClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    The endpointConfigurationName is the name attribute in the client element in the configuration. In your example the value is "WSDualHttpBinding_ICommService"

    3) If you mean mention the URL from code instead of configuration, you can use the constuctor I mentioned when answering your first question or you can use any of the othe constructors for the the class that allows you to change the endpointAddress

    For example using the following constructor

    public CommServiceClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    The above constuctor accepts a configuration name (allowing you to copy all the settings from the config file) and a remote address string (allowing you to specify a different URL). This is the easiest way. If you don't want entries in your config file, then you will have to provide WCF with the binding you want to use. Simply using "new WSDualHttpBinding()" will create an instance of the binding you want to use with the default settings. You can set the properties for the binding object if you want to change any of the default settings.

    Important Note:  Your call will fail if the binding you use in your client does not match the binding used by the server. The add service reference wizard reads the service metadata to extract this info and put it in the config file for you. If you want to do this by code and remove config entries, you will have to set the properties using code. Refer to MSDN documentation for more detailed explanation about bindings and configuration.


    http://sherifelmetainy.blogspot.com/


    Thursday, February 7, 2013 4:30 PM