简体   繁体   中英

Calling WCF service from Azure ServiceConfig

I added WCF Servicereference to my webapplication ,the following entries got created to my web.config .

<system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_INameVerification" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint address="https://int. NameVerification.com/Default/NameVerificationService.svc"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_INameVerification"
    contract="NameVerificationService.INameVerification"
    name="WSHttpBinding_INameVerification" />
</client>

Now I am planning to host my application to Azure cloud platform. Once I host my app on cloud I want to be able to change my endpoints at any time ie.,

<endpoint address=”https://int. NameVerification.com/Default/NameVerificationService.svc” 
   binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_INameVerification"
    contract="NameVerificationService.INameVerification"
    name="WSHttpBinding_INameVerification" />

How do I add this call to my WCF service from SericeConfig ?What is the actual entriess in ServiceConfig so that my application will read my WCF end point address from Service config and not from web.config

Thank you all for your help .

I found a solution for this problem.

  1. Associated binding and serviceaddress in program, then added settings to Serviceconfig
  2. ServiceConfig entry Added
 <Setting name="myServiceAddressUrl" value="https://int. NameVerification.com/Default/NameVerificationService.svc" /> 
    WSHttpBinding binding = new WSHttpBinding();
    binding.Name = "WSHttpBinding_INameServiceVerification";
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.ReliableSession.Enabled = false;
    binding.TransactionFlow = false;
    binding.Security.Mode = SecurityMode.Transport;
    binding.Security.Message.ClientCredentialType = 
        MessageCredentialType.Windows;

    string myServiceAddressUrl = 
        RoleEnvironmentWrapper.GetConfigurationSettingValue(
           "AdderssServiceURL.svc");
    EndpointAddress myService = new EndpointAddress(myServiceAddressUrl);

    NameVerificationClient verificationClient = 
        new NameVerificationClient(binding, myService );

Consider using startup task with console utility to read configuration from RoleEnvironment and manually (or by using ServiceManager) update your web.config. Another option would be to use Service Factory and read role configuration before explicit service creation.

Probably it can be also accomplished by using WebRole OnStart method, but i'm not sure whether it is safe to modify web.config at this point

If I understand correctly, you're only looking to change endpoints of what service you consume without redeploying your service-consuming application that is running in Azure? You're not looking to change end-points of the actual service as it seems that this service is external to your solution?

If so, I'd like to suggest that you make your application not reliant on the endpoint configuration specified in the web.config file, but instead instrument your endpoint settings in ServiceConfig file (in whatever name-value pairs you feel are needed) and manually parse them when constructing a proxy to call your 3rd party endpoint. This way you'll have complete control over what you're getting from web.config (perhaps binding configuration can still be driven by web.config) and what you're getting from ServiceConfig (the end-point UrL, etc) and are able to switch the endpoint later on in ServiceConfig w/o redeploying and/or taking down your application.

You will need to check RoleEnvironment if you're running under Azure (real or emulator) in order to read from ServiceConfig.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM