Timeout error when accessing WCF service remotely - c#

When I access the WCF service locally it works. To do this I type into my browser: http://localhost:54123/MyService/GetValue
This shows my expected json formatted output. However, when accessing remotely using http://myIPAddress:54123/MyService/GetValue I get ERR_CONNECTION_TIMED_OUT in Chrome.
I have my inbound IP whitelisted for all TCP ports, so I am not sure why I would be unable to access remotely. This is being hosted on an amazon EC2 instance if that makes any difference.
Here is the code I have in my main() method for hosting the service via Topshelf
const string serviceUri = "http://localhost:54123/MyService";
var host = HostFactory.New(configurator =>
{
configurator.Service<WcfServiceWrapper<MyServiceClass, IMyServiceClass>>(serviceConfigurator =>
{
serviceConfigurator.ConstructUsing(x =>
new WcfServiceWrapper<MyServiceClass, IMyServiceClass>("MyService", serviceUri));
serviceConfigurator.WhenStarted(service => service.Start());
serviceConfigurator.WhenStopped(service => service.Stop());
});
configurator.RunAsLocalSystem();
configurator.SetDescription("Runs My Service.");
configurator.SetDisplayName("MyService");
configurator.SetServiceName("MyService");
});
Here is the relevant code from my WcfWrapper start() method
var webHttpBinding = new WebHttpBinding(WebHttpSecurityMode.None);
_serviceHost.AddServiceEndpoint(typeof(TServiceContract), webHttpBinding, _serviceUri);
var webHttpBehavior = new WebHttpBehavior
{
DefaultOutgoingResponseFormat = WebMessageFormat.Json
};
_serviceHost.Description.Endpoints[0].Behaviors.Add(webHttpBehavior);
_serviceHost.Open();
openSucceeded = true;
Below is what I have in my config file
<configuration>
<system.serviceModel>
<services>
<service name="MyServiceClassNS.MyServiceClass">
<host>
<baseAddresses>
<add baseAddress="http://myIPAddress:54123/MyService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Most likely cause of the problem is that the firewall is blocking the call.

Related

Error in deploying WCF service on WPF application

I am working on WCF application, starting simple HelloWorld service. I have develop simple WPF application to host services, i.e. start and stop service
when I try to test this service with url "http://localhost:8087/MyServices/HelloWorldService" on WCFTestClient, I am getting following error
Error: Cannot obtain Metadata from http://localhost:8087/MyServices/HelloWorldService 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:8087/CreditUnionServices/HelloWorldService Metadata contains a reference that cannot be resolved: 'http://localhost:8087/CreditUnionServices/HelloWorldService'. <?xml version="1.0" encoding="utf-16"?><Fault xmlns="http://www.w3.org/2003/05/soap-envelope"><Code><Value>Sender</Value><Subcode><Value xmlns:a="http://schemas.xmlsoap.org/ws/2005/02/sc">a:BadContextToken</Value></Subcode></Code><Reason><Text xml:lang="en-GB">The message could not be processed. This is most likely because the action 'http://schemas.xmlsoap.org/ws/2004/09/transfer/Get' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.</Text></Reason></Fault>HTTP GET Error URI: http://localhost:8087/CreditUnionServices/HelloWorldService There was an error downloading 'http://localhost:8087/CreditUnionServices/HelloWorldService'. The request failed with HTTP status 400: Bad Request.
app.config
<system.serviceModel>
<services>
<service name="App.Services.Managers.HelloWorldManager">
<endpoint address="HelloWorldService"
binding="wsHttpBinding"
contract="App.Services.Contracts.IHelloWorldService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8087/MyService"/>
</baseAddresses>
</host>
</service>
</services>
C# class to open and close service
public class ServicesHostManager
{
ServiceHost _helloWorldServicesHost = new ServiceHost(typeof(HelloWorldManager));
public void ProcessHelloWorldService(string _process)
{
if(!string.IsNullOrEmpty(_process))
{
try
{
if (_process.Equals("open_service"))
{
_helloWorldServicesHost.Open();
}
else if (_process.Equals("close_service"))
{
_helloWorldServicesHost.Close();
}
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.ToString());
}
}
}
The previous commenter was on the right track, you need to declare a mex endpoint. mex is what provides the MetaData.
Try the following config:
<system.serviceModel>
<services>
<service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="SimpleWcfServiceBehavior">
<endpoint address="HelloWorldService"
binding="wsHttpBinding"
contract="App.Services.Contracts.IHelloWorldService">
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8087/MyService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SimpleWcfServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
</system.serviceModel>
I have update code and is working now,
<system.serviceModel>
<services>
<service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="DefaultServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8087/MyService"/>
</baseAddresses>
</host>
<endpoint address="HelloWorldService" binding="wsHttpBinding" contract="App.Services.Contracts.IHelloWorldService"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultServiceBehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>

WCF Discovery to search server with net.tcp://0.0.0.0:8888 address

I am trying to find my servers with the help of WCF Discovery protocol.
Everything is nice when I specify explicit address for server, for example: net.tcp://192.168.36.18:8888.
But when I specify net.tcp://0.0.0.0:8888 address on the server to listen all interfaces I receive net.tcp://0.0.0.0:8888 on the client. Certainly, I can't use this address to connect to the server.
Physically responce for wcf discovery request is coming from real servers addresses: 192.168.36.100, 192.168.36.239 and 192.168.36.61. I can see it by Wireshark. But I can't get this adresses from FindResponce class.
Is there a way to get physical address if server was configured to listen all interfaces by WCF discovery?
Code on the client:
var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var findResponce = discoveryClient.Find(new FindCriteria(typeof(IRadioService)));
discoveryClient.Close();
Code on the server:
var serviceHost = new ServiceHost(new RadioServer());
serviceHost.Open();
Config on the server:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="RadioServer">
<serviceDiscovery />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="RadioServer" name="Server.RadioServer">
<endpoint address="RS" binding="netTcpBinding" contract="Common.IRadioService" />
<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://0.0.0.0:8888/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>

Add Discovery to my WCF service

I have WCF service how run on 1 machine and simple comsole application client who run on another machine.
the server do something very simple: one method that return the value of 2 number that the client sent:
[ServiceContract]
public interface IMySampleWCFService
{
[OperationContract]
int Add(int num1, int num2);
[OperationContract]
void CreateDirectory(string directory);
[OperationContract]
string GetVendorToRun(string path);
}
public class MySampleWCFService : IMySampleWCFService
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false 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="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
What i want to do and find it hard to implement because i am a new developer is add Discovery to my WCF service, suppose i have several services that installed on several machines, what the client application is open i want to now which services is alive\running and all this data that i can received from Discovery.
I try to read several articles but as i mention didn't understand how to do it and I would love some help.
Using WCF Discovery is a bit convoluted and few people actually use it in my experience but it does work. This MSDN article has all the detail needed for adding Discovery to both the service & client configuration files.
The premise behind WCF Discovery is that you a expose a new discovery endpoint in similar way to the default MEX endpoint. The MEX endpoint allows the service to provide WSDL to clients. A WCF Discovery endpoint exposes a configured service to clients by means of UDP based responses to client UDP based requests. The overview link above provides much more detail.
Here is how your service configuration would look like:
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService"
behaviorConfiguration="endpointDiscoveryBehavior">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<!-- Discovery Endpoint: -->
<endpoint kind="udpDiscoveryEndpoint" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
<!-- Announcement Listener Configuration -->
<service name="AnnouncementListener">
<endpoint kind="udpAnnouncementEndpoint" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false 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="True" />
<serviceDiscovery>
<announcementEndpoints>
<endpoint kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="endpointDiscoveryBehavior">
<endpointDiscovery enabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
I think the most easy way to do this is to try to connect your client to addresses that you calculate at the runtime. For example:
static void Main(string[] args)
{
var addresses = new List<string>
{
#"http://192.168.1.1:8730/MySampleWCFService/",
#"http://localhost:8731/MySampleWCFService/",
#"http://localhost:8732/MySampleWCFService/",
#"http://localhost:8733/MySampleWCFService/",
};
foreach (var address in addresses)
{
var client = new MySampleWCFServiceClient(new BasicHttpBinding(), new EndpointAddress(address));
try
{
client.Open();
client.Add(0, 1);
Console.WriteLine("Connected to {0}", address);
}
catch (EndpointNotFoundException)
{
Console.WriteLine("Service at {0} is unreachable", address);
}
}
Console.ReadLine();
}
In my case I create a list with addresses but in your case you may build addresses with some predefined rules. For example you know that services use http binding with some name and port. Also you know that your cluster is in 192.0.16.xxx LAN so you may use formula:
address = "http://" + NextLanAddress() + ":" + port + "/" + serviceName + "/";

"Unable to Download Metadata" when using Two Bindings

In my book, it wants me to expose two endpoints using two bindings: WsHttpBinding & NetTCPBinding and host the service in a host application.
I use the following code in C# to try to connect to my service:
Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");
Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
On the service side I have:
[ServiceContract]
public interface IService...
public class SService : IService....
Then in my Config file I have:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WsHttpBindingConfig"
contract="SService.IService" />
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="NetTCPBindingConfig"
contract="SService.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5640/SService.svc" />
<add baseAddress="net.tcp://localhost:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
But when I try to add service reference to my host application, it says unable to download metadata from the address. I don't understand what is wrong and the teacher never taught this.. I decided to go ahead and look it up and learn ahead of time. I used the Editor to create the WebConfig shown above.
Can anyone point me in the right direction? I added Metadata behaviour through the editor and I set the HttpGetEnabled to true.
I can find a few issues with your code that can cause this issue:
Host = new ServiceHost(typeof(SServiceClient), BaseAddress). Pass here typeof(SService.SService) instead of typeof(SServiceClient). Change like this:
Host = new ServiceHost(typeof(SService.SService))
<service behaviorConfiguration="WsHttpBehaviour". I guess this should be "Behavior" as you have defined that. Since you have metadata enabled in config, you may remove the lines of code which add a ServiceMetadataBehavior to your servicehost.
Here is a sample that you can use for reference:
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Service">
<endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
<endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://<machinename>:5640/SService.svc" />
<add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service : IService
{
public string DoWork()
{
return "Hello world";
}
}
}
Metadata will be automatically available at the http baseaddress defined in the config.

provided URI scheme'http' is invalid; expected 'https'

I have a RESTful Web Service hosted in IIS 6.0, I am able to Browse the Service in browser. When i am trying to access the same service via Client console App, it is giving me the following error:
"provided URI scheme'http' is invalid; expected 'https', Parameter name: Via"
My WebService web.config has this settings:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="TestAPI">
<endpoint address="" behaviorConfiguration="RESTFriendly" binding="webHttpBinding" contract="ITestAPI" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
My Client App has App.config from where i am getting the address :
<appSettings>
<add key="WEBSERVICE" value="URL"/>
in the Main method :
WebChannelFactory<ITestAPI> cf = new WebChannelFactory<IAPI>(baseAddress);
WebHttpBinding wb =cf.Endpoint.Binding as WebHttpBinding;
wb.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
wb.Security.Mode = WebHttpSecurityMode.Transport;
cf.Credentials.UserName.UserName = "usermane";
cf.Credentials.UserName.Password = "password";
ITestAPI channel = cf.CreateChannel();
string msg = channel.TestMethod();
When it tries to call TestMethod, it gives me this error.
You're setting the security to transport mode, which is HTTPS, with this line:
wb.Security.Mode = WebHttpSecurityMode.Transport;
Is the value of baseAddress an HTTP or HTTPS address?

Categories

Resources