I'm new to WCF. Yesterday I put together my first Self-hosted WCF service. Everything was going well, however I am no longer getting any HTTP response from my dev machine. I can't even get to WSDL... I was hoping somebody could take a look over my configuration please?
host.Open(); actually works, so it seems to be running okay; it's just I can't seem to browse to any of the endpoints in a web browser on the hosting machine without a 404.
Program.cs
static void Main(string[] args)
{
try
{
using (ServiceHost host = new ServiceHost(typeof(WebImageRenderer.Renderer)))
{
host.Open();
Console.WriteLine(host.BaseAddresses[0].AbsoluteUri);
}
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
while (true) Console.ReadLine();
}
App.Config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="WebImageRenderer.Renderer" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="WebImageRenderer.IRenderer"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Thank you all
I think your ServiceHost is closed as soon as you exit the "using" block. Try putting the ReadLine loop inside.
Related
I have created a IDummyService contract implemented by DummyService. I am trying to self-host this service using the following configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="DummyService.DummyService" behaviorConfiguration="MEX">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/" />
</baseAddresses>
</host>
<endpoint address="DummyService"
binding="basicHttpBinding"
contract="DummyService.IDummyService"/>
<endpoint address="net.tcp://localhost:8734/DummyService/"
binding="netTcpBinding"
bindingConfiguration = "TransactionalTCP"
contract="DummyService.IDummyService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name = "TransactionalTCP" transactionFlow = "true"/>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name = "MEX">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The Service Host code that runs fine and starts these endpoints. However, when I try to access the following endpoints I get error as Page isn't working in Chrome browser.
http://localhost:8733/DummyService/
http://localhost:8733/mex/
However, I can access the page http://localhost:8733/
I don't understand why is that.
I thought the endpoint address is address entry for that endpoint appended to host's baseaddress
Any ideas what I am doing wrong ?
Thanks.
You have applied the attribute behaviorConfiguration="MEX" but didnt make use of it,you need to enable the metadata discovery which is false by default in WCF for security reasons.
<serviceBehaviors>
<behavior name = "MEX">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
Now when you browse the http://localhost:8733/DummyService/ and http://localhost:8733/mex/ ,a blank page will be displayed in the browser,instead of default error page.
And also if you want to test the net.tcp endpoint you can use WCFtestclient.exe.
I started working with WCF recently, and I'm having a problem that I just don't have a clue how to solve.
I start a WCF Service using Service Host, but when I use the URI in a browser it doesn't show the contract of the service, and it gives an exception when I try to connect to it using a ChannelFactory.
I created the project in Visual Studio 2017, and didn't do anything to the config file, excpet changing the base address. Both the service interface and implementation are in the root project "folder", and I've tried disabling the firewall and even my antivirus, but nothing seems to work.
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TaskExecutor.Exec">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Service interface:
namespace TaskExecutor
{
[ServiceContract]
public interface IExec
{
[OperationContract]
void DoWork();
}
}
Service implementation:
namespace TaskExecutor
{
public class Exec : IExec
{
public void DoWork()
{
Console.WriteLine("Doing work.");
}
}
}
Program launching the service:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
}
Console.WriteLine("Press any key to end...");
Console.ReadLine();
After launching the program display the message:
exec service started at http://localhost:8001/TaskExecutor/Exec/
Press any key to end...
The service client code is the following:
EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint);
IExec proxy = channelFactory.CreateChannel();
proxy.DoWork();
And it gives the exception:
System.ServiceModel.EndpointNotFoundException occurred
HResult=0x80131501
Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ 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 1:
WebException: Unable to connect to the remote server
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it
I seriously don't know what to do, and any help would be amazing.
Thank you very much!
You have exposed metadata but didnt bind it to the service.Here's how you have to.
<behaviors>
<serviceBehaviors>
<behavior name="metadadiscovery">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
Now bind the behavior to the service.
<services>
<service name="TaskExecutor.Exec" behaviorConfiguration="metadadiscovery">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
Now when you type the address in the browser you should be able to see wsdl.
http://localhost:8001/TaskExecutor/Exec/
So I figured out what was wrong, It was the code starting the service. instead of what I have It should be:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
Console.WriteLine("Press any key to end...");
Console.ReadLine();
}
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".
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.
I'm getting a:
http 400 bad request
error only if I use a relative address on my endpoint.
If I remove "basic" from the address I can view the WSDL definition without any problem:
http://localhost:8001/Test/
But as soon as I add "basic" and open up a browser and type in:
http://localhost:8001/Test/basic
i get the error.
Here's my config:
<system.serviceModel>
<services>
<service name="HostConsole.ContactService"
behaviorConfiguration="ServiceBehavior" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/Test/"/>
</baseAddresses>
</host>
<endpoint address="basic"
binding="basicHttpBinding"
contract="HostConsole.IContactService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
ServiceHost:
ServiceHost host = new ServiceHost(typeof(ContactService));
try {
host.Open();
foreach (ServiceEndpoint se in host.Description.Endpoints) {
Console.WriteLine(string.Format(
"Address: {0}",
se.Address.Uri.AbsoluteUri));
}
Console.WriteLine("Enter to close");
Console.ReadLine();
host.Close();
}
catch (Exception ex) {
host.Abort();
Console.WriteLine(ex.Message);
Console.ReadLine();
}
The address field you have with "basic" in it comes after the full service URL. I don't see a separate service activation in here or the name of your .svc file, but it would be something like
http://localhost:8001/Test/YourService.svc/basic .