Can't add service reference to net tcp wcf service - wcf

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.

Related

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

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.

Consume WCF service from IIS

I created a WCF Service Library in VS2012 and loaded it to my local IIS.
My client is a Windows-Mobile 6.5 device and the configuration files for the client were generated using the netcfsvcutil.exe. during debugging everything worked great but when I uploaded the service to an external IIS i keep getting a There was no endpoint listening... exception even though the netcfsvcutil.exe generated the files from the external URL. this is the web.config file located on the remote server:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="TestBinding" receiveTimeout="00:01:00" sendTimeout="00:01:00">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="MyService.CollectionService">
<endpoint address="" binding="basicHttpBinding" contract="MyService.ICollectionService" bindingConfiguration="TestBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/MyService/CollectionService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
and the client endpoint is generated like this:
public static System.ServiceModel.EndpointAddress EndpointAddress =
new System.ServiceModel.EndpointAddress("http://1.2.3.4/MyService/MyService.CollectionService.svc");
I haven't made any configuration changes when i uploaded the service to the external IIS, i simply copied & pasted the files from one virtual directory to another.
some thing the might help is that the exception is raised without any timeout.
The base address path and port do not match the endpoint address in your client configuration.
Address in your config: http://localhost:8733/Design_Time_Addresses/MyService/CollectionService/"
Address in your endpoint:
http://1.2.3.4/MyService/MyService.CollectionService.svc
You need to change your endpoint code to
public static System.ServiceModel.EndpointAddress EndpointAddress = new System.ServiceModel.EndpointAddress("http://1.2.3.4:8733/Design_Time_Addresses/MyService/CollectionService");

Can't Access Net TCP service From WCF Test Client

I'm trying to run two WCF services from within IIS one is a web service and one is a Net TCP Binding Service.
Here is a simulcrum of my Web.config (I've anonymized the service name):
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="ServerService">
<endpoint address="ServerService"
binding="netTcpBinding"
bindingConfiguration=""
name="NetTcpEndPoint"
contract="IServerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/"/>
<add baseAddress="htt://localhost:8523/"/>
</baseAddresses>
</host>
</service>
<service name="MyWebService">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration=""
name="AACCWSEndPoint"
contract="IMyWebService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8523/IMyWebService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
When I run this in the IDE it opens a page running on port 51953 and I can get the WSDL for the webservice by browsing to http://localhost:51953/WebService.svc?wsdl (Note the port is different).
I can't seem to get the WSDL by changing to port to what I've specified in the webconfig file (8523).
When I point the WcfTestClient app at "net.tcp://localhost:8523/ServerService and it gives me an error saying that it can't access the meta data, which as far as I can see I've configured (the second end point in the service).
What am I doing wrong here?
UPDATE:
I've tried changing the port number on the project properties to 8523 as suggested that didn't seem to work, I've also tried changint the address of the mex endpoint to "ServerService\mex" the test client spent some time churning but then it threw the following error:
Error: Cannot obtain Metadata from
net.tcp://localhost:8523/ServerService If this is a Windows (R)
Communication Foundation service to which you have access, please
check that you have enabled metadata publishing at the specified
address. For help enabling metadata publishing, please refer to the
MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: net.tcp://localhost:8523/ServerService Metadata
contains a reference that cannot be resolved:
'net.tcp://localhost:8523/ServerService'. You have tried to create
a channel to a service that does not support .Net Framing. It is
possible that you are encountering an HTTP endpoint. Expected
record type 'PreambleAck', found '72'.
I'm going to keep digging but I'd appreciate any help.
UPDATE 2:
I've changed the mex endpoint to a mexTcpBinding:
Here is the service tag:
<endpoint address="ServerServiceWS"
binding="wsHttpBinding"
bindingConfiguration=""
name="WSEndPoint"
contract="IServerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/"/>
<add baseAddress="http://localhost:8523/"/>
</baseAddresses>
</host>
</service>
Still no luck. Just to be sure I am entering the correct url into the tester the Url I am using is:
net.tcp://localhost:8523/ServerService
I've also Tried:
net.tcp://localhost:8523/mex
and
net.tcp://localhost:8523/
All of which give me some variation of the following error:
Error: Cannot obtain Metadata from
net.tcp://localhost:8523/ServerService If this is a Windows (R)
Communication Foundation service to which you have access, please
check that you have enabled metadata publishing at the specified
address. For help enabling metadata publishing, please refer to the
MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: net.tcp://localhost:8523/ServerService Metadata
contains a reference that cannot be resolved:
'net.tcp://localhost:8523/ServerService'. You have tried to create
a channel to a service that does not support .Net Framing. It is
possible that you are encountering an HTTP endpoint. Expected
record type 'PreambleAck', found '72'.
UPDATE 3
FWIW I think there might be a bigger problem with my WEb.config Here is what it currently looks like:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="ServerService">
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="DefaultBindingConfig"
name="NetTcpEndPoint"
contract="IServerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"
bindingConfiguration="mexBinding"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/"/>
</baseAddresses>
</host>
</service>
<service name="MyWebService">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration=""
name="MyWSEndPoint"
contract="IMyWebService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
bindingConfiguration="mexHttpBinding"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8523/MyWebService"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="DefaultBindingConfig"
maxConnections="5"
portSharingEnabled="true" >
</binding>
<binding name="mexBinding"
portSharingEnabled="true">
<security mode="None"></security>
</binding>
</netTcpBinding>
<mexTcpBinding>
<binding name="mexTcpBinding"/>
</mexTcpBinding>
<mexHttpBinding>
<binding name="mexHttpBinding"/>
</mexHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServerServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="MexBehaviour">
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I can browse to the Webservice and that allows me to get the WSDL using ?wsdl but if I put the Adddress http://localhost:8523/MyWebService into the WCF tester it throws an error too.
Error: Cannot obtain Metadata from http://localhost:8523/MyWebService
If this is a Windows (R) Communication Foundation service to which you
have access, please check that you have enabled metadata publishing at
the specified address. For help enabling metadata publishing, please
refer to the MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: http://localhost:8523/MyWebService Metadata contains
a reference that cannot be resolved:
'http://localhost:8523/MyWebService'. An error occurred while
receiving the HTTP response to http://localhost:8523/MyWebService.
This could be due to the service endpoint binding not using the HTTP
protocol. This could also be due to an HTTP request context being
aborted by the server (possibly due to the service shutting down). See
server logs for more details. The underlying connection was closed:
An unexpected error occurred on a receive. Unable to read data from
the transport connection: An existing connection was forcibly closed
by the remote host. An existing connection was forcibly closed by
the remote hostHTTP GET Error URI:
http://localhost:8523/MyWebService There was an error downloading
'http://localhost:8523/MyWebService'. The request failed with HTTP
status 404: Not Found.
I think the issue is either something to do with paths or I'm just putting the wrong URL into the test application. Either that or I've still not configured the meta data correctly.
For the HTTP endpoint, you need to reconfigure the Project Properties so that it starts your web.config defined endpoint at port 8523 if hosted using IIS Express. Use specific port (8523) instead of auto-assigned port (51953).
For the TCP metadata, you need to support a TCP-enabled mex endpoint (mexTcpBinding).
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
The net.tcp and HTTP bindings must be set on different ports on IIS.
It can be tested with the same port or different. It will crash in the first case. Also, you can use whatever port numbers you want. In this case, the TCP port can be changed to 8524. It's just that you cannot use two separate protocols on the same port. On the other hand, the base addresses make sense only for self-hosted services. Here the base address is determined by the URL from IIS.
just try to keep this as it is, and changing your service inside and paste it inside your config file, it should work perfectly.
to test the net.tcp you can use SvcUtil.exe
<system.serviceModel>
<services>
<service name="MyWCFServices.HelloWorldService">
<endpoint
binding="netTcpBinding"
bindingConfiguration="ultra"
contract="MyWCFServices.IHelloWorldService"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8086/HelloWorldService.svc" />
<add baseAddress="http://localhost:8081/HelloWorldService.svc" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="ultra"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
portSharingEnabled="false"
transactionFlow="false"
listenBacklog="2147483647" >
<security mode="None">
<message clientCredentialType="None"/>
<transport protectionLevel="None" clientCredentialType="None"/>
</security>
<reliableSession enabled="false"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Can't host tcp based wcf service on IIS, Cannot obtain Metadata from net.tcp://localhost/TestApp2/MyService error pops up

I have a simple test purpose WCF service. I'm trying to host it under IIS 7.5 ( Windows 7 ) but no luck so far. I'm getting error
Cannot obtain Metadata from net.tcp://localhost/TestApp2/MyService
I have a web site TestApp2 under Default Web Site, I enabled tcp on Default Web Site and TestApp2. Here is my web.config file, while I realize that this error simply states that I didn't have endpoint for metadata exchange, I can't see what's the problem because I included endpoint for metadata exchange.
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint address="net.tcp://localhost/TestApp2/MyService"
binding="netTcpBinding"
bindingConfiguration="PortSharingBinding"
contract="II7WcfService.IService1" />
<endpoint address="MEX"
binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost/TestApp2/MyService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="PortSharingBinding" portSharingEnabled="true">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="False" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
Thanks.
My guess is your service isn't actually called MyService but rather it is in a namespace. This means your declared endpoints are not getting picked up, and the default ones (which doesn't include IMetadataExchange) are being used instead.
Add the name of your namespace to the attribute in the config and it should work.

How to run WCF service on a specific port

I have a .Net 4.0 WCF service running on IIS. I have not specified a port so assume it is running on port 80. I need to install my service on a server where port 80 is already being used and the network guy had asked me to change my service to run on port 443. How do I do this? I'm guessing it can be configured in app.config but I can't find an article that shows me how.
Here is my current app.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
I am assuming you are running your services on net.tcp protocols.
1) Edit your bindings (right click Default Web Site select Edit Bindings
2) Server Side
<service name="YouServiceNameSpace.YourService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="YourBinding" contract="YourContract" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />
</service>
3) Client Side
<endpoint address="net.tcp://YourHost:443/YourServiceDirecotry/YourService.svc"
behaviorConfiguration="YourBehavior" binding="netTcpBinding"
bindingConfiguration="YourTcpBinding" contract="YourContract"
name="YourContractName" />
We can do it using the .csproj file of the WCF project(in case of using VS).Just change the value of this xml tag in your corresponding file:
To Run the service at port number 60000 ,
<DevelopmentServerPort>60000</DevelopmentServerPort>
Normally you should have a services node with at least one service node and each having endpoints, where you can specify the port. See more at: http://msdn.microsoft.com/en-us/library/ms733932.aspx
For example:
<services>
<service name="MyNamespace.myServiceType">
<endpoint
address="net.tcp://0.0.0.0:8000" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration1"
contract="MyContract" />
</service>
</services>
Specify the port in the address of the endpoint. See the 'Defining Endpoint Addresses in Code' section in this article for more details.