WCF Service in Azure WorkerRole for Silverlight application

笑着哭i 提交于 2019-12-13 19:47:07

问题


I have a Windows Azure WorkerRole which hosts a WCF Service. This service shall be consumed by a Silverlight application. Locally, this works fine, however, when I try to deploy it, the endpoint needs to be configured in the Role configuration (see image below).

When I delete that endpoint "WCFEndpoint", everything works fine locally. However, when it is there, the following exception occurs:

System.ServiceModel.AddressAlreadyInUseException: HTTP konnte die URL "http://+:9196/GreenwayService/" nicht registrieren, weil der TCP-Port 9196 von einer anderen Anwendung verwendet wird.

which means in English: HTTP could not register URL "...", because the TCP PORT 9196 is used by another application.

As far as I understand, the endpoint needs to be defined like in the picture in order to be accessible inside the cloud.

Here is my app.config:

<?xml version="1.0"?>
<configuration>
    <system.diagnostics>
        <trace>
            <listeners>
                <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
                    <filter type=""/>
                </add>
            </listeners>
        </trace>
    </system.diagnostics>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><system.serviceModel>
        <services>
            <service name="Greenway.AzureWorkerRole.ServiceHosting.CrossDomainService">
                <endpoint address="" behaviorConfiguration="HttpEnableBehavior"
                    binding="webHttpBinding" contract="Greenway.AzureWorkerRole.ServiceHosting.ICrossDomainService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9196/" />
                    </baseAddresses>
                </host>
            </service>
            <service behaviorConfiguration="GreenwayServiceBehavior" name="Greenway.AzureWorkerRole.ServiceHosting.GreenwayService">
                <endpoint address="" binding="basicHttpBinding" contract="Greenway.AzureWorkerRole.ServiceHosting.IGreenwayService" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9196/GreenwayService/" />
                    </baseAddresses>
                </host>
            </service>
        </services>

        <behaviors>
          <endpointBehaviors>
            <behavior name="HttpEnableBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>

          <serviceBehaviors>

            <behavior name="GreenwayServiceBehavior">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

And this is the code snippet starting the services:

ServiceHost greenwayServiceHost = new ServiceHost(typeof(GreenwayService));
ServiceHost crossDomainServiceHost = new ServiceHost(typeof(CrossDomainService));
greenwayServiceHost.Open();
crossDomainServiceHost.Open();

What do I need to change inside those three places in order to host the services inside the cloud?


回答1:


First and foremost issue is that you are using "localhost" as binding address. This will never work in Windows Azure. All the traffic in Windows Azure is directed (from the Load Balancers) to the physical internal IP address (also called DIP or Direct IP Address) of the role instance (VM).

In order to make things working in windows azure you have to bind to the DIP (Direct IP Address) of the instance of the Role and to use the port porvided by the Input Endpoint. I do it like that (for Worker Role and self hosted WCF):

 private void CreateServiceHost()
    {
        this._serviceHost = new UnityServiceHost(typeof(MyService));

        var binding = new NetTcpBinding(SecurityMode.None);
        RoleInstanceEndpoint externalEndPoint =
            RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["ServiceEndpoint"];
        string endpoint = String.Format(
            "net.tcp://{0}/MyService", externalEndPoint.IPEndpoint);
        this._serviceHost.AddServiceEndpoint(typeof(IMyService), binding, endpoint);
        this._serviceHost.Open();
    }

And this works in 100% in local dev and real live Azure environment. The thing to note here is that I building my endpoint dynamically out from the IPEndpoint instance of my Role's Input Endpoint.

I'm pretty sure your services will work as expected once you bind them to the actual IP Address and port which you take from the RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WCFEndpoint"];



来源:https://stackoverflow.com/questions/8375913/wcf-service-in-azure-workerrole-for-silverlight-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!