none
About Wcf Hosted in Windows Service RRS feed

  • Question

  • hi ;

    I am developing a WCF that Hosted in Windows service , I can add to WCF as reference

    if WCF and Client in same network , otherwise I Can't

    Wcf App.Config

    <configuration>

    <system.web>

    <compilation debug="true"/>

    </system.web>

    <system.serviceModel>

    <services>

    <service name="WCFServiceHostingWinService.Service1">

    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="WCFServiceHostingWinService.IService1"/>

    <host>

    <baseAddresses>

    <add baseAddress="http://localhost:8080/Service1"/>

    </baseAddresses>

    </host>

    </service>

    </services>

    <behaviors>

    <serviceBehaviors>

    <behavior>

    <serviceMetadata httpGetEnabled="True"/>

    <serviceDebug includeExceptionDetailInFaults="True"/>

    </behavior>

    </serviceBehaviors>

    </behaviors>

    </system.serviceModel>

    </configuration>

    /* Windows Service /*

     ServiceHost sh= null ;
    protected override void OnStart(string[] args)
      {

       if (sh != null)
         {

        sh.Close();

          } 
       sh = new ServiceHost(typeof(MySampleWinService));
       sh.Open();

      }

    protected override void OnStop()
      {
       if(sh!=null)
       {
        sh.Close();
        sh = null;

       }  
      }

    Thanks in advance .


    • Edited by Aly14 Friday, August 18, 2017 9:47 AM a
    Friday, August 18, 2017 9:46 AM

Answers

  • finally it's working ,

    actually it was so simply ,no need to extend the process ,

    the main problem is misconfigured app.config file that I said :)

    I am sorry I cant mark the your's solution as a answer .

    But , thank you for spend time .

    • Marked as answer by Aly14 Wednesday, August 23, 2017 3:46 PM
    Wednesday, August 23, 2017 3:46 PM

All replies

  • <addbaseAddress="http://localhost:8080/Service1"/>

    What is localhost or IP 127.0.0.1?

    https://en.wikipedia.org/wiki/Localhost

    What is an IP?https://en.wikipedia.org/wiki/IP_address

    Friday, August 18, 2017 11:05 AM
  • if you mean local host ip ,it's 192.168.16.3

    also I forwarded the 8080 port to WCF machine and its open and working.

    Friday, August 18, 2017 1:30 PM
  • if you mean local host ip ,it's 192.168.16.3

    also I forwarded the 8080 port to WCF machine and its open and working.

    That is not what the client-side config indicates that you have posted, which is using localhost or 127.0.0.1 the loopback IP. The traffic is never leaving the client machine as it is being looped back to the client machine, localhost, assuming the WCF service is running locally on the machine too.

    localhost would have to be changed to the IP address of the machine that is hosting the WCF service if the service is hosted on a machine on the LAN or WAN.

    http://www.webopedia.com/TERM/L/loopback_address.html

    Friday, August 18, 2017 5:09 PM
  • I try to add WCF as reference in visual studio ,app.config is default .

    I am starting the story explain to you :)

    I did create 3 project in visual studio , Windows Service , WCF library and Client app.

    I configured Windows Service and WCF , I install the windows service that included WCF remote side

    The Windows service is respond to specific port and its working

    I try to add WCF in client app. via Visual Studio and I am not connecting the WCF

    WCF and Client app. away from each other

    I think the image that attached explain all you need

    Friday, August 18, 2017 9:47 PM
  • I try to add WCF as reference in visual studio ,app.config is default

    What you tried to do is add a ASP.NET WCF Web service reference with the service that is supposed to be deployed to local IIS the real one, and it is not deployed on local IIS. And what you are getting is an HTTP return code of 'not found' because the service is not deployed to local IIS the real one. IIS Express is being used, which is used by Visual Studio development run the solution is run in VS. 

    I don't know what version of the Windows O/S you are using,  but IIS the real one has to be configured to even host a WCF Web service and some O/S(s) must be a professional workstation O/S and not a 'home' edition.

    I configured Windows Service and WCF , I install the windows service that included WCF remote side

    No that doesn't seem right as the WCF Web service would have to be deployed to the local real IIS. I suspect the IIS Express is being used.

      

    Friday, August 18, 2017 11:07 PM
  • Hi DA924x

    I created the all project under Windows Application , Not as a Web (ASP.NET or MVC)

    I will show you other screen .

    Saturday, August 19, 2017 8:24 AM
  • Hi ;

    I did review this and other article ,by the end I can connect to WCF if the client in same location otherwise I can't

    Saturday, August 19, 2017 8:28 AM
  • http://www.csharptutorial.in/30/csharp-net-how-to-consume-wcf-service-hosted-as-window-services

    see if you can get your client program to consume the service, and the windows service hosting the WCF service must be running on the machine without VS hosting the Windows service. 

    Saturday, August 19, 2017 12:23 PM
  • Hi Aly14,

    Could you share us the address you used to access in the same network and different network? What do you mean by the different network? Could you Ping the server from client? I assume you need to replace localhost by Public IP address.

    Per to your screen shot, I am not sure how your service could work in the same network. Your service basic address is “http:..//service1”, but you used http://MySampleWinService address, they are mismatch, they should keep the same.

    In addition, I suggest you check the firewall whether you enabled 8080 port, or you could disable firewall to check whether it is related with Firewall.

    Best Regards,

    Edward 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 21, 2017 3:27 AM
  • hi Edward ,

    I am so happy to see you again :)

    * I developed all Project in A location ,the local ip adres of A location 192.168.2.6

    * I did install the Service in B location ,

    the local ip adress of B location 192.168.16.10 also the externall ip of B location 92.84.70.150

    * I created a testclient in A location and I try to add reference the Service Whic is B location

    But I cant , all scenario is working in same location bu not working remotely.

    I just want to create a WCF that hosted in Windows Service and then I want to deploy remote location and I want to use it anywhere adding as reference.

    Note : Remote WCF location service is started and the firewall allow to 8080 port (ALL Tested)

    Thanks in advance.

    Monday, August 21, 2017 10:58 AM
  • the local ip adress of B location 192.168.16.10 also the externall ip of B location 92.84.70.150

    * I created a testclient in A location and I try to add reference the Service Whic is B location

    But I cant , all scenario is working in same location bu not working remotely.

    It's  never going to work remotely.  92.84.70.150 is an Internet IP, that is known publically and is provided by the Internet Service Provider. Your service is setting on a machine on the protected LAN using a LAN IP behind router or a firewall.

    The only way traffic is going to reach the service on a machine on the protected LAN when the external client is using the Internet IP is by port forwarding firewall rules where inbound traffic is forwarded/mapped for the Internet IP  to a machine using a LAN IP that is hosting the service. The machine on the LAN hosting the service should be using a static IP and not a DHCP IP.

    https://en.wikipedia.org/wiki/Port_forwarding

    Monday, August 21, 2017 1:36 PM
  • I know and I configured it .

    I did install and start the IIS and test it .

    When I type to browser  http://92.84.70.150:8080/iisstart.htm  it was ok . Don try it's fake adres :)

    and I did stop the iis service  and start the WCF service , But it's not working .

    Monday, August 21, 2017 2:04 PM
  • I know and I configured it .

    I did install and start the IIS and test it .

    When I type to browser  http://92.84.70.150:8080/iisstart.htm  it was ok . Don try it's fake adres :)

    and I did stop the iis service  and start the WCF service , But it's not working .

    What is it? On one hand, you say that IIS is not being used and the whole thing is Windows Service hosting a WCF service.

    And now you are showing IISstart.htm? I would expect to see some URL pointing to the WCF Service with the service page being shown indicating that the service was sitting there and listening and contact was made from a client's perspective.

    I would expect the same thing with the WCF service hosted by the Windows service that you see the WCF service page, before you tried to set a Service Reference to the service.

    Monday, August 21, 2017 3:39 PM
  • oh my God ,

    I think you dont want to understand me ,

    I said , I just install the iis for that firewall rule and port forwarding is working or not ?

    Understand ?

    Monday, August 21, 2017 3:59 PM
  • oh my God ,

    I think you dont want to understand me ,

    I said , I just install the iis for that firewall rule and port forwarding is working or not ?

    Understand ?

    Installed IIS for what,  because you are using TCP port 8080? If you are not developing a WCF service an ASP.NET WCF Web service, which you can't be if developing a WCF service that is using TCP/IP hosted by a Windows service, then what is the point of installing IIS?

    A firewall port forwarding rule is to forward inbound traffic to a LAN IP that a computer is using that has a service running and listening on a port. The service can be a WCF Web Service using HTTP, HTTPS or it can be using a WCF Service using TCP/IP. 

    So no, port forwarding is not working for you based on your statements. And in order to test if port forwarding was working, you would have to be in the Internet like at WiFi café using the Internet IP. 

    Where did you get the Internet IP of 92.84.70.150? 

    Monday, August 21, 2017 5:58 PM
  • if so ,

    could you give me step by step ,

    how to implement a wcf that hosted in Windows service remotely

    I want create Project in visual studio and then I deploy the service to remote computer and

    able to add from anywhere .Nettcpbinding or httpbinding doesn't matter .

    Monday, August 21, 2017 8:47 PM
  • https://msdn.microsoft.com/en-us/library/ff649818.aspx

    you should get it work on the LAN between a machine hosting the service with a machine hosting the client, that's remote.

    Exposing a machine to the public Internet running the Windows O/S is risky business for the inexperienced. If the O/S, file system, registry and user accounts are not harden to attack, with 1,000 page books covering this and the experts can hardly do it, then the inexperienced have little chance with none leading. You'll just be exposing some hack bait that will be used by hackers to hack networks and machines on the Internet.

    You best find a hosting provider if you want to do something with the Internet.

      

    Monday, August 21, 2017 11:04 PM
  • Hi Aly14,

    For iisstart.html which is B location, do you access this address from A location?

    Which base address did you use after hosting the service in B location? You need to change the baseAddress to 92.84.70.150 after hosting the service in B.

    I made a test with below steps for Intranet.

    1. Create a Console Application in location B, add wcf service.(both console application and windows service are self-host, it would not make difference)

    App.config

        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="">
                        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                        <serviceDebug includeExceptionDetailInFaults="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service name="ConsoleApplication1.Service1">
                    <endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication1.IService1">
                        <identity>
                            <dns value="localhost" />
                        </identity>
                    </endpoint>
                    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://Intranet IP:8080/Service1/" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
    

    Code

                ServiceHost host = new ServiceHost(typeof(Service1));
                host.Open();
                Console.WriteLine("Service is start");
                Console.ReadLine();
    

    2. Access http://B Intranet IP:8080/Service1/ in Location B

    3. Check whether above URL is accessible in Location A

    I do not have public address to test, if you want to access the service over Internet, you need to change B Intranet IP to B Internet IP.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 22, 2017 2:01 AM
  • * I create a Console App and configured WCF structure and deploy it to remote side

    * I can acces to wcf that hosted remote via browser

    * But Still I can't add as a reference any Project in visual studio from other side


    • Edited by Aly14 Tuesday, August 22, 2017 8:31 AM a
    Tuesday, August 22, 2017 8:19 AM
  • * But Still I can't add as a reference any Project in visual studio from other side

    For VS,  the reference must be done from things on the local machine and the reference is set to the service in the client program from a local machine standpoint, which is done one time.

    An app.config is created when  the Add Service reference is done successfully with the required client-side information and the endpoint set to localhost in the config file.

    All VS projects that create a programname.exe that has an app.config in the project, the app.config is copied over to a runtime config called programname.exe.config upon the successful build of the project that is sitting in the Bin folder along with the programname.exe.

    The runtime config is where .NET is going to look for any config information. The runtime config must always reside at the location of the programname.exe config so that .NET can find the config information.

    So it's back to the localhost that is the endpoint location in the client-side config file that specifies the endpoint TCP/IP address of the service from a client's perspective when the Service Reference was successfully established in the solution, because you set the whole thing up through VS from a local machine perspective.

    Localhost in the config file could be changed to 127.0.0.1 which is the same thing as localhost, if the client and service are sitting on the same machine. If the WCF service is on a LAN machine, the localhost in the client-side config endpoint must be changed from localhost to 192.168.1.10, as an example, if the client and service machines are on the LAN.

    IF the service is sitting on a machine on the Internet, then localhost on the client-side config file endpoint for the client must be changed to the Internet IP, like 92.84.70.150 as an example,  so that the client can find the service. 

    The kicker for you is that you change the endpoint in the config file for the service from a client's perspective in the programname.exe.config, the runtime config. The runtime config can be edited with a text editor and saved.

    So, the service reference in your case and VS is all done from a local machine standpoint, and it is the config file information that must be changed from localhost to whatever the IP/machine that is hosting the service.

    Tuesday, August 22, 2017 1:12 PM
  • The client is located in LAN and the WCF is located in WAN

    I modified app.cofig endpoint adres {localhost,127.0.0.1,192.168.16.10,92.84.70.150 }

    but , the problem is continue .

    Tuesday, August 22, 2017 3:34 PM
  • The client is located in LAN and the WCF is located in WAN.

    My god do you not understand what I have written? Are you deliberately doing this? :)

    Again, the whole thing should have been dealt with on your local machine with windows service and WCF service running on your local machine. VS with your test client is loaded up and you set the Service Reference to the WCF service when found on the local machine.

    The service references set inside the client program and the client program itself could care less as to how the references are set nor does the client care about the location of the WCF service when the service references are created for the client, as long as VS  found the WCF service so references are set for the client.

    Then it's a matter of you GOING to the config file that was talked about and setting the IP to the location of the WCF Service on the WAN.

    You either make the connection or you don't make the connection.

    You can take the URL that is present at the Service Reference screen, copy the URL to a Web browser, change localhost the to WAN IP and try to access the WCF service on the WAN. You either comeback with service page in the browser, the site was found, the service is running and listening for inbound traffic, or the site is not found. 

    Tuesday, August 22, 2017 4:33 PM
  • I already described the screeshoot or all you need above,

    you are writing a answer like article ,so its so long but not include any information that I needed,

    the answer of question is simple , if you a develeoper or expert on WCF it's so easiliy for you ,if not we can wait other one .

    I think it's because of misconfigured app.config

    We just need to simple app.config

    is it possible to test in your's enviorement

    Wednesday, August 23, 2017 9:39 AM
  • I think it's because of misconfigured app.config

    We just need to simple app.config

    You got the simple and properly configured app.config for the client, if you ever set the Service References by using Add Service Reference in VS  and you actually found the service running on your local machine via VS.  

    Then it's a matter of changing localhost in the app.config to the remote IP. I mean it's that simple.

    The question is this. Do you have everything set up correctly network-wise for the client to make the initial contact with the service like configuring the gateway firewall and Windows firewall too running the remote hosting machine to allow the initial contact with the service.

    I also question are you dealing with the right config file, like the runtime config file that .NET looks at during program runtime for configuration informaation.

    An app.config is only used in debug mode for the program when program is running in VS with the debugger. 

    I also question if you are hitting the WAN network properly when running the WCF client from wherever you are trying to run the client program.

    Wednesday, August 23, 2017 12:18 PM
  • finally it's working ,

    actually it was so simply ,no need to extend the process ,

    the main problem is misconfigured app.config file that I said :)

    I am sorry I cant mark the your's solution as a answer .

    But , thank you for spend time .

    • Marked as answer by Aly14 Wednesday, August 23, 2017 3:46 PM
    Wednesday, August 23, 2017 3:46 PM