WCF self-hosted service is not accessible remotely in local network - c#

I am currently creating the WCF self-hosted service (hosted in ConsoleApplication). Service contract is quite simple, it contains only two methods. When I host service locally on the local machine, everything works great. When I try to host it to have an access from other machines in local network things get complicated. I have an access via the browser on the all machines - no problem right here. But when I only try to create the client application and invoke some method I get the following exception:
Additional information: There was no endpoint listening at >http://localhost:9001/ValueDataService that could accept the message. This is >often caused by an incorrect address or SOAP action. See InnerException, if >present, for more details.
Inner exception:
The remote server returned an error: (404) Not Found.
I feel confused because I can access it remotely via browser typing http://localhost:9001/ValueDataService address. Client application always throws an above exception.
Just to get things simplified:
To get WCF service visible on the other machine in local network via browser (instead of single localhost machine), the only thing I have to change is add the
hostNameComparisonMode="Exact"
attribute to the endpoint binding.
Update:
and of course change localhost to IP address of host machine.
Service contract looks like:
[ServiceContract]
public interface IValuesDataService
{
[OperationContract]
int[] GetValues();
[OperationContract]
void SetValues(int[] values);
}
Console Application Program looks like:
static void Main(string[] args)
{
ServiceHost svcHost = null;
try
{
svcHost = new ServiceHost(typeof(ValueDataService.ValueDataService));
svcHost.Open();
Console.WriteLine("Value Data Service has been hosted.");
}
catch (Exception e)
{
svcHost = null;
Console.WriteLine("Service can not be started \n\nError Message [" + e.Message + "]");
}
if (svcHost != null)
{
Console.WriteLine("\nPress any key to close the Service");
Console.ReadLine();
svcHost.Close();
svcHost = null;
}
}
Host application App.config file:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="ValueDataService.ValueDataService" behaviorConfiguration="mathServiceBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/ValueDataService"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="ValueDataService.IValueDataService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mathServiceBehave">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="bindingName" hostNameComparisonMode="Exact">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
and at least Client Application App.config file:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBinding">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9001/ValueDataService" binding="basicHttpBinding" contract="ValueDataServiceReference.IValueDataService"/>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webEndpoint">
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Can anyone help me to get this work?

UPDATE:
So IIS has a annoying binding system that does not convert IP address requests to localhost bindings. Its and unfortunate thing but thats how its request system works. When you run the WCF service in debug it runs an instance of IIS express with a localhost binding. Hence when the request comes through, it doesn't get translated to the binding address the instance is running on.
How to fix this:
1) Start IIS
2) Host your Developed Application in IIS with the suitable IP address binding
OLD:
In the link : http://localhost:9001/ValueDataService replace localhost with server's IP address so that the link would look like : http://192.168.1.5:9001/ValueDataService goto network and sharing center/use ipconfig to get server machine's IP address.
The reason the localhost link doesn't work is because when the browser reads localhost it resolves it looks at the computers own network interface rather than look outwards.
I hope it helps.

Related

WCF Serice - Getting 400 Bad Request but can use IE to browse to it

I have written a WCF Service which is being called by a Winforms application.
I have tested calling the WCF Service from the WinForms application locally and it all works fine
I have moved both the WinForms application and the WCF Service to a Remote Server, on the Remote Server I can use IE to browse to the Service but when I try to use the Winforms application I get a 400 Bad Response error.
My local machine and the Remote Server has been configured exactly the same, Windows Firewall, User accounts etc and the codebase/config files of both the Service and Winforms app are the same.
The config file for the WCF Service is as follows (i've had to remove the base address)
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="CodeLocksAPIServiceBehavior" name="CodeLocksAPI.WCF.CodeLocksAPIService">
<host>
<baseAddresses>
</baseAddresses>
</host>
<endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="CodeLocksAPI.WCF.ICodeLocksAPIService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CodeLocksAPIServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
In the Winforms app config I have tried adding the DefaultProxy tabs which do not help
WCF Service is called from Code Using the following
string serviceUserName = ConfigurationManager.AppSettings["ServiceUserName"];
string servicePassword = ConfigurationManager.AppSettings["ServicePassword"];
HttpClientHandler handler = new HttpClientHandler
{
Credentials = new
System.Net.NetworkCredential(serviceUserName, servicePassword)
};
this.Client = new HttpClient(handler);
this.Client.BaseAddress = new Uri(this.GetBaseAddressFromConfig());
processInitializeLockRequestArgs.AssetRef = assetRef;
string contentMessage = Newtonsoft.Json.JsonConvert.SerializeObject(processInitializeLockRequestArgs);
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(contentMessage);
ByteArrayContent content = new ByteArrayContent(contentBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
httpResponse = this.Client.PostAsync(methodUri, content).Result;
As already stated - this code is working locally but not on the remote server - the WCF Service can be browsed to locally and on the remote service
I've been informed that the issue is most likely down to Proxy settings on the WinForms application and that the issue is not down to a Firewall or anything like that
You could try adding
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
....
to the beginning of your WinForms config file.

Could not find a base address that matches scheme net.tcp for the endpoint with binding MetadataExchangeTcpBinding self hosting

I have created a class library which have Datacontract and service contract and its implementation. Then I created a console application to host the wcf service. Where i have defined WCF setting in app.config file which is below.
<configuration>
<system.serviceModel>
<services>
<service name="TestService.TestService" behaviorConfiguration="tcpbindingConfiguration">
<host>
<baseAddresses>
<add baseAddress="net:tcp://FullComputerName:9002/TestService"/>
</baseAddresses>
</host>
<endpoint address="net:tcp://FullComputerName:9002/TestService" binding="netTcpBinding" contract="TestService.ITestServiceContract"></endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="tcpbindingConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Then I program.cs File.
ServiceHost sh = null;
try
{
sh = new ServiceHost(typeof(TestService.TestService));
sh.Open();
Console.WriteLine("Service started at net:tcp//FullComputerName:9002/TestService");
}
catch (Exception ex)
{
sh = null;
Console.WriteLine("Service cann't started");
throw;
}
I have activate the WCF for Non HTTP from windows features on and off.
I want to host this wcf service in console application, Some article suggest that i need to set nettcp binding in IIS. Why i do that?
IIS is an alternative hosting for your WCF service, that is you either host it in your console application or in IIS. As far as I understand, there should be no need to setup IIS, if you use a console application.
Try changing "net:tcp" in your configuration to "net.tcp".

"No Endpoint listening" only when consuming WCF via windows service

I have deployed WCF web service in IIS (remote-host: godaddy) and trying to consume through windows service. But getting below error when I try to consume via Windows Service.
"Exception: There was no endpoint listening at http://mydomainname.com/vfolder1/vfolder2/HelloService.svc/HelloService" that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
But when i consume the same service via Windows Form, it works without any issue.
Below is my WCF web service configuration (web.config)
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://mydomainname.com/vfolder1/vfolder2/"/>
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
Below is my WCF client configuration (app.config)
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IHelloService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://mydomainname.com/vfolder1/vfolder2/HelloService.svc/HelloService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService"
contract="HelloService.IHelloService" name="BasicHttpBinding_IHelloService" />
</client>
</system.serviceModel>
Below is the code in Windows forms
private void button1_Click(object sender, EventArgs e)
{
HelloService.HelloServiceClient client = new HelloService.HelloServiceClient();
label1.Text = client.GetMessage(textBox1.Text);
}
Below is the code in Windows Service
public static void write_data()
{
try
{
HelloService.HelloServiceClient clNt = new HelloService.HelloServiceClient();
for (int i = 0; i < 10; i++)
{
Write_Log(clNt.GetMessage(i.ToString()));
}
clNt.Close();
}
catch (Exception ex)
{
Write_Log("Exception: " + ex.Message.ToString());
}
}
When we initially developed & hosted in local network (IIS), we never had any issues. This is our first project in WCF and involving remote host. Tried to look all similar posts but all were talking on complete non-working. Nothing similar to our scenario exists.
Appreciate the great help.
The issue is resolved. Our company policy wasn't allowing the service to communicate to internet when running under "Local System" privilege. I temporarily changed to run with my user credentials and it went through.

Hosting NamedPipe WCF on Win7 IIS 7.5 EndpointNotFoundException

I am trying to create WCF NamedPipe in win7 with IIS 7.5.
In IIS Manager right click, and create WebSite named "TestWCFWithNamedPipe"
Right click my WebSite "TestWCFWithNamedPipe" -> select Edit bindings
Type:http
Host Name:sample.localdev.net
Port:80
Address:*
Type:net.pipe
Binding informations:*
In Advanced settings set the value for Enabled Protocol as "http,net.pipe"
In My WebSite "TestWCFWithNamedPipe" Web Application Project
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceDebug />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DefaultBehavior" name="SampleWcfLib.SampleWcfObj">
<endpoint address="net.pipe://localhost/Sample" binding="netNamedPipeBinding"
bindingConfiguration="" contract="SampleWcfInterfaceLib.ISampleWcfObj" />
</service>
</services>
</system.serviceModel>
TestClient.exe code is following
string address = "net.pipe://localhost/Sample";
NetNamedPipeBinding binding = new NetNamedPipeBinding();
EndpointAddress ep = new EndpointAddress(address);
ISampleWcfObj channel = ChannelFactory<ISampleWcfObj>.CreateChannel(binding, ep);
string result = channel.Ping("Test");
And I run TestClient.exe, and I get an exception:
There was no endpoint listening at net.pipe://localhost/Sample that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
I have checked Win7 Service status:
Net.Pipe Listener Adapter Started
Net.Tcp Listener Adapter Started
I have done all settings for WCF.
Why do I still get the EndpointNotFoundException error message ?
You can find the name of your tcp binding if you have a look in your XSD file.
It will be on the bottom of your file and it will look like this
<wsdl:service name="PersonService">
<wsdl:port name="NetNamedPipeBinding_IPersonContract" binding="tns:NetNamedPipeBinding_IPersonContract">
<soap12:address location="net.pipe://wcftestpipe/WcfTest/PersonService.svc/test"/>
<wsa10:EndpointReference>
<wsa10:Address>net.pipe://wcftestpipe/WcfTest/PersonService.svc/test</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
Afther that you propably get a security exeception that will say you don't have the right credentials. You can set your binding security mode to NONE in your web.config of your WCF service and app.config of your Client.
<bindings>
<netNamedPipeBinding>
<binding>
<security mode="None"/>
</binding>
</netNamedPipeBinding>
</bindings>
Hope this helps

Need to create client using svcutil but get error message Metadata contains a reference that cannot be resolved

I am attempting to create WCF client files using the svcutil.exe. I run my wpf app that is hosting my wcf service library. I get the following error when running svcutil from visual studio command prompt 2010:
WS-Metadata Exchange Error
URI: net.tcp://localhost:50100/duplex
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:50100/duplex'.
Can someone explain what is wrong with my configuration file, and how I can successfully generate my client files using svcutil.exe?
Here is my svcutil command (I run this command after starting my service of course):
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>svcutil.exe net.tcp://localhost:50100/duplex /d:c:\temp
Here is my app.config for wpf application hosting the wcf service library:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="baseAddress" value="net.tcp://localhost:50100/duplex"/>
</appSettings>
<system.serviceModel>
<!--<bindings />-->
<client />
<services>
<service name="TrackRejectCommunication.RejectService"
behaviorConfiguration="RejectServiceBehavior">
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="DuplexNetTcpBinding_IRejectService"
name="netTcp"
contract="TrackRejectCommunication.IRejectService" />
<endpoint name="NetTcpMetadataPoint"
address="mexTcp"
binding="mexTcpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="DuplexNetTcpBinding_IRejectService"
sendTimeout="00:00:10"
portSharingEnabled="false">
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="RejectServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:50103/Metadata/Duplex" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
I get the base address via the configuration manager in my wpf application. The I use the following code to start the service in my wpf application:
RejectService rejectService = new RejectService();
//start the reject service.
HostStartup.StartService(rejectService);
The StartService code:
Uri baseAddress = new Uri(ConfigurationManager.AppSettings["baseAddress"]);
//Instantiate new ServiceHost with the singleton instance being passed in
myServiceHost = new ServiceHost(instance, baseAddress);
//Open myServiceHost
myServiceHost.Open();
I encountered a similar problem when using svcutil to create wsdl/proxy for a singleton service started via "myServiceHost = new ServiceHost(instance, baseAddress);". I was able to work around the problem by temporarily commenting out the code that created/opened the ServiceHost instance, then running svcutil to create the wsdl, then restoring the code to it's original format. It appears that whatever svcutil is doing is incompatible with the in-code instantiated ServiceHost.
For reference, the particular error that I encountered was:
Warning: There was an error loading the service type to be exported: ...
Details:The type initializer for ... threw an exception.

Categories