none
crossdomain.xml on WCF Service hosted by Windows Service RRS feed

  • Question

  • I am trying to make my Windows Service, which hosts a WCF service cross-domain compatible. After hours and hours of searching I was told that I needed to create another service which will load the crossdomain.xml and clientaccesspolicy.xml files so that the Windows hosted WCF service can be run on any domain.

    This is my main service :

    Namespace UmbrellaMobileService
    <RunInstaller(True)> _
    Public Class ProjectInstaller
        Inherits Installer
        Private process As ServiceProcessInstaller
        Private service As ServiceInstaller
        Private components As System.ComponentModel.Container
    
        Public Sub New()
            process = New ServiceProcessInstaller()
            process.Account = ServiceAccount.LocalSystem
            service = New ServiceInstaller()
            service.ServiceName = "UmbrellaMobileService"
            service.DisplayName = "Umbrella Mobile Service"
            service.Description = "Handels Umbrella Mobile Requests."
            Installers.Add(process)
            Installers.Add(service)
        End Sub
    End Class
    
    <ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)> _
    Public Class UmbrellaMobileService
        Inherits ServiceBase
        Public serviceHost As ServiceHost = Nothing
        Public CrossDomainServiceHost As ServiceHost = Nothing
    
        Public Shared Sub Main()
            ServiceBase.Run(New UmbrellaMobileService())
        End Sub
    
        Public Sub New()
            ServiceName = "UmbrellaMobileService"
        End Sub
    
        'Start the Windows service.
        Protected Overloads Overrides Sub OnStart(ByVal args As String())
            If serviceHost IsNot Nothing Then
                serviceHost.Close()
            End If
            serviceHost = New WebServiceHost(GetType(UmbrellaService), New Uri("http://localhost/UmbrellaMobileService"))
            serviceHost.AddServiceEndpoint(GetType(IUmbrellaMobileService), New WebHttpBinding(), "http://localhost/UmbrellaMobileService")
            CrossDomainServiceHost = New ServiceHost(GetType(CrossDomainService))
    
            Else
                System.Diagnostics.EventLog.WriteEntry("Umbrella Mobile Service", objIniFile.ErrorMessage, EventLogEntryType.Error)
                serviceHost.Close()
                CrossDomainServiceHost.Close()
            End If
    
            serviceHost.Open()
            CrossDomainServiceHost.Open()
        End Sub
    
        ' Stop the Windows service.
        Protected Overloads Overrides Sub OnStop()
            If serviceHost IsNot Nothing Then
                serviceHost.Close()
                CrossDomainServiceHost.Close()
                serviceHost = Nothing
            End If
        End Sub
    End Class
    
    <AspNetCompatibilityRequirements(Requirementsmode:=AspNetCompatibilityRequirementsMode.Allowed)> _
    Public Class UmbrellaService
        Inherits System.Web.Services.WebService
        Implements IUmbrellaMobileService
    
        Function GetCustomers() As Stream Implements IUmbrellaMobileService.GetCustomers
        'Function Logic
        End Function
    
    
    End Class
    
    End Namespace

    This is my Implementation of my main service:

    Namespace UmbrellaMobileService
    <ServiceContract()> _
    Public Interface IUmbrellaMobileService
    
    
        <OperationContract()> _
           <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Bare, ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
        Function GetCustomers() As Stream
    
    
    End Interface
    
    End Namespace

    This is the 'Cross Domain Service' I was advised to add:

    Namespace UmbrellaMobileService
    Public Class CrossDomainService
        Implements ICrossDomainService
        Public Function ProvidePolicyFile() As System.ServiceModel.Channels.Message Implements ICrossDomainService.ProvidePolicyFile
            Dim filestream As FileStream = File.Open("ClientAccessPolicy.xml", FileMode.Open)
            ' Either specify ClientAccessPolicy.xml file path properly
            ' or put that in \Bin folder of the console application
            Dim reader As XmlReader = XmlReader.Create(filestream)
            Dim result As System.ServiceModel.Channels.Message = Message.CreateMessage(MessageVersion.None, "", reader)
            Return result
        End Function
    End Class
    
    End Namespace

    And here is its Implementation :

    Namespace UmbrellaMobileService
    <ServiceContract()> _
    Public Interface ICrossDomainService
        <OperationContract(), WebGet(UriTemplate:="ClientAccessPolicy.xml")> _
        Function ProvidePolicyFile() As Message
    End Interface
    
    End Namespace

    My config file looks like this :

    <?xml version="1.0"?>
    <configuration>
        <system.serviceModel>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="MyServiceBehavior">
                        <serviceMetadata httpGetEnabled="true"/>
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                    </behavior>
                </serviceBehaviors>
                <endpointBehaviors>
                    <behavior name="CrossDomainServiceBehavior">
                        <webHttp/>
                </behavior>
                </endpointBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="MyServiceBehavior" name="UmbrellaMobileService.UmbrellaMobileService">
                    <endpoint address="" binding="basicHttpBinding" contract="UmbrellaMobileService.IUmbrellaMobileService">
                    </endpoint>
                    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8731/UmbrellaMobileService"/>
                        </baseAddresses>
                    </host>
                </service>
                <service name="UmbrellaMobileService.CrossDomainService">
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8731/"/>
                        </baseAddresses>
                    </host>
                    <endpoint address="" binding="webHttpBinding" contract="UmbrellaMobileService.ICrossDomainService" behaviorConfiguration="CrossDomainServiceBehavior"/>
                </service>
            </services>
        </system.serviceModel>
        <system.web>
            <compilation debug="true"/></system.web></configuration>

    Now, I get this error when I attempt to run this service:

    Service cannot be started. System.InvalidOperationException: Service has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

    I have searched and searched and still do not know what to do, can anyone give me some advice?

    Thursday, October 15, 2015 1:31 PM

Answers

All replies

    • Edited by Jimyken Friday, October 16, 2015 8:27 AM
    • Marked as answer by HanneSThEGreaT Wednesday, October 28, 2015 6:34 PM
    Friday, October 16, 2015 8:25 AM
  • Hi VinceIII.

    Thanks for your reply.  I have followed those articles, but I still get the same error?

    Friday, October 16, 2015 12:09 PM
  • Service cannot be started. System.InvalidOperationException: Service has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

    It seems to me that the config file that holds configuration information was not found. A Windows service is a exe file type of programname.exe. If there is an app.config in the VS project that holds config information, then upon the successful build of the project by VS, the app.config is made into programname.exe.config, which is the root.config for the project and it is going to be sitting in the Bin folder where the programname.exe is located. The  programname.exe.config must accompany the programname.exe, and both files must be in the same location so that .NET can find the config file and access its configuration content.

    Friday, October 16, 2015 5:55 PM
  • Hi. Thanks for all your replies.

    The files are in the same place as the service, so I do not know anymore.

    Could anyone see, based on my code, what else could be the problem?

    Monday, October 19, 2015 8:41 AM
  • OK, progress. The service is not throwing errors anymore, but my CORS still doesn't want to work....

    I added the two files as suggested and I edited my Service's OnStart event to :

           'Start the Windows service.
            Protected Overloads Overrides Sub OnStart(ByVal args As String())
                If serviceHost IsNot Nothing Then
                    serviceHost.Close()
                End If
    
                If CrossDomainServiceHost IsNot Nothing Then
                    CrossDomainServiceHost.Close()
                End If
    
                serviceHost = New WebServiceHost(GetType(UmbrellaService), New Uri("http://localhost/UmbrellaMobileService"))
                serviceHost.AddServiceEndpoint(GetType(IUmbrellaMobileService), New WebHttpBinding(), "http://localhost/UmbrellaMobileService")
    
                CrossDomainServiceHost = New WebServiceHost(GetType(CrossDomainService), New Uri("http://localhost/CrossDomainService"))
                CrossDomainServiceHost.AddServiceEndpoint(GetType(ICrossDomainService), New WebHttpBinding(), "http://localhost/CrossDomainService")
                'CrossDomainServiceHost = New ServiceHost(GetType(CrossDomainService))
    
    
    
                serviceHost.Open()
                CrossDomainServiceHost.Open()
            End Sub

    Now in my App, I am getting the same old error:

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at URL/GetCustomers. (Reason: CORS request failed). <unknown>

    Any help in enabling CORS, please?
    Monday, October 19, 2015 11:30 AM