why I can't access wcf hosted in windows service? - wcf

I have a WCF service hosted in windows service.
when I am trying to access the service I am getting below error message.
No connection could be made because the target machine actively refused it 127.0.0.1:9002
Inner Exception
There was no endpoint listening at http://localhost:9002/MainService/Service that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
But endpoints has been defined in App config of WCF as below.
<system.serviceModel>
<services>
<service name="MainService.CalculatorService">
<endpoint address="CalculatorService" binding="basicHttpBinding" contract="MainService.ICalculator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9002/MainService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
web config of the client
<client>
<endpoint address="http://localhost:9002/MainService/CalculatorService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INoiseCalculator"
contract="Service.ICalculator" name="BasicHttpBinding_INoiseCalculator" />
</client>
In Controller I am accessing service as per below
MainService.CalculatorClient proxy = new MainService.CalculatorClient();
proxy.getDetails();
I have opened up the port in the firewall as well.
I can't figure out what's wrong because when service gets self hosted in WccSvcHost it works fine but after deployment it doesn't work.

It seems that there is something wrong with the service running state. For verifying this, we could talk about the client endpoint at first.
<client>
<endpoint address="http://localhost:9002/MainService/CalculatorService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INoiseCalculator"
contract="Service.ICalculator" name="BasicHttpBinding_INoiseCalculator" />
</client>
The contract is Service.ICalculator, while the namespace you are using to instantiate the client proxy is MainService
MainService.CalculatorClient proxy = new MainService.CalculatorClient();
proxy.getDetails();
Is the client service endpoint automatically generated by adding service reference? Why the namespace is incongruity?
I suggest you generate the client endpoint again by adding service reference on the client-side, with this, we can check if the service is working well.
About calling the service by adding service reference.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Feel free to let me know if the problem still exists.

Related

Role of baseAddress while configurting WCF Service EndPoint

I have created simple WCF service and configured its endpoint as bellow.
<services>
<service name="AsynchWCFService.MathOperation">
<endpoint address="MathsOperation" binding="wsHttpBinding" contract="AsynchWCFService.IMathOperation">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/OperationService/" />
</baseAddresses>
</host>
</service>
</services>
I have hosted this WCF service in a stand alone exe. I am expecting that my service will be accessible at below address.
http://localhost:8080/OperationService/MathsOperation/
But service is accessible at http://localhost:8080/OperationService/
I want to access service using http://localhost:8080/OperationService/MathsOperation/ link. Can any one help me?
I don't think your service is available at http://localhost:8080/OperationService. What you see there is just a HTML page created by WCF which describes available mex endpoints or path to WSDL.
These mex endpoints describe the ABC of your WCF service where A = address => http://localhost:8080/OperationService/MathsOperation/. Potential clients know about your service url by querying the mex endpoint.
By default, this HTML page will show up at your base address. However, you can disable this page or set it to appear at some different url by using serviceDebug behavior.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug httpHelpPageUrl="http://localhost:8080/OperationService/myhelppage"
/> <!-- use httpHelpPageEnabled="false" to disable the page -->
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Unfortunately, I don't think you can set httpHelpPageUrl to same address as your service endpoint.

Referencing a Wcf Tcp service from within the same solution

I am going through the walkthrough "How to: Use netTcpBinding with Windows Authentication and Transport Security in WCF Calling from Windows Forms"
http://msdn.microsoft.com/en-us/library/ff647180.aspx
When I get to "Step 7: Create a Test Client Application" and substep 5: "5.In the Add Service Reference dialog box, set the Service URI: to net.tcp://localhost:8523/WCFTestService and then click Go"
I get "The URI prefix is not recognized. Metadata contains a reference that cannot be resolved".
It's as if the "Add Service Reference" doesn't recognize the "net.tcp" binding.
I'm on Windows 7 x64 using Visual Studio 2012 and my project is targeting x86 and Framework 4.5.
I found this thread
An error in the MSDN walk-through - "How to: Host WCF in a Windows Service Using TCP"
But they didn't have a problem when the client was in the same solution. I am.
Service Config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Publisher.MyServiceBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="POC_WcfTcpSubscribePublishService.Publisher">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
name="NetTcpBindingEndpoint" contract="POC_WcfTcpSubscribePublishService.IPublisher">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="MexTcpBindingEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/WcfTcpSubscribePublish" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
any pointers are welcome.
The service must be running before you can add a reference to it.
You can run the service project separately from the context menu within Solution Explorer.
You have a service behavior: Publisher.MyServiceBehavior
But you don't apply that service behavior to your service. After service name="..." do this:
behaviorConfiguration="Publisher.MyServiceBehavior"

Can't add service reference to net tcp wcf service

I have a WCF service hosted on my local machine as windows service. Now I'm trying to add a service reference in the client (again on my local dev machine) but I'm getting an error
Metadata contains a reference that cannot be resolved:
'net.tcp://localhost:8523/Service1'. Could not connect to
net.tcp://localhost:8523/Service1. The connection attempt lasted for a
time span of 00:00:02.0011145. TCP error code 10061: No connection
could be made because the target machine actively refused it
I checked that the Windows firewall doesn't block port 8523. I even created a new rule in Windows firewall to allow run 8523 port. But when I'm running netstat -sp tcp I can't seem to find port 8523. But I can see Serice1 service's state is set to START in Services. This is the config files
Service Library
<system.serviceModel>
<services>
<service 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" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Config in the windows service project looks identical.
Michael, this is a net.tcp binding that I typically use for a WCF service.
<bindings>
<netTcpBinding>
<binding name="TcpBinding"
receiveTimeout="Infinite"
sendTimeout="Infinite"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<reliableSession inactivityTimeout="Infinite"/>
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
Try to add it to your configuration:
<service name="WcfServiceLibrary1.Service1">
<endpoint
address=""
binding="netTcpBinding" bindingConfiguration="TcpBinding"
contract="WcfServiceLibrary1.IService1">
...
Also, my services are running under ServiceAccount.LocalSystem.
Check
netstat -ap tcp
if the service is listening.
EDIT: my Service class below. Note that the current directory of the windows service is set programatically to the BaseDirectory, i.e. the directory of the executable.
public class Service : ServiceBase
{
public static void Main()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
ServiceBase.Run(new Service());
}
...
}
You've specified a behavior but haven't given it a name. Give the behavior a name and point to it using behaviorConfiguration in your service.
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="svc1behavior">
...
<serviceBehaviors>
<behavior name="svc1behavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
Check that Net.Tcp listener adapter service is running (although if it works while hosting the service in console application, it should be running).
To find the port your service is using,
Open Task Manager
Select View -> Select Columns -> Check PID -> OK
Find your service process name, note the PID
Run the followind command from the command prompt: netstat -ano | findstr *PID*
Had the same issue. After a lot of troubleshooting I found out I was missing a reference to the service in Web.config in the service project, so I added this to Web.config:
<system.serviceModel>
<services>
<service name="serviceName" behaviorConfiguration="BasicServiceBehavior" >
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicServiceBasicHttpBinding" contract="(I<nameofservice>)"/>
</service>
</services>
</system.serviceModel>
After this a rebuilt the solution (which created the interface) and republished. Then I could finally add a service reference from my other project.

mexHttpBinding - Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract

I know this has been asked many times, and answered many times, but, all the provided samples that should be working don't seem to want to work for me today.
When I try to start the host, I keep getting the following error:
"The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service TraceService. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract."
My service is being hosted in a managed windows service host as per Microsoft's example: http://msdn.microsoft.com/en-us/library/ms733069%28v=vs.90%29.aspx
And here is my nice and simple config:
<system.serviceModel>
<services>
<service name="Daff.Lae.Service.TraceService">
<endpoint address="" binding="wsHttpBinding" name="TraceService" contract="Contracts.Service.ITraceService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/TraceService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Of course, the problem becomes more interesting when there are no errors if I remove this line:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
Any help would be very very very greatly appreciated :)
Be sure to specify a behaviorConfiguration in the service element of your configuration in order to allow either httpGet or httpsGet.
I see that you have already defined a serviceBehavior named DefaultBehavior - now all you need to do is add behaviorConfiguration="DefaultBehavior" to the service element, so that line becomes:
<service name="Daff.Lae.Service.TraceService" behaviorConfiguration="DefaultBehavior">
If you don't explicitly specify a behavior for your service, both HTTP GETs and HTTPS GETs are disallowed by default, and your metadata will not be exposed.
As you're using WS-Http you are binding to an HTTPS protocol, so you need to use the correct MEX binding;
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
and change the baseaddress to a https one.
Or (the other way around) convert your wsHttp binding to a basicHttp binding and things will start working for you.
`<services>
<service name="MyService.Service1" behaviorConfiguration="Service1" >
</services>
`
where MyService is the application name , Service1 is the default implementation class for IService1
`
<protocolMapping>
//Remove any http or https bindings provided
</protocolMapping>
`
It should help when you use WCF Application Project

WCF unable to find netTcpBinding

I am trying to create a WCF service that is accessible through both webHttpBinding and netTcpBinding. I have been successful in getting the webHttpBinding to be accessible through a Java client and now I'm working on trying to get the netTcpBinding working.
I have set up the configuration like this;
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="httpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="MR_Jukebox_Service.JukeboxService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/MR_Jukebox_Service/"/>
<add baseAddress="net.tcp://localhost:8523/Design_Time_Addresses/MR_Jukebox_Service/net/"/>
</baseAddresses>
</host>
<endpoint address=""
behaviorConfiguration="httpBehavior"
binding="webHttpBinding"
contract="MR_Jukebox_Service.IJukeboxService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="MR_Jukebox_Service.IJukeboxService" />
<endpoint address="net.tcp://localhost:8523/Design_Time_Addresses/MR_Jukebox_Service/net"
binding="netTcpBinding"
bindingConfiguration=""
contract="MR_Jukebox_Service.IJukeboxService" />
</service>
</services>
</system.serviceModel>
In the same solution, I have a test application that I wish to connect to the netTcpBinding, I've right clicked on "Service References" and chosen "Add Service Reference...".
When I click on "Discover" it finds the service although says;
There was an error downloading 'http://localhost:8732/Design_Time_Addresses/MR_Jukebox_Service'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://localhost:8732/Design_Time_Addresses/MR_Jukebox_Service'.
There was no endpoint listening at http://localhost:8732/Design_Time_Addresses/MR_Jukebox_Service that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
But I am also unable to see the netTcpBinding in order for me to create a service reference to it.
I was wondering whether anyone can see what I am doing wrong as its probably something rather simple, but due to my lack of experience with WCF haven't noticed.
Thanks for any help in advance.
Try changing your mex endpoint to this:
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
You were using your service's contract for the mex endpoint, which I don't believe will work.
You can set up a similar one for the NetTcpBinding:
<endpoint address="net.tcp://localhost:8523/Design_Time_Addresses/MR_Jukebox_Service/net/mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
I have been successful in getting the webHttpBinding to be accessible through a Java client and now I'm working on trying to get the netTcpBinding working.
Are you trying to get the netTcpBinding to work with a java client ? Because, netTcpBinding only works with a .net client.
NetTcpBinding is not designed for interop, it's designed for performance when both the server and client are .net