How do I set IncludeExceptionDetailInFaults in code without using App.Config?
Yes, sure - on the server side, before you open the service host. This would however require that you self-host the WCF service - won't work in IIS hosting scenarios:
ServiceHost host = new ServiceHost(typeof(MyWCFService));
ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
// if not found - add behavior with setting turned on
if (debug == null)
{
host.Description.Behaviors.Add(
new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
}
else
{
// make sure setting is turned ON
if (!debug.IncludeExceptionDetailInFaults)
{
debug.IncludeExceptionDetailInFaults = true;
}
}
host.Open();
If you need to do the same thing in IIS hosting, you'll have to create your own custom MyServiceHost descendant and a suitable MyServiceHostFactory that would instantiate such a custom service host, and reference this custom service host factory in your *.svc file.
You can also set it in the [ServiceBehavior] tag above your class declaration that inherits the interface
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}
Related
I've been trying to create an application that can receive information from other running applications through WCF.
I've setup a void method in a separate class, created the interface, and hosted the service.
In my Host application I have the following method.
public Class ReceivingMethods : IReceivingMethods
{
Public void HelloWorld(string text)
{
MessageBox.Show(text);
}
}
and
[ServiceContract]
interface iReceivingMethods
{
[OperationContract]
void HelloWorld(string text);
}
In the client, I would like to do this:
HostService client = new HostService();
client.HelloWorld("Hello World");
client.close();
But it doesn't work and instead I have to do this.
HostService client = new HostService();
HelloWorld hi = new HelloWorld();
hi.text = "Hello World";
client.HelloWorld(hi);
client.close();
I've gotten it to work as the former previously in an Application/ASP combination, but not on this application and I cannot find any difference in the setup between the two applications.
Can anybody tell me what is required from the WCF setup to get it to work as the former?
HostService client = new HostService();
You haven't mention what endpoint or which class object to use.Typically the servicehost class must create the object of particular end point,something like below one.
using(System.ServiceModel.ServiceHost host =
new System.ServiceModel.ServiceHost(typeof(ReceivingMethodsnamespace.ReceivingMethods )))
{
host.Open();
Console.WriteLine("Host started # " + DateTime.Now.ToString());
Console.ReadLine();
}
Generally the hostservice must create an object of the class which is implementing servicecontract interface(servicename of AddressBindingContract file)
Turns out I found the issue somewhere else.
I configured the client service reference to "always generate message contracts"
Unchecking this and updating the service reference solved the issue.
I found the solution here.
https://social.msdn.microsoft.com/Forums/en-US/b9655eeb-cdbb-4703-87d8-00deac340173/add-service-reference-creates-message-contracts-requestresponse-objects-with-always-generate?forum=wcf
I have the following code to setup a WCF service in code:
namespace Serviceman
{
public class Hostman
{
public Uri VServicesTCPAddress = new Uri("net.tcp://localhost:8000/v-services");
public ServiceHost VServicesHost = new ServiceHost(typeof(MyDemoService), new Uri("net.tcp://localhost:8000/v-services"));
public void ConfigureTcpService()
{
NetTcpBinding tcpBinding = new NetTcpBinding();
ServiceMetadataBehavior sMBehavior = new ServiceMetadataBehavior();
VServicesHost.Description.Behaviors.Add(sMBehavior);
VServicesHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
VServicesHost.AddServiceEndpoint(typeof(IAccountsService), tcpBinding, VServicesTCPAddress);
}
}
}
I have started the service and it's working just fine, but when I connect multiple instances of my client, after some time I receive errors of having used all available channels. The question now is how can I increase the default value for connection pooling limit or even remove that?
Enable port sharing on your TCP binding like this,
tcpBinding.PortSharingEnabled = true;
Or,
Change maxConnections available on your TCP binding configuration to something of your choice.The default for Max connection is 10 out of the box.
I think I've literally checked for all possibilities but I still keep getting this error (written in eventvwr) when I attempt to start my service:
Service cannot be started. System.InvalidOperationException: Service
'NexolNotifierWinService.NexolNotifier' has zero application
(non-infrastructure) endpoints. This might be because no configuration
file was found for your application, or because no service element
matching the service name could be found in the configuration file, or
because no endpoints were defined in the service element.
Service installation goes smoothly using installutil.
I'm genuinely not sure why I'm having this error. It's just a simple Windows Service project, so there's no app.config to mess around with either.
Here's my code:
Program.cs
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new NexolNotifierService()
};
ServiceBase.Run(ServicesToRun);
}
}
NexolNotifierService.cs
public partial class NexolNotifierService : ServiceBase
{
private ServiceHost host;
public NexolNotifierService()
{
InitializeComponent();
this.ServiceName = "NexolNotifierService";
}
protected override void OnStart(string[] args)
{
Type serviceType = typeof(NexolNotifier);
host = new ServiceHost(serviceType);
host.Open();
}
protected override void OnStop()
{
if (host != null)
host.Close();
}
}
ProjectInstaller.Designer.cs (For installing service)
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "NexolNotifierService";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
and my actual service:
NexolNotifier.cs
public class NexolNotifier
{
public NexolNotifier()
{
....
}
Service was added from add new project->Windows Service in Visual Studio 2008.
I'm just trying to get a very simple windows service working. From what I can see, there is zero reason why this shouldn't work.
What do you want to do?
If you want just a plain Windows Service - no communication, nothing - then you don't need ServiceHost! You just need to derive from the ServiceBase class in the .NET framework and implement/override some of the methods - that's all. Read values from a database, do something with them, send e-mails - whatever - you will never need a ServiceHost for this!
If you use ServiceHost then you're using the WCF infrastructure, which means, you're writing a self-hosted web service.
So what do you want to do, really??
What's the task/job that your Windows Service is supposed to do?? ServiceHost has nothing to do with a plain Windows Service! ServiceHost == WCF - always. You don't need a ServiceHost for just a plain Windows service
For just plain Windows service (no WCF), see e.g.
Creating a Basic Windows Service
Simple Windows Service Sample
and many, many more samples. Both samples show just a plain Windows service, no WCF, no ServiceHost anywhere in sight.
Add service endpoint from code like this
Uri baseAddress = new Uri("http://localhost:8095/Service");
serviceHost = new ServiceHost( typeof(YourService), baseAddress );
serviceHost.AddServiceEndpoint( typeof(IYourService), new BasicHttpBinding(), baseAddress );
serviceHost.Open();
I have a Windows service that exposes a WCF service.
I also have an application that talks to the WCF service and sends commands and receives data back.
All this works fine when I run the application from Visual Studio.
However, when I install the application and run it, the application cannot communicate with the service.
Nor can the batch files that the application runs to Stop and Start the service.
I've tried using netsh to 'reserve' the URI but I don't really know what I'm doing :-)
Can you point me in the right direction?
Windows Server code WCF Service code (abridged):
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCF_Service" in both code and config file together.
[ServiceContract]
public class InternetGauge_IO
{
[OperationContract]
public void Pause()
{
RunningState.paused = true;
}
[OperationContract]
public void Continue()
{
RunningState.paused = false;
}
[OperationContract]
public bool GetRunningState()
{
return RunningState.paused;
}
....
Windows Server code WCF Create endpoint code:
private void InitializeConsoleComms()
{
try
{
//Prepare comms with the Console application
Type serviceType = typeof(InternetGauge_IO);
host = new ServiceHost(serviceType, new Uri[] { new Uri("http://localhost:8080/") });
// Add behavior for our MEX endpoint
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
// Create basicHttpBinding endpoint at http://localhost:8080/RJB.InternetGauge/
host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), "RJB.InternetGauge");
// Add MEX endpoint at http://localhost:8080/MEX/
host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");
host.Open();
logger.LogEvent("Console comms ready", "Internet Gauge", 4, 1);
}
catch (Exception e)
{
logger.LogEvent("Failed to open Console comms", "Internet Gauge", 1, 1);
logger.LogEvent("Exception : " + e.InnerException + " Stack Trace: " + e.StackTrace + " Message: " + e.Message, "RJB.InternetGauge.WindowsService.Main", 1, 1);
}
}
The application just uses the generated proxy and methods e.g.
private bool GetRunningState()
{
try
{
InternetGauge_IOClient client = new InternetGauge_IOClient();
isRunning = true;
return(client.GetRunningState());
}
catch (Exception)
{
trayIcon.Text = "Internet Gauge Windows Service does not appear to be running.";
trayIcon.Icon = RJB.InternetGauge.Console.Properties.Resources.ServiceStopped;
isPaused = true;
isRunning = false;
return isPaused;
}
}
The netsh command I tried
netsh http add urlacl url=http://+:8080/InternetGauge_IO user=PC-01\richard
Any ideas?
Thanks
Richard
It is because I am a twit.
Didn't copy the app.config over during the installation program.
All works fine now.
I need to make some connections on startup of a server. I'm using the wcf technology for this client-server application. The problem is that the constructor of the server isn't called at any time, so for the moment, i initialize the connections when the first client makes a connection. But this generates problems in a further part.
This is my server setup:
private static ServiceHost _svc;
static void Main(string[] args)
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
Uri address = new Uri("net.tcp://localhost:8000");
_svc = new ServiceHost(typeof(MonitoringSystemService), address);
publishMetaData(_svc, "http://localhost:8001");
_svc.AddServiceEndpoint(typeof(IMonitoringSystemService), binding, "Monitoring Server");
_svc.Open();
Console.WriteLine("Listener service gestart op net.tcp://localhost:8000/Monitoring");
Console.ReadLine();
}
private static void publishMetaData(ServiceHost svc, string sEndpointAddress)
{
ServiceMetadataBehavior smb = svc.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb != null)
{
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri(sEndpointAddress);
}
else
{
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri(sEndpointAddress);
svc.Description.Behaviors.Add(smb);
}
}
How can i start the server without waiting for a client to logon so i can initialize it.
Thanks in advance.
WCF will instantiate your MonitoringSystemService class as needed. It won't instantiate it until the first client makes a connection, and if you get a lot of client connections all at once, it will instantiate a few MonitoringSystemServices to deal with the load.
You can disable this behaviour, and instead just use one instance of MonitoringSystemService that gets created when your program starts. Instead of telling WCF which type it should be automatically instantiating, you just instantiate it yourself and pass it in:
_svc = new ServiceHost(new MonitoringSystemService()), address);
You gain control of when the MonitoringSystemService contructor runs, at the expense of scalability.
Alternatively (if you need the scalability), you could "initialize the connections" in your Main method, but be aware that WCF could instantiate multiple MonitoringSystemServices that would need to share those connections.
There are two ways I can immediately think of.
One - you can implement your solution as windows service
and Second - let a dummy client program call your server at start-up.