WCF hosting in Windows Services (Network Service) - c#

WCF Service
WCF service hosting in Windows Services
WinService log on as Network Service
Host app config :
<service name="StudyingControllerService.ControllerService">
<host>
<baseAddresses>
<add baseAddress="http://IP:PORT/ControllerService.svc"/>
</baseAddresses>
</host>
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IControllerService"
contract="StudyingControllerService.IControllerService" />
</service>
Everything work fine in local network. (localhost)
But I want to access my service from another computers via network.
I used another computer(from another localation), did ping IP (winservice's). ping was OK.
I installed my client and tried to connect to my remote service, but connect failed.
telnet also can't establish connection.
What is wrong?
Firewalls are disconnected. (on both sides)
On the client side telnet cant open connection to IP PORT

There is no problem with windows service or WCF. Problem was with my internet provider.
It blocked port which used my service. So, I ask to administrator to open port. and now everything is OK.
Thanks all for answers.

I had a similar issue. It was just abut opening the PORT on windows firewall.
Just to test if that would work, i turned off the firewall service.
Hope that helps!

I would try changing your base address in your config file, as follows:
<service name="StudyingControllerService.ControllerService">
<host>
<baseAddresses>
<add baseAddress="http://IP:PORT"/>
</baseAddresses>
</host>
<endpoint address="ControllerService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IControllerService"
contract="StudyingControllerService.IControllerService" />
</service>
My reason for recommending this is that the base address is used to allow endpoints to be added relative to the base address - putting the endpoint in the base address may be causing the issue.
Something to try at least - a couple minutes of editing and you'll have an answer one way or the other.

Related

Is it possible to create an other endpoint in HTTP on the same port?

One of my mate has created a C# WCF service working on Net.TCP on a specific port, it is asked to me if it is possible to create an other endpoint on this WCF service in order to consume it with HTTP protocol on the same port ?
How can I do to consume a WCF service with two differents protocols Net.TCP + HTTP ?
Thanks per advance for your answers :)
Edit : Do you have an example about how to implement a HTTP + Net.TCP endpoints in a WCF service ?
As far as I know, this is completely impossible. But service could work properly over Net.TCP+Http over different port number.
<services>
<service name="ConsoleApp3.TestService">
<endpoint address="" binding="netTcpBinding" contract="ConsoleApp3.ITestService" ></endpoint>
<endpoint address="http" binding="basicHttpBinding" contract="ConsoleApp3.ITestService"></endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:21012"/>
<add baseAddress="http://localhost:21013"/>
</baseAddresses>
</host>
</service>
</services>
Net.TCP Port sharing only is applicable for the scenario that has different service addresses, not the indeed port sharing.
Feel free to let me know if there is anything I can help with.

Error "An attempt was made to access a socket in a way forbidden by its access permissions"

Running a WCF service for testing on a local machine. In App.config I have:
<system.serviceModel>
<services>
<service name="Pizza.Services.PizzaService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733"/>
</baseAddresses>
</host>
<endpoint address="pizza"
binding="basicHttpBinding"
contract="Pizza.Services.IPizzaService">
</endpoint>
<endpoint address="net.tcp://localhost:8733/pizza"
binding="netTcpBinding"
contract="Pizza.Services.IPizzaService">
</endpoint>
</service>
</services>
If I run the app from VS2015 (as a regular user or administrator), I get:
A TCP error (10013: An attempt was made to access a socket in a way
forbidden by its access permissions) occurred while listening on IP
Endpoint=0.0.0.0:8733. ---> System.Net.Sockets.SocketException: An
attempt was made to access a socket in a way forbidden by its access
permissions
Now my co-worker has vaguely explained about a Windows 10 security feature that will prevent users from grabbing a random port and giving it away to some service. So I opened the command prompt as admin and did:
netsh http add urlacl url=http://+:8733/ user=Everyone
Still the same error.
I've noticed similar questions, but none of them address my issue, I feel.
EDIT: One more thing I forgot to mention is that I've tried turning off the firewal and anti-virus without success.
An endpoint binding was screwing things up. I've changed the port in:
<endpoint address="net.tcp://localhost:8733/pizza"
binding="netTcpBinding"
contract="Pizza.Services.IPizzaService">
</endpoint>
from 8733 to 8732 and it works. Basically, two endpoints were trying to use the same port.
Restarting Visual Studio normally fixes this issue.

Multiple endpoints - public/internal

I want to expose 2 endpoints on my WCF service. First offers standard methods for users over HTTP, second one offers "administrator-only" method ovet net.tcp.
I have only 1 class, that implements both ServiceContracts exposed by endpoints, like that:
public class ComputingModuleProvider : IComputingModuleProvider, IComputingModuleAdminProvider
My App.Config looks like this:
<services>
<service behaviorConfiguration="ComputingModuleBehaviour" name="ComputingModuleProvider.ComputingModuleProvider">
<endpoint address="ComputingModuleProvider" binding="basicHttpBinding" contract="ComputingModuleProvider.IComputingModuleProvider" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="ComputingModuleAdminProvider" binding="netTcpBinding" contract="ComputingModuleProvider.IComputingModuleAdminProvider" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8091/ComputingModuleProvider"/>
<add baseAddress="net.tcp://localhost:8092/ComputingModuleProvider" />
</baseAddresses>
</host>
</service>
</services>
Now the situation is that I have the service on the server-computer in our office and the client is on my personal computer (but both of them are connected to local network).
Still, I had to add an exception to server firewall and allow communication on ports 8091 and 8092 in order to be able to add service reference and generate proxy on client side. With this exception allowed, I can add service reference using both net.tcp and http base adress.
The problem is, that I see both ServiceContracs for each base adress, like in the image below:
I expected, that for net.tcp I would only see IComputingModuleAdminProvider and for http, I would see IComputingModuleProvider.
I also tried to exclude port 8092 from allowed ports on server firewall - that resulted in being able to add service reference only by using http base adress, but still, I could see both Service contracts exposed, like in image above.
So the administrator-only method is available over http as well.
Question:
Am I doing something wrong in my app.config file or is this the standard behaviour? I know I am on the same local network, but still, I had to add the exception in firewall, thus I expected, that IComputingModuleAdminProvider would not be available over http protocol.ยจ
UPDATE:
Ass suggested by Otiel, if I create new ServiceContract like this:
public class ComputingModuleAdminProvider : IComputingModuleAdminProvider
And mark it as a new <service> in App.Config, I can separate the admin and standard interface.

Windows Service Hosted TCP WCF Service

I am trying to host a WCF service on a windows 2008 R2 server as a windows service. I followed the directions provided by msdn (found here). Everything works fine as long as everything is part of the same solution in visual studio. However, I tried creating a client in a different solution (on the same machine) and it can't find the service. I get an 'Add Service Reference Error' shown below.
My goal is to be able to access the wcf service remotely, but I can't seem to even access it locally unless the client was created within the same client. Is there any guides, tutorials, or helpful hints that anyone can give me to get this to work?
Update:
It seems that even though the windows service is running, the WCF service doesn't seem to be listening to any ports. Which would suggest that it isn't running. This also explains why everyone's first thought I didn't have the service running. I had assumed that since the windows service was running and that the same solution client worked, that the WCF service was working as well. Turns out that Visual Studio was starting up a WCF service whenever I ran the same solution client.
So, why isn't the windows service starting the WCF service? Any ideas?
It turns out that there was a problem with the tutorial provided by MSDN (provided in the question above). They named both the windows service and the WCF service Service1 which is the default name for both of them.
The windows service was suppose to start the WCF service, however, it was actually trying to start itself again because both services had the same name.
myServiceHost = new ServiceHost(typeof(Service1));
To fix this problem, you can rename one of the services or fully qualify the WCF service when referenced in the windows service.
myServiceHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
The funny thing is that the code provided still looks like it works because Visual Studio is smart enough to notice that the WCF service isn't running and it starts an instance up behind the scenes.
It was a simple bug to fix, but because Visual Studio was hiding the problem from me, I don't think I would have found it without the help from Espen Burud.
There are two ways for Add Service Reference to learn about a service:
Discover button: searches the projects in the current solution.
Go button: connects to the service in the Address box and retrieves the metadata.
You need to actually have the service running before you click Go.
EDIT
I just noticed from your screenshot that you're trying to connect to a net.tcp URL. I think it's more common to use http for MEX. Your app.config would look something like:
<services>
<service behaviorConfiguration="WcfServiceLibrary1.Service1Behavior"
name="WcfServiceLibrary1.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/Service1" />
<add baseAddress="http://localhost:8524/Service1" />
</baseAddresses>
</host>
</service>
</services>
Note the different port number for the http base address. You would then use "http://localhost:8524/Service1" in the Add Service Reference tool. You should also be able to connect to it with your web browser.
To allow metadata exchange via http GET (e.g. from a browser), you also need to enable it via a behavior:
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
I'm not sure if the Add Service Reference tool cares about that.
Even if you don't want to allow http get access (httpGetEnabled="False"), you still need to include this behavior to enable MEX (unless you're adding it programatically).
I have tested the MSDN article in the and it works without modifications. If the firewall is enabled on the server, I think you will need to add some rules for your service.
To verify that the service are listening on the correct tcp port, you can use command: netstat -a. If the service are listening on the correct port, this command will return:
Proto Local Address Foreign Address State
TCP 0.0.0.0:8523 machinename:0 LISTENING
I managed to figure out the issue. My service didn't know about the endpoints because I hadn't copied the service configuration from the app.config in the WCF project into the app.config of the actual windows service. Once I did that it functioned correctly.
This was not made clear in the original MSDN article which I had also followed although it is mentioned in a comment in the WCF app.config.

How to publish WCF service in internet (hosted in wnd service)

I have a windows service on my laptop, which hosts a WCF service. I would like to use these service on my ASP.NET website, which is on external ASP.NET server.
Could you help me, how to do this?
Is it necessary a specific laptop configuration for that? What should I configure?
And binding, what type will adequate? .. Right now, I've got:
<service behaviorConfiguration="WcfServices.InfoBehavior" name="MyProgram.WcfServices.Info.Info">
<endpoint address="" binding="wsHttpBinding" contract="MyProgram.WcfServices.Info.IInfo">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Info/" />
</baseAddresses>
</host>
</service>
UPDATE:
Right now, my client app is still on my laptop (it is not publish yet).. This is my client code:
<client>
<endpoint address="http://localhost:8732/Info/" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IInfo" contract="ServiceInfo.IInfo"
name="WSHttpBinding_IInfo">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
I don't know, what binding use.. what port, what settings should be changed on my laptop?
Unless your laptop has its own fixed IP address exposed externally (most unlikely) I think you will find it hard to do this directly.
You might consider using Azure Service Bus to broker message exchanges: I believe this is one way to solve the problem of accessing a service hosted on a non-constant IP address or behind a firewall/NAT.
Or you could consider changing your design to turn things the other way around. That is, when it is connected and running, your laptop service connects to a service hosted on the ASP.NET box, over a duplex binding, with your current service contract as the callback contract.
If you have a WCF service running on your laptop hosted via ServiceHost you'll need to duplicate that configuration in your ASP.NET web.config file, as well as add a "service.svc" file which is referenced to the Interface of your service.
You should change localhost with real external facing IP address of your laptop and it could work if your router at home has no firewall. Change it in both client and server endpoint address.

Categories

Resources