Using WCF on Localhost on Azure

后端 未结 2 892
悲哀的现实
悲哀的现实 2021-02-09 02:01

In summary
How do I acces a WCF service on localhost when hosted in IIS on Azure? Azure does not bind localhost or 127.0.0.1 to my website.

相关标签:
2条回答
  • 2021-02-09 02:18

    Okay, so this is how I solved it. IMHO it's a hack but at least it works.

    Basically, I need to add a "*" binding, so I can do this in Powershell. The general recipe is here: http://blogs.msdn.com/b/tomholl/archive/2011/06/28/hosting-services-with-was-and-iis-on-windows-azure.aspx

    That deals with adding Named Pipes support, but the principle is the same. I just changed the Powershell script to:

    import-module WebAdministration
    # Set up a binding to 8080 for the services 
    Get-WebSite "*Web*" | Foreach-Object { 
      $site = $_;
      $siteref = "IIS:/Sites/" + $site.Name;
      New-ItemProperty $siteref -name bindings -value @{protocol="http";bindingInformation="*:8080:"}
    }
    

    This now allows me to use http://127.0.0.1:8080/service.svc to access my service.

    Note: You do need to follow the rest of the recipe to set elevated execution context and change the powershell execution mode, so do follow it carefully

    0 讨论(0)
  • 2021-02-09 02:22

    You have to build the endpoint address dynamically.

    Step 1: In your ServiceDefinition.csdef you need to declare an Endpoint.

    <ServiceDefinition name="MyFirstAzureWorkflow" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
      <WebRole name="WorkflowWeb" vmsize="ExtraSmall">
        <Sites>
          <Site name="Web">
            <Bindings>
              <Binding name="Endpoint1" endpointName="WorkflowService" />
            </Bindings>
          </Site>
        </Sites>
        <Endpoints>
          <InputEndpoint name="WorkflowService" protocol="http" port="80" />
        </Endpoints>
        <Imports>
          <Import moduleName="Diagnostics" />
        </Imports>
      </WebRole>
    </ServiceDefinition>
    

    Step 2: When you want to call the service

    var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WorkflowService"].IPEndpoint;
    var uri = new Uri(string.Format(
        "http://{0}:{1}/MyService.xamlx",
        endpoint.Address,
        endpoint.Port));
    var proxy = new ServiceClient(
        new BasicHttpBinding(),
        new EndpointAddress(uri));
    
    0 讨论(0)
提交回复
热议问题