WCF Service is not hosted on the machine - c#

I have implemented a simple chat console app and it worked well. When i tried to apply the same concept on GUI app. the service side when hosting , there is any error but if i use CMD command netstat -ao to show all ports , it is not exists.So when i run client app , there is an Exception (No connection could be made because the target machine actively refused). How can i solve these problem ?
Server
ServiceHost host;
using (host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111")))
{
host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "IService");
try
{
host.Open();
}
catch
{
}
}
Client
public bool Connect()
{
DuplexChannelFactory<IService> pipeFactory = new DuplexChannelFactory<IService>(new InstanceContext(this),
new NetTcpBinding(),
new EndpointAddress(AppConfiguration.GetValue(net.tcp://localhost:4111/IService"));
try
{
pipeProxy = pipeFactory.CreateChannel();
if (pipeProxy.Register())
{
return true;
}
}
catch
{
}
return false;
}

Assuming you are showing all your code.
You need to add a line after host.Open();, you could add Console.ReadLine();
This would get the program to stop from existing. What happens now is that the host opens, then the program exists, and the host gets closed/garbage collected.

I have solve it.
In GUI remove the (using) at defined new ServiceHost. Why i don't know but it works!!!
ServiceHost host;
host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111"));

Related

Create a self hosted WCF Service inside a Windows Form

If I use this code for self Host a WCF service in a Console application it works. I run the host app and then from another app (which I call the client app,) I can add the service reference from visual studio > solution explorer > ADD SERVICE REFERENCE > http://10.131.131.14:8080/sendKioskMessage > click GO, add the service with no problems and consume it from the client app (which is a windows form)
But if I run the same code in a Windows Form, I run first the (SELF HOST WCF) windows form app, then from the other app (client app) in visual studio I try to add the service reference from ADD SERVICE REFERENCE in solution explorer (Just the same way that it works before but with the Console App self host) but it throws the following error:
*
An error (Details) occurred while attempting to find services at
http://10.131.131.14:8080/sendKioskMessage.
(If I click Details Link, says the following:)
There was an error downloading
'http://10.131.131.14:8080/sendKioskMessage/$metadata'. Unable to
connect to the remote server. Metadata contains a reference that
cannot be resolved: 'http://10.131.131.14:8080/sendKioskMessage'.
There was no endpoint listening at
http://10.131.131.14:8080/sendKioskMessage that could accept the
message. This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details. Unable to connect to
the remote server. If the service is defined in the current solution,
try building the solution and adding the service reference again.
*
The IP that I use is the IP of my pc where both apps are running. I also used localhost instead of my actual IP with the same result.
Windows Form Code (can't add the service from another app):
public partial class KioskosServerForm : Form
{
[ServiceContract]
public interface IKioskMessageService
{
[OperationContract]
string SendKioskMessage(string message);
}
public class KioskMessageService : IKioskMessageService
{
public string SendKioskMessage(string message)
{
return string.Format("Message sent: {0}", message);
}
}
public KioskosServerForm()
{
InitializeComponent();
}
private void KioskosServerForm_Load(object sender, EventArgs e)
{
Uri baseAddress = new Uri("http://10.131.131.14:8080/sendKioskMessage");
try
{
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(KioskMessageService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.InnerException.Message);
}
}
}
Console App Code (Works! I can add the service from other client app):
[ServiceContract]
public interface IKioskMessageService
{
[OperationContract]
string SendKioskMessage(string message);
}
public class KioskMessageService : IKioskMessageService
{
public string SendKioskMessage(string message)
{
return string.Format("Message sent: {0}", message);
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/sendKioskMessage");
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(KioskMessageService),baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
}
I don't know why I can consume the service if the service is self hosted in a console app, but I can't add it if the service is self hosted in a Windows Form.
I will appreciate a lot your help to achieve this from a Windows From, since I need to self host the WCF service from a windows form, no a console app.
I'm using Visual Studio 2017, .Net Framework 4.6.1
THANKS IN ADVANCE GUYS!!
TL;DR the console app works because you have a delay before shutting down the service; the WinForms host doesn't
The reason your console WCF host service works is that you start the hosting and continue until the Console.ReadLine() line:
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine(); // <-------- program waits here
// Close the ServiceHost.
host.Close();
...after which the service is torn down. Prior to that, your other clients can connect fine and add Service References.
The WinForms app has no such delay:
private void KioskosServerForm_Load(object sender, EventArgs e)
{
Uri baseAddress = new Uri("http://10.131.131.14:8080/sendKioskMessage");
try
{
// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(KioskMessageService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open(); // <------ opened here
} // <------ shutdown here
}
catch (Exception exp)
{
MessageBox.Show(exp.InnerException.Message);
}
}
...it is immediately shutdown when the code goes out of scope of the using block. The using will automatically call Dispose() on the host object which in turn calls Close().
Consider placing the host into a variable like so:
ServiceHost _host; // <---------- new!
private void KioskosServerForm_Load(object sender, EventArgs e)
{
Uri baseAddress = new Uri("http://10.131.131.14:8080/sendKioskMessage");
try
{
// Create the ServiceHost.
_host = new ServiceHost(typeof(KioskMessageService), baseAddress))
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
_host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
_host.Open();
}
catch (Exception exp)
{
MessageBox.Show(exp.InnerException.Message);
}
}
Later, you can close the _host instance with a call to Close.

Get Address Already In Use exception. self hosted wcf service

I found numerous questions about this issue : here, here, here, and here, But could not solve my problem.
Background:
We have an existing unmanaged application that launches a managed winforms application.
There is a shared managed assembly which exposes comvisible objects for the unmanaged app and that assembly launches the managed app.
So the ServiceHost runs on the winforms app and the client on that shared assembly. The setup is similar to this implementation .
The application runs on a approx. 100 pcs, win xp or win 7.
The problem:
once in a few days we get :
System.ServiceModel.AddressAlreadyInUseException when trying to launch the winforms application.
we ran netstat but could not find anything listening on that port.
The only 'solution' is rebooting the pc.
I could not reproduce this problem on my dev machine. but it happens on production environment once in a while.
Code:
Code is being typed hopefully without typos, can not copy paste :
Service config (found in winform appconfig):
<system.ServiceModel>
.
.
<services>
<service name="MyService">
<endpoint address="net.tcp://localhost:4444" binding="netTcpBinding"
contract="IMyService"/>
</service>
</services>
.
.
<\system.ServiceModel>
Service
StartService() is called from mainform_Load
private static void StartService()
{
try
{
_serviceHost = new ServiceHost(typeof(MyService));
_serviceHost.Open();
}
catch(Exception ex)
{
LogError(ex);
ExitApp();
}
}
private static void ExitApp()
{
try
{
_serviceHost.Close();
}
catch(CommunicationException cex)
{
LogError(cex);
_serviceHost.Abort();
}
finally
{
Application.Exit();
}
}
Client
private static void CallMyService()
{
var channelFactory = new ChannelFactory<IMyService>("net.tcp://localhost:4444");
IMyService myChannel= channelFactory.CreateChannel();
bool error = true;
try
{
myChannel.PerformOperation();
((IClientChannel)myChannel).Close();
error = false;
}
finally
{
if (error)
{
((IClientChannel)myChannel).Abort();
}
}
}
I hope i am clear enough, thanks.
We solved this issue back in 2015, i am sharing the answer now:
Since the communication between the processes is on the same machine, we used NetNamedPipeBinding instead of NetTcpBinding and we got no more System.ServiceModel.AddressAlreadyInUseException exceptions.
The setup is like the following (actual code is a bit different):
Service :
private static void StartService()
{
try
{
string address = "net.pipe://localhost/MyApp/NamedPipeBindingHost";
_serviceHost = new ServiceHost(typeof(MyService));
NetNamedPipeBinding b = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
_serviceHost.Open();
b.OpenTimeout = TimeSpan.FromMinutes(2);
b.closeTimeout = TimeSpan.FromMinutes(1);
b.ReceiveTimeout = TimeSpan.FromMinutes(10);
_serviceHost.AddServiceEndpoint(typeOf(IMyService), b, address);
_serviceHost.Open();
}
catch(Exception ex)
{
LogError(ex);
ExitApp();
}
}
private static void ExitApp()
{
try
{
_serviceHost.Close();
}
catch(CommunicationException cex)
{
LogError(cex);
_serviceHost.Abort();
}
finally
{
Application.Exit();
}
}
Client :
private static void CallMyService()
{
string address = "net.pipe://localhost/MyApp/NamedPipeBindingHost";
EndpointAddress ep = new EndpointAddress(adress);
var channelFactory = new ChannelFactory<IMyService>(GetNamedPipeBindings(), ep);
IMyService myChannel= channelFactory.CreateChannel();
bool error = true;
try
{
myChannel.PerformOperation();
((IClientChannel)myChannel).Close();
error = false;
}
finally
{
if (error)
{
((IClientChannel)myChannel).Abort();
}
}
}
private NamedPipeBinding GetNamedPipeBindings()
{
NamedPipeBinding binding = new NamedPipeBinding (NetNamedPipeSecurityMode.None);
binding.OpenTimeout = TimeSpan.FromMinutes(2);
b.closeTimeout = TimeSpan.FromMinutes(1);
b.SendTimeout = TimeSpan.FromMinutes(10);
return binding;
}
Something I noticed that helped in my machine: somehow IIS Express generated multiple addresses with that same port when I was configuring the project, and tried to launch all of them during testing causing this failure.
The error would also appear via a popup from the IIS Express taskbar icon. I navigated to its config file ("User Documents\IISExpress\config\applicationhost.config"), removed all 'site' nodes that contained virtual directory bindings to those addresses, saved and re-tried again and it worked.
Hope this works for you?

EndpointNotFoundException in WCF + NamedPipes hosted in Windows Forms

I've created a simple code to allow cross-appdomain communication using WCF and NamedPipes. I'm testing the code on my Windows 8.1 and it is causing a EndpointNotFoundException.
Here is my code:
Service Contract
[ServiceContract(Namespace = "http://PoC.AppDomainWCF")]
public interface ICrossAppDomainSvc
{
[OperationContract]
bool HasPermission(String User, String Permission);
}
Program.cs (WinForms)
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread thread = new Thread(new ThreadStart(StartService));
thread.Start();
Application.Run(new Form1());
}
static void StartService()
{
using (ServiceHost host = new ServiceHost(typeof(CrossAppDomainSvc), new Uri[] {
new Uri("http://localhost:12000/AppDomainWCF/"),
new Uri("net.pipe://localhost/")
}))
{
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
host.AddServiceEndpoint(typeof(ICrossAppDomainSvc), binding, "CrossAppDomainSvc");
// Add a MEX endpoint
//ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
//metadataBehavior.HttpGetEnabled = true;
//metadataBehavior.HttpGetUrl = new Uri("http://localhost:12001/AppDomainWCF");
//host.Description.Behaviors.Add(metadataBehavior);
host.Open();
}
}
}
Client code
NetNamedPipeBinding binding = new NetNamedPipeBinding();
ChannelFactory<ICrossAppDomainSvc> channelFactory = new ChannelFactory<ICrossAppDomainSvc>(binding);
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/CrossAppDomainSvc");
ICrossAppDomainSvc service = channelFactory.CreateChannel(endpointAddress);
MessageBox.Show(service.HasPermission("Juliano", "XPTO").ToString());
The exception is thrown at the service.HasPermission call.
What is wrong with my code?
UPDATE
As the question has been answered and my proof-of-concept is working, I've created a repository on GitHub to help anyone who needs to allow cross appdomain communication.
CrossAppDomainWCF sample code
You open your ServiceHost and immediately close it. serviceHost.Open() method doesn't block. So your endpoint doesn't exist because host is already closed when you are connecting

How to keep WCF Soap Service Open for the duration of the program running

I am working on an C# WCF project and I have got it pretty much working except for quite a big but hopefully simple problem.
The WCF service is hosted from within my Console application and my console application calls a function to a different class to open the connection for the WCF service.
However, if the last line of the function is host.open(); the function call then finishes to the connection gets closed and the service can no longer be used. However, if I put Console.ReadLine() after the host.open then the service stays open and I can use it but obviously the rest of the flow of the program no longer runs.
Below is the code I am using to open the host connection.
public void startSoapServer()
{
string methodInfo = classDetails + MethodInfo.GetCurrentMethod().Name;
if (String.IsNullOrEmpty(Configuration.soapServerSettings.soapServerUrl) ||
Configuration.soapServerSettings.soapPort == 0)
{
string message = "Not starting Soap Server: URL or Port number is not set in config file";
library.logging(methodInfo, message);
library.setAlarm(message, CommonTasks.AlarmStatus.Medium, methodInfo);
return;
}
//baseAddress = new Uri(string.Format("{0}:{1}", Configuration.soapServerSettings.soapServerUrl,
// Configuration.soapServerSettings.soapPort));
baseAddress = new Uri("http://localhost:6525/hello");
using (ServiceHost host = new ServiceHost(typeof(SoapServer), baseAddress))
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Opened += new EventHandler(host_Opened);
host.Faulted += new EventHandler(host_Faulted);
host.Open();
Console.ReadLine();
}
Without the Console.ReadLine() there the function finishes so the connection closes. How can I keep the host open for the duration that the C# is app is running.
This function call is called from within the Main method halfway through initiliasing some stuff within the console stuff.
Thanks for any help you can provide.
You need to declare ServiceHost at class scope instead of function scope and do not use using.
using {} will automatically Dispose the object to which it pertains and Disposal means closing. Also, since your ServiceHost is defined at function scope, it will go out of scope as soon as the function finishes and will be cleaned up by the garbage collector.
The reason that your ReadLine call is preventing the closing is because it is inside the using statement and stops the program inside the function where the variable is declared keeping it in scope.
You need to do something like this:
private ServiceHost host;
public void startSoapServer()
{
// trimmed... for clarity
host = new ServiceHost(typeof(SoapServer), baseAddress));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Opened += new EventHandler(host_Opened);
host.Faulted += new EventHandler(host_Faulted);
host.Open();
// etc.
You will close host when you exit the application.

Windows service starts on Windows 7 but fails to start on Windows Server 2008 R2

I have created a wcf service which is deployed via a managed windows service. what the onstart() does is it creates and opens a tcp host for the wcf serice.
everything works fine in windows 7 but when I try to install the service in windows server 2008 R2 the service starts and then stops with the error that sometimes services stop when there is nothing to do. It is run under the network service account.
I cant find anything usefull in the windows logs.
I have tryed installing a dubug build and call debugger.launch() but its not working. I cant use remode debug because the the process does not stay open long enough for me to attach to it.
I really dont know what to do. Here is my code:
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
try
{
//if (!System.Diagnostics.EventLog.SourceExists("i2s CU Service (eng)"))
//{
// System.Diagnostics.EventLog.CreateEventSource("i2s CU Service", "Application");
//}
System.Diagnostics.EventLog.CreateEventSource("i2s CU Service", "Application");
}
catch (Exception)
{
//throw;
}
eventLog1.Source = "i2s CU Service";
eventLog1.Log = "Application";
if (serviceHost != null)
{
serviceHost.Close();
}
System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();
Uri tcpUri = null;
try
{
tcpUri = new Uri((string)reader.GetValue("Uri", typeof(string))); //net.tcp://localhost:8008/i2sServer
serviceHost = new ServiceHost(typeof(ConcurrentUsers), tcpUri);
// Create a binding that uses TCP and set the security mode to none.
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.None;
// Add an endpoint to the service.
serviceHost.AddServiceEndpoint(typeof(IConcurrentUsers), b, "");
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message, EventLogEntryType.Error);
}
if (serviceHost.State == CommunicationState.Opened)
{
eventLog1.WriteEntry("Service started.", EventLogEntryType.Information);
}
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
eventLog1.WriteEntry("Service stopped.", EventLogEntryType.Information);
}
this code as is works perfectly fine in windows 7 but I cant get it to run on win 2008 R2 server.
Thanks in advance
Refactor your application as a console application, and then run it on the desired environment to be able to debug it easily.
I'm sure that the problem will reveal itself once you run your console replica under the same user account that is assigned to your windows service

Categories

Resources